android - Open different activities from listview -
i open activity each of listview items. done research around onitemclicklistener don't know quite how fit in current code.
my activities called:
bhutaninfo.java
columbiainfo.java
my listview set in xml file:
<?xml version="1.0" encoding="utf-8"?> <resources> <string-array name="country_names"> <item>bhutan</item> <item>colombia</item> </string-array> <array name="country_icons"> <item>@drawable/bhutan</item> <item>@drawable/colombia</item> </array> </resources>
in class file have following:
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); context ctx = getapplicationcontext(); resources res = ctx.getresources(); string[] options = res.getstringarray(r.array.country_names); typedarray icons = res.obtaintypedarray(r.array.country_icons); setlistadapter(new imageandtextadapter(ctx, r.layout.main_list_item, options, icons)); }
i have class sets adapter.
public class imageandtextadapter extends arrayadapter<string> { private layoutinflater minflater; private string[] mstrings; private typedarray micons; private int mviewresourceid; public imageandtextadapter(context ctx, int viewresourceid, string[] strings, typedarray icons) { super(ctx, viewresourceid, strings); minflater = (layoutinflater)ctx.getsystemservice( context.layout_inflater_service); mstrings = strings; micons = icons; mviewresourceid = viewresourceid; } @override public int getcount() { return mstrings.length; } @override public string getitem(int position) { return mstrings[position]; } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { convertview = minflater.inflate(mviewresourceid, null); imageview iv = (imageview)convertview.findviewbyid(r.id.option_icon); iv.setimagedrawable(micons.getdrawable(position)); textview tv = (textview)convertview.findviewbyid(r.id.option_text); tv.settext(mstrings[position]); return convertview; }
any appreciated.
if had 2 items means can redirect activity based on position , can perform way
listview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { string str = parent.getitematposition(position).tostring(); log.e("clicked value is", str); if(str.equals("<your class name>")){ startactivity(new intent(mainactivity.this, firstactivity.class); }else{ startactivity(new intent(mainactivity.this, second.class); } } });
Comments
Post a Comment