java - Replace fragments and start actives based on string name instead of list view item position -
based on list view, how can replace detail container particular fragment or start particular activity based on name of string item selected rather position of item within list? i'm looking case string
rather case 0
, case 1
, case 2
, etc. code under inital incorrect code suggestion similar i'm looking not right solution. appreciated.
strings
<string-array name="continent_names"> //item 0 <item>@string/africa</item> //item 1 <item>@string/asia</item> //item 2 <item>@string/europe</item> </string-array> <string-array name="continent_descriptions"> //item 0 <item>@string/africa_description</item> //item 1 <item>@string/asia_description </item> //item 2 <item>@string/europe_description </item> </string-array>
fragmentworld.java
public class fragmentworld extends listfragment implements searchview.onquerytextlistener { private worldlistadapter madapter; public fragmentworld() { // required empty constructor } public static fragmentworld newinstance() { return new fragmentworld(); } @nullable @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view view = inflater.inflate(r.layout.fragment_world, container, false); sethasoptionsmenu(true); initialize(view); return view; } list<world> list = new arraylist<world>(); private void initialize(view view) { string[] items = getactivity().getresources().getstringarray(r.array.continent_names); string[] itemdescriptions = getactivity().getresources().getstringarray(r.array.continent_descriptions); (int n = 0; n < items.length; n++){ world world = new world(); world.setid(); world.setname(items[n]); world.setdescription(itemdescriptions[n]); list.add(world); } madapter = new worldlistadapter(list, getactivity()); setlistadapter(madapter); } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // adapter, name adapter @ position worldlistadapter adapter = (worldlistadapter) parent.getadapter(); string country = adapter.getitem(position); if (mtwopane) { setitemnormal(); view rowview = view; setitemselected(rowview); fragment newfragment; if (country.equals(view.getresources().getstring(r.string.africa))) { newfragment = new fragmentafrica(); } else if (country.equals(view.getresources().getstring(r.string.asia))) { newfragment = new fragmentasia(); } else if (country.equals(view.getresources().getstring(r.string.europe))) { newfragment = new fragmenteurope(); } else { newfragment = new fragmentafrica(); } worldactivity activity = (worldactivity) view.getcontext(); fragmenttransaction transaction = activity.getsupportfragmentmanager().begintransaction(); transaction.replace(r.id.detail_container, newfragment); transaction.commit(); } else { intent intent; if (country.equals(view.getresources().getstring(r.string.africa))) { intent = new intent(getactivity(), africaactivity.class); } else if (country.equals(view.getresources().getstring(r.string.asia))) { intent = new intent(getactivity(), asiaactivity.class); } else if (country.equals(view.getresources().getstring(r.string.europe))) { intent = new intent(getactivity(), europeactivity.class); } else { intent = new intent(getactivity(), africaactivity.class); } startactivity(intent); } } @override public void oncreateoptionsmenu(menu menu, menuinflater inflater) { // set search view inflater.inflate(r.menu.menu_world, menu); menuitem item = menu.finditem(r.id.action_search); searchview searchview = (searchview) menuitemcompat.getactionview(item); searchview.seticonifiedbydefault(true); searchview.clearanimation(); searchview.setonquerytextlistener(this); searchview.setqueryhint(getresources().getstring(r.string.search_hint)); view close = searchview.findviewbyid(r.id.search_close_btn); close.setbackgroundresource(r.drawable.ic_action_content_clear); } @override public boolean onquerytextsubmit(string newtext) { return false; } @override public boolean onquerytextchange(string newtext) { madapter.getfilter().filter(newtext); return false; } }
worldlistadapter.java
public class worldlistadapter extends baseadapter implements filterable { private list<world> mdata; private list<world> mfiltereddata; private layoutinflater minflater; private itemfilter mfilter; public worldlistadapter (list<world> data, context context) { mdata = data; mfiltereddata = new arraylist(mdata); minflater = (layoutinflater) context.getsystemservice(context.layout_inflater_service); } @override public int getcount() { return mfiltereddata.size(); } @override public string getitem(int position) { return mfiltereddata.get(position).getname(); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder; if (convertview == null) { convertview = minflater.inflate(r.layout.list_item_dualline, parent, false); holder = new viewholder(); holder.title = (textview) convertview.findviewbyid(r.id.item_name); holder.description = (textview) convertview.findviewbyid(r.id.item_description); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.title.settext(mfiltereddata.get(position).getname()); holder.description.settext(mfiltereddata.get(position).getdescription()); return convertview; } @override public filter getfilter() { if (mfilter == null) { mfilter = new itemfilter(); } return mfilter; } /** * view holder */ static class viewholder { private textview title; private textview description; } private class itemfilter extends filter { @override protected filterresults performfiltering(charsequence constraint) { filterresults results = new filterresults(); if (textutils.isempty(constraint)) { results.count = mdata.size(); results.values = new arraylist(mdata); } else { //create new list filter on list<world> resultlist = new arraylist<world>(); (world str : mdata) { if (str.getname().tolowercase().contains(constraint.tostring().tolowercase())) { resultlist.add(str); } } results.count = resultlist.size(); results.values = resultlist; } return results; } @suppresswarnings("unchecked") @override protected void publishresults(charsequence constraint, filterresults results) { if (results.count == 0) { mfiltereddata.clear(); notifydatasetinvalidated(); } else { mfiltereddata = (arraylist<world>)results.values; notifydatasetchanged(); } } } }
inital incorrect code suggestion
listview.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { if (mtwopane) { setitemnormal(); view rowview = view; setitemselected(rowview); fragment newfragment; switch (position) { case 0: newfragment = new fragmentafrica(); break; case 1: newfragment = new fragmentasia(); break; case 2: newfragment = new fragmenteurope(); break; default: newfragment = new fragmentafrica(); break; } worldactivity activity = (worldactivity) view.getcontext(); fragmenttransaction transaction = activity.getsupportfragmentmanager().begintransaction(); transaction.replace(r.id.detail_container, newfragment); transaction.commit(); } else { intent intent; switch (position) { case 0: intent = new intent(getactivity(), africaactivity.class); break; case 1: intent = new intent(getactivity(), asiaactivity.class); break; case 2: intent = new intent(getactivity(), europeactivity.class); break; default: intent = new intent(getactivity(), africaactivity.class); break; } startactivity(intent); } } public void setitemselected(view view) { view rowview = view; view.setbackgroundcolor(color.parsecolor("#0099cc")); textview tv0 = (textview) rowview.findviewbyid(r.id.item_name); tv0.settextcolor(color.parsecolor("#ffffff")); textview tv1 = (textview) rowview.findviewbyid(r.id.item_description); tv1.settextcolor(color.parsecolor("#ffffff")); } public void setitemnormal() { (int = 0; < listview.getchildcount(); i++) { view v = listview.getchildat(i); v.setbackgroundcolor(color.transparent); textview tv0 = ((textview) v.findviewbyid(r.id.item_name)); tv0.settextcolor(color.white); textview tv1 = ((textview) v.findviewbyid(r.id.item_description)); tv1.settextcolor(color.parsecolor("#b5b5b5")); } } });
what want adapter parent, country name adapter.
try this:
@override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // adapter, name adapter @ position worldlistadapter adapter = (worldlistadapter) parent.getadapter(); string country = adapter.getitem(position); if (mtwopane) { setitemnormal(); view rowview = view; setitemselected(rowview); fragment newfragment; if (country.equals(view.getresources().getstring(r.string.africa))) { newfragment = new fragmentafrica(); } else if (country.equals(view.getresources().getstring(r.string.asia))) { newfragment = new fragmentasia(); } else if (country.equals(view.getresources().getstring(r.string.europe))) { newfragment = new fragmenteurope(); } else { newfragment = new fragmentafrica(); } worldactivity activity = (worldactivity) view.getcontext(); fragmenttransaction transaction = activity.getsupportfragmentmanager().begintransaction(); transaction.replace(r.id.detail_container, newfragment); transaction.commit(); } else { intent intent; if (country.equals(view.getresources().getstring(r.string.africa))) { intent = new intent(getactivity(), africaactivity.class); } else if (country.equals(view.getresources().getstring(r.string.asia))) { intent = new intent(getactivity(), asiaactivity.class); } else if (country.equals(view.getresources().getstring(r.string.europe))) { intent = new intent(getactivity(), europeactivity.class); } else { intent = new intent(getactivity(), africaactivity.class); } startactivity(intent); } }
you comparisons using raghav's suggestion switch
statement strings.
Comments
Post a Comment