java - How can I get the n-th element in a ListView -
i have listview 2 items inside each row, textview , switch button.
this how created list view:
rootview = inflater.inflate(r.layout.fragment_list_view, container,false); listview list = (listview) rootview.findviewbyid(r.id.listviewfragment); rootview.findviewbyid(r.id.buttonsubmitplayerstrainingattendances) ; string[] = { "nameplayer", "switch" }; int[] = { r.id.textviewplayername, r.id.switchattendance }; list<hashmap<string,string>> alist = new arraylist<hashmap<string,string>>(); for(int a=0; < totalplayers ;a++) { hashmap<string, string> hm = new hashmap<string,string>(); hm.put("nameplayer", playerssurnames.get(a) + " " + playersnames.get(a) ); alist.add(hm); } simpleadapter adapter = new simpleadapter(getactivity().getbasecontext(), alist, r.layout.player_list, from, to); list.setadapter(adapter);
i used layout fragment_list_view.xml has listview , in simpleadapter used list> (alist) , layout player_list.xml has textview , switch. don’t know if right way works.
fragment_list_view.xml
<listview android:id="@+id/listviewfragment" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" > </listview>
player_list.xml
<textview android:id="@+id/textviewplayername" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:text="textview" /> <switch android:id="@+id/switchattendance" android:layout_width="wrap_content" android:layout_height="wrap_content" />
as can see listview works , see on screen want : list of players , switches each player:
screenshot --> image
now, if getitem() adapter see first element of row (player1, player2,...)
my question is: how can switch id of each row (each player) see if it’s checked or not? , if did listview in wrong way can explain me best way that? thank you
you may take of answer this, spannable string working last item in listview.
you can replace arrayadapter simpleadapter, this:
string[] = {item_id, item_name, item_checked}; int[] = {r.id.tvid, r.id.tvdescription, r.id.cb}; msimpleadapter = new simpleadapter(this, mdatalist, r.layout.custom_list_items, from, to){ @override public view getview(int position, view convertview, viewgroup parent) { // prepare views ready data convertview = super.getview(position, convertview, parent); checkbox cb = (checkbox) convertview.findviewbyid(r.id.cb); textview tvdescription = (textview) convertview.findviewbyid(r.id.tvdescription); cb.setonclicklistener(new onclicklistener(){ @suppresswarnings("unchecked") @override public void onclick(view view) { checkbox buttonview = (checkbox) view; // view, position , datalist(position) int pos = (int) buttonview.gettag(); hashmap<string, object> selecteditem = new hashmap<string, object>(); selecteditem = (hashmap<string, object>) getitem(pos); // update datalist mdatalist.remove(selecteditem); selecteditem.remove(item_checked); if(buttonview.ischecked()){ selecteditem.put(item_checked, true); mdatalist.add(pos, selecteditem); }else{ selecteditem.put(item_checked, false); mdatalist.add(pos, selecteditem); } // update ui views toast.maketext(getapplicationcontext(), ""+pos+" changed", toast.length_short).show(); notifydatasetchanged(); } }); // data datalist @suppresswarnings("unchecked") hashmap<string, object> currentitem = (hashmap<string, object>) getitem(position); togglelinethrough(tvdescription, (string)currentitem.get(item_name), (boolean)currentitem.get(item_checked)); // save position checkbox, position can retrieved when checkbox clicked cb.settag(position); return convertview; } }; mlistview.setadapter(msimpleadapter);
hope help!
Comments
Post a Comment