android - How to set both lines of a ListView using simple_list_item_2? -
so following create listview rows have "primary" textview filled values array.
arrayadapter<string> adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_2, android.r.id.text1, values); changing third parameter android.r.id.text2 sets "secondary" textview. there simple way set both?
afaik simple_list_item_2 contains twolinelistitem containing 2 textviews. arrayadapter not going work here,you'll either have create custom adapter or use 1 supports simplecursoradapter.
listadapter adapter = new simplecursoradapter( this, android.r.layout.simple_list_item_2, mcursor, // pass in cursor bind to. new string[] {people.name, people.company}, // array of cursor columns bind to. new int[] {android.r.id.text1, android.r.id.text2}); // parallel array of template objects bind columns. // bind our new adapter. setlistadapter(adapter); or if dont want simplecursoradapter have create custom arrayadapter or baseadapter
create custom arrayadapter,apply object(having 2 items) array custom adapter, , feed getlistview.setadapter.
override arrayadapter's getview method apply name strings textviews in custom list row view.
following snippet you.
sampleactivity.java
package org.sample; import java.util.arraylist; import android.app.listactivity; import android.content.context; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.textview; import android.widget.twolinelistitem; public class sampleactivity extends listactivity { @override public void oncreate(bundle icicle) { super.oncreate(icicle); person person; arraylist<person> persons = new arraylist<person>(); person = new person(); person.setname("vipul"); person.setage(20); persons.add(person); person = new person(); person.setname("anil"); person.setage(22); persons.add(person); setlistadapter(new myadapter(this, persons)); } } person.java
class person { string name; int age; public string getname() { return name; } public void setname(string name) { this.name = name; } public int getage() { return age; } public void setage(int age) { this.age = age; } } myadapter.java
class myadapter extends baseadapter { private context context; private arraylist<person> persons; public myadapter(context context, arraylist<person> persons) { this.context = context; this.persons = persons; } @override public int getcount() { return persons.size(); } @override public object getitem(int position) { return persons.get(position); } @override public long getitemid(int position) { return 0; } @override public view getview(int position, view convertview, viewgroup parent) { twolinelistitem twolinelistitem; if (convertview == null) { layoutinflater inflater = (layoutinflater) context .getsystemservice(context.layout_inflater_service); twolinelistitem = (twolinelistitem) inflater.inflate( android.r.layout.simple_list_item_2, null); } else { twolinelistitem = (twolinelistitem) convertview; } textview text1 = twolinelistitem.gettext1(); textview text2 = twolinelistitem.gettext2(); text1.settext(persons.get(position).getname()); text2.settext("" + persons.get(position).getage()); return twolinelistitem; } }
Comments
Post a Comment