android - Can not set OnListItemClick on Tab Activity -
i want activity tab bar, , have 5 tab menus. mainactivity includes tabhost that;
public class mainactivity extends tabactivity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); requestwindowfeature(window.feature_no_title); setcontentview(r.layout.activity_main); string tab_title[] = { "tab 1", "tab 2", "tab 3", "tab 4", "tab 5" }; int tab_drawables[] = { r.drawable.menu_bookmark, r.drawable.menu_filemanager, r.drawable.menu_download, r.drawable.menu_sharepage, r.drawable.menu_about }; object tab_act[] = { tab1.class, tab2.class, tab3.class, tab4.class, tab5.class }; final tabhost tabhost = (tabhost) findviewbyid(android.r.id.tabhost); tabhost.setup(); tabspec tab_spec; (int = 0; < tab_act.length; i++) { tab_spec = tabhost.newtabspec(tab_title[i]); tab_spec.setindicator(tab_title[i], getresources().getdrawable(tab_drawables[i])); tab_spec.setcontent(new intent(this, (class<?>) tab_act[i])); tabhost.addtab(tab_spec); } tabhost.setcurrenttab(0); } @override public boolean onkeydown(int keycode, keyevent event) { if (keycode == keyevent.keycode_back) { mainactivity.this.finish(); return true; } return super.onkeydown(keycode, event); }
}
when click on tab2, tab2.java class called. class has listview. want play music on youtube when click on list item. code working onlistitemclick method not working.
public class tab2 extends activity{ private layoutinflater minflater; private vector<rowdata> data; rowdata rd; context context ; bitmap bitmap; public static string title[] = { "song 1", "song 2", "song 3"}; @override protected void oncreate(bundle savedinstancestate) { setcontentview(r.layout.tab2); super.oncreate(savedinstancestate); minflater = (layoutinflater) getsystemservice(activity.layout_inflater_service); data = new vector<rowdata>(); (int = 0; < title.length; i++) { try { rd = new rowdata(i, title[i]); } catch (parseexception e) { e.printstacktrace(); } data.add(rd); } customadapter adapter = new customadapter(this, r.layout.activity_list_view, r.id.title, data); } private class rowdata { protected int mid; protected string mtitle; rowdata(int id, string title) { mid = id; mtitle = title; } @override public string tostring() { return mid + " " + mtitle; } } public void onlistitemclick(listview parent, view v, int position, long id) { if (position == 0) { startactivity(new intent(intent.action_view,uri.parse("http://www.youtube.com/watch?v=qk8mjjjvaes"))); } else if (position == 1) { startactivity(new intent(intent.action_view,uri.parse("http://www.youtube.com/watch?v=hwyeej2pst0"))); } else if (position == 2){ startactivity(new intent(intent.action_view,uri.parse("http://www.youtube.com/watch?v=hwyeej2pst0"))); } } private class customadapter extends arrayadapter<rowdata> { public customadapter(context context, int resource, int textviewresourceid, list<rowdata> objects) { super(context, resource, textviewresourceid, objects); } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; textview title = null; rowdata rowdata = getitem(position); if (null == convertview) { convertview = minflater .inflate(r.layout.activity_list_view, null); holder = new viewholder(convertview); convertview.settag(holder); } holder = (viewholder) convertview.gettag(); title = holder.gettitle(); title.settext(rowdata.mtitle); return convertview; } private class viewholder { private view mrow; private textview title = null; private imageview i11 = null; public viewholder(view row) { mrow = row; } public textview gettitle() { if (null == title) { title = (textview) mrow.findviewbyid(r.id.title); } return title; } } }
}
i have no problem layouts, m sure no need show xml files. :)
updated
i change tab2 class following codes;
public class tab2 extends activity implements adapterview.onitemclicklistener{ private layoutinflater minflater; private vector<rowdata> data; rowdata rd; context context ; bitmap bitmap; public static string title[] = { "song 1", "song 2", "song 3"}; @override protected void oncreate(bundle savedinstancestate) { setcontentview(r.layout.tab2); super.oncreate(savedinstancestate); minflater = (layoutinflater) getsystemservice(activity.layout_inflater_service); data = new vector<rowdata>(); (int = 0; < title.length; i++) { try { rd = new rowdata(i, title[i]); } catch (parseexception e) { e.printstacktrace(); } data.add(rd); } customadapter adapter = new customadapter(this, r.layout.activity_list_view, r.id.title, data); listview list = (listview) findviewbyid(r.id.list); list.setadapter(adapter); list.setonitemclicklistener(this); } private class rowdata { protected int mid; protected string mtitle; rowdata(int id, string title) { mid = id; mtitle = title; } @override public string tostring() { return mid + " " + mtitle; } } private class customadapter extends arrayadapter<rowdata> { public customadapter(context context, int resource, int textviewresourceid, list<rowdata> objects) { super(context, resource, textviewresourceid, objects); } @override public view getview(int position, view convertview, viewgroup parent) { viewholder holder = null; textview title = null; rowdata rowdata = getitem(position); if (null == convertview) { convertview = minflater .inflate(r.layout.activity_list_view, null); holder = new viewholder(convertview); convertview.settag(holder); } holder = (viewholder) convertview.gettag(); title = holder.gettitle(); title.settext(rowdata.mtitle); return convertview; } private class viewholder { private view mrow; private textview title = null; private imageview i11 = null; public viewholder(view row) { mrow = row; } public textview gettitle() { if (null == title) { title = (textview) mrow.findviewbyid(r.id.title); } return title; } } } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // todo auto-generated method stub if (position == 0) { startactivity(new intent(intent.action_view,uri.parse("http://www.youtube.com/watch?v=4jhrdlufa-8"))); } else if (position == 1) { startactivity(new intent(intent.action_view,uri.parse("http://www.youtube.com/watch?v=-hmfkutyhog"))); } else if (position == 2){ startactivity(new intent(intent.action_view,uri.parse("http://www.youtube.com/watch?v=7bebw8uqor8"))); } }
}
public class tab2 extends activity
tab2
extends activity, not listactivity
.
to working, find listview
widget in r.layout.tab2
:
super.oncreate(savedinstancestate); setcontentview(r.layout.tab2); minflater = (layoutinflater) getsystemservice(activity.layout_inflater_service); data = new vector<rowdata>(); (int = 0; < title.length; i++) { try { rd = new rowdata(i, title[i]); } catch (parseexception e) { e.printstacktrace(); } data.add(rd); } customadapter adapter = new customadapter(this, r.layout.activity_list_view, r.id.title, data); listview lv = (listview) findviewbyid(r.id.your_listiew_id); lv.setadapter(adapter); lv.setonitemclicklistener(this);
change class definition to:
public class tab2 extends activity implements adapterview.onitemclicklistener {
and change onlistitemclick(listview parent, view v, int position, long id)
to:
@override public void onitemclick((adapterview<?> parent, view view, int position, long id)
the problem code never assign onitemclicklistener
listview
. , if want use onlistitemclick (listview l, view v, int position, long id)
, tab2
should extend listactivity
, not activity
. see why: link.
Comments
Post a Comment