Android: fill a listview with an object -
i wondering if possible put list of textview inside listview in android. tried code result not give me right text wanted. want display 20 toto testing.
here main.class
public class mainactivity extends activity { private listview listview; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); listview = (listview) findviewbyid(r.id.listcontacts); // instanciating array list (you don't need this, // have yours). list<textview> contacts = new arraylist<textview>(); for(int = 0 ; i< 20; i++) { textview toto = new textview(this); toto.settext("toto"); contacts.add(toto); } // array adapter, takes context of activity // first parameter, type of list view second parameter , // array third parameter. arrayadapter<textview> arrayadapter = new arrayadapter<textview>( this, android.r.layout.simple_list_item_1, contacts ); listview.setadapter(arrayadapter); }
then xml file of main
<listview android:id="@+id/listcontacts" android:layout_width="wrap_content" android:layout_height="wrap_content" > </listview>
should need create textview inside listview , call "findviewbyid(textview)" in class?
thanks.
should need create textview inside listview , call "findviewbyid(textview)" in class?
no shouldn't. adapter responsible adapt dataset sort of visualization. arrayadapter
, instance, using textview contained simple_list_item_1.xml
. in case, contacts
, should arraylist<string>
, adapter should adapter<string>
Comments
Post a Comment