java - Buttons in a ListView row with two layouts and one activity -
i want add 2 buttons in each row of listview. while using 1 activity means in code have 1 mainactivity.java , layout (activity_main.xml) , additional layout (list_item.xml) have items in each row (textviews) when add 2 buttons in list_item.xml , initialize in mainactivity , add listners on it shows me exception shared below.help me solve problem, sort of highly appreciated.
mainactivity.java
public class mainactivity extends actionbaractivity { string myjson ; string id; private static final string tag_results="result"; private static final string tag_id = "id"; private static final string tag_name = "name"; private static final string tag_add ="address"; jsonarray peoples = null; arraylist <hashmap <string, string> > personlist; listview list; textview quant; int count=0; button b_plus,b_minus; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); list = (listview) findviewbyid(r.id.listview); personlist = new arraylist<hashmap<string,string>>(); quant=(textview)findviewbyid(r.id.quantity); b_plus=(button)findviewbyid(r.id.ib_plus); b_minus=(button)findviewbyid(r.id.ib_minus); b_plus.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { count++; quant.settext(integer.tostring(count)); } }); b_minus.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { count--; quant.settext(integer.tostring(count)); } }); getdata(); } protected void showlist(){ try { jsonobject jsonobj = new jsonobject(myjson); peoples = jsonobj.getjsonarray(tag_results); for(int i=0;i<peoples.length();i++){ jsonobject c = peoples.getjsonobject(i); id = c.getstring(tag_id); string name = c.getstring(tag_name); string address = c.getstring(tag_add); hashmap<string,string> persons = new hashmap<string,string>(); persons.put(tag_id,id); persons.put(tag_name,name); persons.put(tag_add,address); personlist.add(persons); } final listadapter adapter = new simpleadapter ( mainactivity.this, personlist, r.layout.list_item, new string[]{tag_id,tag_name,tag_add}, new int[]{r.id.id, r.id.name, r.id.address} ); list.setadapter(adapter); list.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> adapterview, view view, int i, long l) { switch(i) { case 0 : intent appinfo = new intent(mainactivity.this, mainactivity2.class); startactivity(appinfo); break; case 1 : intent ap = new intent(mainactivity.this, mainactivity3.class); startactivity(ap); break; case 2 : intent info = new intent(mainactivity.this, mainactivity2.class); startactivity(info); break; }} }); } catch (jsonexception e) { e.printstacktrace(); } } public void getdata(){ class getdatajson extends asynctask<string, void, string>{ @override protected string doinbackground(string... params) { defaulthttpclient httpclient = new defaulthttpclient(new basichttpparams()); httppost httppost = new httppost("http://10.0.2.2/in.php"); // depends on web service httppost.setheader("content-type", "application/json"); inputstream inputstream = null; string result = null; try { httpresponse response = httpclient.execute(httppost); httpentity entity = response.getentity(); inputstream = entity.getcontent(); // json utf-8 default bufferedreader reader = new bufferedreader(new inputstreamreader(inputstream, "utf-8"), 8); stringbuilder sb = new stringbuilder(); string line = null; while ((line = reader.readline()) != null) { sb.append(line + "\n"); } result = sb.tostring(); } catch (exception e) { // oops } { try{if(inputstream != null)inputstream.close();}catch(exception squish){} } return result; } @override protected void onpostexecute(string result){ myjson=result; showlist(); } } getdatajson g = new getdatajson(); g.execute(); } }
activity_main.xml
<linearlayout 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:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" android:orientation="vertical" android:background="#ff3c3f41" android:paddingbottom="@dimen/activity_vertical_margin" tools:context=".mainactivity"> <listview android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/listview" /> </linearlayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" android:padding="10dp" android:paddingleft="10dp" android:paddingright="10dp" > <textview android:id="@+id/id" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingbottom="2dip" android:paddingtop="6dip" android:textcolor="#fff9f9f9" android:textstyle="bold" /> <textview android:id="@+id/name" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingbottom="2dip" android:textcolor="#fff9f9f9" android:textstyle="bold"/> <textview android:id="@+id/address" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:textcolor="#fff9f9f9" android:textstyle="bold" /> <textview android:id="@+id/quantity" android:layout_width="wrap_content" android:layout_height="wrap_content" android:gravity="left" android:textcolor="#fff9f9f9" android:textstyle="bold" /> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="166dp" android:layout_height="wrap_content" android:orientation="horizontal" android:padding="10dp" android:paddingleft="10dp" android:paddingright="10dp" > <imagebutton android:layout_width="58dp" android:layout_height="52dp" android:src="@drawable/plus" android:layout_gravity="start" android:id="@+id/ib_plus" /> <imagebutton android:layout_width="58dp" android:layout_height="52dp" android:src="@drawable/minus" android:id="@+id/ib_minus" android:layout_gravity="center_horizontal" android:layout_alignbottom="@+id/ib_plus" android:layout_alignparentright="true" android:layout_alignparentend="true" android:layout_aligntop="@+id/ib_plus" /> </relativelayout>
logcat:
caused by: java.lang.nullpointerexception: attempt invoke virtual method 'void android.widget.button.setonclicklistener(android.view.view$onclicklistener)' on null object reference @ com.example.abdul.zx.mainactivity.oncreate(mainactivity.java:62)
you'll have make adapter more complicated, because there not 1 many buttons , text fields. pseudo code solution below. replace line create adapter this:
final listadapter adapter = new simpleadapter (mainactivity.this, personlist, r.layout.list_item, new string[]{tag_id,tag_name,tag_add}, new int[]{r.id.id, r.id.name, r.id.address}) { public view getview(int position, view convertview, viewgroup parent) { view view = super.getview(position, convertview, parent); final textview quant=(textview)view.findviewbyid(r.id.quantity); final imagebutton b_plus=(imagebutton)view.findviewbyid(r.id.ib_plus); final imagebutton b_minus=(imagebutton)view.findviewbyid(r.id.ib_minus); b_plus.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { count++; quant.settext(integer.tostring(count)); } }); b_minus.setonclicklistener(new view.onclicklistener() { @override public void onclick(view view) { count--; quant.settext(integer.tostring(count)); } }); return view; } };
remove related quant
, b_minus
, b_plus
other places in activity
Comments
Post a Comment