java - Custom ListView won't show up in the Activity -
i trying create listview custom row layout , adapter in android studio. followed this tutorial attempted simpler version familiarize myself it. not fill data in listview data in json format or use imageloader, trying use similar format set strings' content manually.
i created row.xml
simple textfield "name". created simple listmodel create , set methods string name. tried create , use custom adapter listmodel place content in listviewactivity. finally, in listviewactivity tried define content of string name , show in view.
i have no errors when running application, listview won't show in activity. expected output single row "hiiiii" init. doing wrong?
list_view.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/background2" android:id="@+id/listviewactivity"> <listview android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/challengeslistview" android:choicemode="singlechoice" /> </relativelayout>
row.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="wrap_content" android:padding="5dp"> <textview android:id="@+id/name" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textstyle="bold" android:gravity="center" android:textsize="17sp" android:textcolor="#000000" />
listmodel.java
public class listmodel { private string name=""; public listmodel() { } public listmodel(string name) { this.name = name; } /*********** set methods ******************/ public void setname(string name) { this.name = name; } /*********** methods ****************/ public string getname() { return this.name; } }
customlistadapter.java
import android.app.activity; import android.content.context; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.baseadapter; import android.widget.textview; import java.util.list; public class customlistadapter extends baseadapter { private activity activity; private layoutinflater inflater; private list<listmodel> items; public customlistadapter(activity activity, list<listmodel> items) { this.activity = activity; this.items = items; } @override public int getcount() { return items.size(); } @override public object getitem(int location) { return items.get(location); } @override public long getitemid(int position) { return position; } @override public view getview(int position, view convertview, viewgroup parent) { if (inflater == null) inflater = (layoutinflater) activity .getsystemservice(context.layout_inflater_service); if (convertview == null) convertview = inflater.inflate(r.layout.row, null); textview name = (textview) convertview.findviewbyid(r.id.name); convertview.setbackgroundresource(r.drawable.testimage); listmodel m = items.get(position); // title name.settext(m.getname()); return convertview; } }
listviewactivity.java
import android.app.activity; import android.content.intent; import android.os.bundle; import android.widget.listview; import java.util.arraylist; import java.util.list; public class listviewactivityextends activity { private list<listmodel> list = new arraylist<listmodel>(); private listview listview; private customlistadapter adapter; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.challenges_view); intent intent = getintent(); listview = (listview) findviewbyid(r.id.list_view); adapter = new customlistadapter(this, list); listview.setadapter(adapter); listmodel listmodel = new listmodel(); listmodel.setname("hiiiiii"); } }
private list<listmodel> list = new arraylist<listmodel>();
your list object of size zero. havnt seen data initialization that
listview = (listview) findviewbyid(r.id.list_view); adapter = new customlistadapter(this, list); listview.setadapter(adapter);
in adapter getcount method return size , 0 in case. pls check
you should initialize list data. or change code this
@override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.list_view); intent intent = getintent(); //create model data listmodel listmodel = new listmodel(); listmodel.setname("hiiiiii"); //add model collection list.add(listmodel); //then can set adapter listview = (listview) findviewbyid(r.id.list_view); adapter = new customlistadapter(this, list); listview.setadapter(adapter); }
Comments
Post a Comment