listview - Android Custom BaseAdapter -
i have baseadapter display listview section. each row have textview, want add textview each row. in item
class , in getview()
method, added new string text_id
, when call adapter have error: item(string, string) in item cannot applied
.
public class alphabetlistadapter extends baseadapter { public static abstract class row {} public static final class section extends row { public final string text; public section(string text) { this.text = text; } } public static final class item extends row { public final string text; public final string text_id; public item(string text, string text_id ) { this.text = text; this.text_id = text_id; } } private list<row> rows; public void setrows(list<row> rows) { this.rows = rows; } @override public int getcount() { return rows.size(); } @override public row getitem(int position) { return rows.get(position); } @override public long getitemid(int position) { return position; } @override public int getviewtypecount() { return 2; } @override public int getitemviewtype(int position) { if (getitem(position) instanceof section) { return 1; } else { return 0; } } @override public view getview(int position, view convertview, viewgroup parent) { view view = convertview; if (getitemviewtype(position) == 0) { // item if (view == null) { layoutinflater inflater = (layoutinflater) parent.getcontext().getsystemservice(context.layout_inflater_service); view = (linearlayout) inflater.inflate(r.layout.row_item, parent, false); } item item = (item) getitem(position); textview textview = (textview) view.findviewbyid(r.id.textview1); textview textview_id = (textview) view.findviewbyid(r.id.id_cl); textview.settext(item.text); textview_id.settext(item.text_id); } else { // section if (view == null) { layoutinflater inflater = (layoutinflater) parent.getcontext().getsystemservice(context.layout_inflater_service); view = (linearlayout) inflater.inflate(r.layout.row_section, parent, false); } section section = (section) getitem(position); textview textview = (textview) view.findviewbyid(r.id.textview1); textview.settext(section.text); } return view; } }
in oncreateview()
list<string> clienti = populateclienti(); collections.sort(clienti); list<alphabetlistadapter.row> rows = new arraylist<alphabetlistadapter.row>(); int start = 0; int end = 0; string previousletter = null; object[] tmpindexitem = null; (string country : clienti) { string firstletter = country.substring(0, 1); // if we've changed new letter, add previous letter alphabet scroller if (previousletter != null && !firstletter.equals(previousletter)) { end = rows.size() - 1; tmpindexitem = new object[3]; tmpindexitem[0] = previousletter.touppercase(locale.italian); tmpindexitem[1] = start; tmpindexitem[2] = end; alphabet.add(tmpindexitem); start = end + 1; } // check if need add header row if (!firstletter.equals(previousletter)) { rows.add(new alphabetlistadapter.section(firstletter)); sections.put(firstletter, start); } // add country list rows.add(new alphabetlistadapter.item(country)); previousletter = firstletter; } if (previousletter != null) { // save last letter tmpindexitem = new object[3]; tmpindexitem[0] = previousletter.touppercase(locale.italy); tmpindexitem[1] = start; tmpindexitem[2] = rows.size() - 1; alphabet.add(tmpindexitem); } adapter.setrows(rows); lista.setadapter(adapter); lista.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { textview temp = (textview) view.findviewbyid(r.id.textview1); string str = temp.gettext().tostring(); toast.maketext(getactivity(),str + " pressed " + position, toast.length_short).show(); } }); updatelist(); return rootview; }
row_item.xml
<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:padding="10dp" > <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="textview" android:textcolor="#000000" android:textsize="20sp" /> <textview android:layout_width="wrap_content" android:layout_height="wrap_content" android:textappearance="?android:attr/textappearancesmall" android:text="small text" android:id="@+id/id_cl" />
the easy way accomplish add textview
in xml layout file r.layout.row_section
Comments
Post a Comment