java - Android listview custom rows -
i have problem custom layout i've set listview. have button adds elements listview , uses custom row everytime try press button add crashes. can suggest solution on how fix this? i'm pretty new android btw.
here's code:
import android.app.activity; import android.content.intent; import android.os.bundle; import android.view.view; import android.widget.adapterview; import android.widget.arrayadapter; import android.widget.button; import android.widget.listadapter; import android.widget.listview; import java.util.arraylist; public class projectcreatescreen extends activity { @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.secondary_layout1); button btn = (button) findviewbyid(r.id.addbtn); final arraylist<string> listitems=new arraylist<string>(); final listadapter addadapter = new arrayadapter<string>(this, r.layout.custom_row, listitems); listview lv = (listview) findviewbyid(r.id.lv); lv.setadapter(addadapter); btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { listitems.add("new project"); ((arrayadapter) addadapter).notifydatasetchanged(); } }); lv.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { intent switchtoedit = new intent(projectcreatescreen.this, teamcreatescreen.class); startactivity(switchtoedit); } }); }
}
the layout activity uses:
<?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:id="@+id/rl"> <button android:layout_width="match_parent" android:layout_height="60dp" android:text="@string/addproject" android:id="@+id/addbtn" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:onclick="startproject"/> <listview android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/addbtn" android:id="@+id/lv"> </listview> </relativelayout>
the custom row layout:
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <textview android:layout_width="wrap_content" android:layout_height="80dp" android:id="@+id/customrow"/> </linearlayout>
edit: need provide r.id.customrow
arrayadapter
constructor.
this should fix it:
final listadapter addadapter = new arrayadapter<string>(this, r.layout.custom_row, r.id.customrow, listitems);
with original code, added data arraylist, , got exception:
04-09 13:12:34.992 12418-12418/com.listviewtest.daniel.listviewtest e/arrayadapter﹕ must supply resource id textview 04-09 13:12:34.992 12418-12418/com.listviewtest.daniel.listviewtest d/androidruntime﹕ shutting down vm 04-09 13:12:34.992 12418-12418/com.listviewtest.daniel.listviewtest w/dalvikvm﹕ threadid=1: thread exiting uncaught exception (group=0x41891da0) 04-09 13:12:35.012 12418-12418/com.listviewtest.daniel.listviewtest e/androidruntime﹕ fatal exception: main process: com.listviewtest.daniel.listviewtest, pid: 12418 java.lang.illegalstateexception: arrayadapter requires resource id textview