android - ExpandableListView ignores setSelectedChild() -
i try select item in expandablelistview using setselectedchild(int groupposition, int childposition, boolean shouldexpandgroup), nothing happens.
here's primitive activity tested with:
import android.app.activity; import android.graphics.color; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.view.viewgroup; import android.widget.baseexpandablelistadapter; import android.widget.button; import android.widget.expandablelistview; import android.widget.textview; public class startupactivity extends activity { private static final string[] _items = new string[] {"oops", "wham", "bam"}; private expandablelistview _explist; private textview _txt_hello; private button _btn_sel; private myexplistadapter _explist_adapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.main); _explist = (expandablelistview) findviewbyid(r.id.explistmain); _txt_hello = (textview) findviewbyid(r.id.txthello); _btn_sel = (button) findviewbyid(r.id.btnaction); _btn_sel.setonclicklistener(new onclicklistener() { @override public void onclick(view arg0) { selectelement(); } }); } @override protected void onresume() { super.onresume(); _explist_adapter = new myexplistadapter(); _explist.setadapter(_explist_adapter); _explist.expandgroup(0); } private void selectelement() { boolean success = _explist.setselectedchild(0, 1, true); long sel = _explist.getselectedid(); _txt_hello.settext((success ? "success" : "failure") + " " + sel); //setselectedchild() returns true //but getselectedid() returns -1 , nothing selected } public class myexplistadapter extends baseexpandablelistadapter { @override public object getchild(int groupposition, int childposition) { return _items[childposition]; } @override public long getchildid(int groupposition, int childposition) { return childposition; } @override public view getchildview(int groupposition, int childposition, boolean islastchild, view convertview, viewgroup parent) { if (convertview == null) { convertview = new textview(startupactivity.this); } textview txt_view = (textview) convertview; txt_view.settext(_items[childposition]); txt_view.setheight(50); return txt_view; } @override public int getchildrencount(int groupposition) { return _items.length; } @override public object getgroup(int groupposition) { return getgroupid(groupposition); } @override public int getgroupcount() { return 1; } @override public long getgroupid(int groupposition) { return 0; } @override public view getgroupview(int groupposition, boolean isexpanded, view convertview, viewgroup parent) { textview txt_view = new textview(startupactivity.this); txt_view.setbackgroundcolor(color.gray); txt_view.setheight(50); return txt_view; } @override public boolean hasstableids() { return true; } @override public boolean ischildselectable(int groupposition, int childposition) { return true; } } }
the layout file:
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"> <textview android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="@string/hello" android:id="@+id/txthello" /> <button android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/btnaction" android:text="select"></button> <expandablelistview android:layout_width="match_parent" android:id="@+id/explistmain" android:layout_height="fill_parent"></expandablelistview> </linearlayout>
how can make work?
i wrestling issue in setselectedchild(int, int, true)
did not expand group says does. got around using strikingly intuitive method of calling expandablelistview.expandgroup(int);
this answer given me co-worker, i'll pass props on if helps.
Comments
Post a Comment