android - Get values of views inside listview item on click -
i have listview containing different xml layouts, each different views. wondering how can data views inside item user has clicked on, inside onitemclick
method.
my listview made of multiple layouts cannot know sure layout being pressed when onitemclick method called. example, 1 of items in layouts in listview is:
// list_item.xml <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <textview android:id="@+id/lblsub" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentright="true" android:layout_alignparenttop="true" android:padding="6dp" android:layout_marginright="4dp" android:text="" android:textcolor="#ffffff" android:textsize="18sp" /> <textview android:id="@+id/lblmain" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:padding="6dp" android:layout_marginleft="4dp" android:text="" android:textcolor="#ffffff" android:textsize="18sp" /> </relativelayout>
where layout in listview be:
// list_item_name.xml <?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <textview android:id="@+id/lblname" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" android:layout_marginleft="4dp" android:padding="4dp" android:text="textview" android:textcolor="#ffffff" android:textsize="28sp" /> </relativelayout>
so @ loss how data these views layout within listview.
my previous attempt this:
public void onitemclick(adapterview<?> listview, view view, int position, long id) { object obj = adapter.getitem(position); string value = obj.tostring(); }
however value
in instance, returns string containing values of views inside item, example: {caption=mobilenumber, value=07000123456}
<<< cannot string unless try , pick apart using substring
, unreliable @ best considering values may have been created user.
this adapter in use (credit jeff sharkey, did not create adapter):
public class separatedlistadapter extends baseadapter { public final map<string,adapter> sections = new linkedhashmap<string,adapter>(); public final arrayadapter<string> headers; public final static int type_section_header = 0; public separatedlistadapter(context context) { headers = new arrayadapter<string>(context, r.layout.list_header); } public void addsection(string section, adapter adapter) { this.headers.add(section); this.sections.put(section, adapter); } public void addsectionnoheader(string section, adapter adapter) { this.sections.put(section, adapter); } public object getitem(int position) { for(object section : this.sections.keyset()) { adapter adapter = sections.get(section); int size = adapter.getcount() + 1; // check if position inside section if(position == 0) return section; if(position < size) return adapter.getitem(position - 1); // otherwise jump next section position -= size; } return null; } public int getcount() { // total sections, plus 1 each section header int total = 0; for(adapter adapter : this.sections.values()) total += adapter.getcount() + 1; return total; } public int getviewtypecount() { // assume headers count one, total sections int total = 1; for(adapter adapter : this.sections.values()) total += adapter.getviewtypecount(); return total; } public int getitemviewtype(int position) { int type = 1; for(object section : this.sections.keyset()) { adapter adapter = sections.get(section); int size = adapter.getcount() + 1; // check if position inside section if(position == 0) return type_section_header; if(position < size) return type + adapter.getitemviewtype(position - 1); // otherwise jump next section position -= size; type += adapter.getviewtypecount(); } return -1; } public boolean areallitemsselectable() { return false; } public boolean isenabled(int position) { return (getitemviewtype(position) != type_section_header); } @override public view getview(int position, view convertview, viewgroup parent) { int sectionnum = 0; for(object section : this.sections.keyset()) { adapter adapter = sections.get(section); int size = adapter.getcount() + 1; // check if position inside section if(position == 0) return headers.getview(sectionnum, convertview, parent); if(position < size) return adapter.getview(position - 1, convertview, parent); // otherwise jump next section position -= size; sectionnum++; } return null; } @override public long getitemid(int position) { return position; } }
i'm looking way of extracting values of all views inside item. fact item has been clicked 1 of layouts confusing me. ideas?
edit:
here example of how using adapter in code:
separatedlistadapter adapter; customer cust; listview lvdetails; public map<string,?> createdoubleitem(string title, string caption) { map<string,string> item = new hashmap<string,string>(); item.put(item_value, title); item.put(item_caption, caption); return item; } public map<string,?> createsingleitem(string name) { map<string,string> item = new hashmap<string,string>(); item.put(item_value, name); return item; } @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_view_customer); intent intent = getintent(); cust = db.functions.getcustomer(intent.getstringextra("uuid")); lvdetails = (listview) findviewbyid(r.id.custdetails); adapter = new separatedlistadapter(this); lvdetails.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> listview, view view, int position, long id) { gen.popup("item clicked"); } }); // name list<map<string,?>> name = new linkedlist<map<string,?>>(); if (cust.firstname == null && cust.lastname == null) name.add(createsingleitem("unnamed customer")); else name.add(createsingleitem( ((cust.title != null) ? cust.title + " " : "") + ((cust.firstname != null) ? cust.firstname + " " : "") + ((cust.lastname != null) ? cust.lastname : ""))); adapter.addsection("name", new simpleadapter(this, name, r.layout.list_item_name, new string[] { item_value }, new int[] { r.id.lblname })); // phones if ((cust.mobilenumber != null || cust.homenumber != null || cust.worknumber != null) && show_phones) { list<map<string,?>> phone = new linkedlist<map<string,?>>(); if (cust.mobilenumber != null) phone.add(createdoubleitem(cust.mobilenumber, "mobile")); if (cust.homenumber != null) phone.add(createdoubleitem(cust.homenumber, "home")); if (cust.worknumber != null) phone.add(createdoubleitem(cust.worknumber, "work")); adapter.addsection("phone", new simpleadapter(this, phone, r.layout.list_item, new string[] { item_value, item_caption }, new int[] { r.id.lblsub, r.id.lblmain })); } lvdetails.setadapter(adapter); adapter.notifydatasetchanged(); } }
first off link provided adapter looks old (2008, 5 years ago) i'd @ 3rd party library this
but that's besides point.
the design of adapter convoluted there's multiple adapters in can't sure how work without spinning own test project.
now when call in onitemclick want add following
int viewtype = adapter.getitemviewtype(position); if (viewtype == separatedlistadapter.type_section_header) { //header clicked handle here maybe nothing } else { map<string,?> clickeditem = (map<string,?>) adapter.getitem(position); string value = (string) clickeditem.get(item_value); string caption = (string) clickeditem.get(item_caption); //here can things clicked item //also because know view type can find other views on }
edit: adjusted onclick should based on provided
Comments
Post a Comment