android - listview does not display -
im making project custom listview. when run project blank activity. wrote baseadapter class put data in listview. pass string arraylist baseadapter fill listview. help? thanks
public class mainactivity extends activity { listview l; arraylist<string> data= new arraylist<string>(); @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); data.add("two"); data.add("three"); data.add("four"); l=(listview) findviewbyid(r.id.listview1); myadapter ada = new myadapter(mainactivity.this,data); l.setadapter(ada); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; }
}
public class myadapter extends baseadapter {
context context; private arraylist<string> data= new arraylist<string>(); public myadapter(context context ,arraylist<string> data ) { this.context=context; this.data=data; } @override public int getcount() { // todo auto-generated method stub return 0; } @override public object getitem(int position) { // todo auto-generated method stub return null; } @override public long getitemid(int position) { // todo auto-generated method stub return position; } @override public view getview(int position, view convertview, viewgroup parent) { // todo auto-generated method stub viewholder holder; convertview = null; if (convertview == null) { convertview = layoutinflater.from(context) .inflate(r.layout.listlayout, parent, false); holder = new viewholder(); holder.mainpic = (imageview)convertview.findviewbyid(r.id.imageview1); holder.text= (textview)convertview.findviewbyid(r.id.textview1); convertview.settag(holder); } else { holder = (viewholder) convertview.gettag(); } holder.text.settext(this.data.get(position)); return convertview; } static class viewholder { imageview mainpic; textview text; }
}
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context=".mainactivity" > <listview android:id="@+id/listview1" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparenttop="true" > </listview> </relativelayout>
change getcount()
this:
@override public int getcount() { return data.size(); }
currently, return 0, tells listview there no items display.
also, might consider implementing getitem()
:
@override public object getitem(int position) { return data.get(position); }
and use items anywhere else.
Comments
Post a Comment