java - Does not show accurate output for android apps -
i new android. create apps followed tutorial. create restaurant info page. when type name , address , types , should show details , instead of shows in emulator
com.example.lunchlist.restaurant@40ad8sd com.example.lunchlist.restaurant@40fhyu5
here xml code:
activity_lunch_list.xml
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:stretchcolumns="1" > <tablelayout android:id="@+id/details" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:stretchcolumns="1" > <tablerow > <textview android:text="@string/namee" /> <edittext android:id="@+id/name" android:ems="10" android:inputtype="text"></edittext> </tablerow> <tablerow> <textview android:text="@string/addresss" /> <edittext android:id="@+id/addr" android:inputtype="text"></edittext> </tablerow> <tablerow > <textview android:text="@string/types"/> <radiogroup android:id="@+id/type"> <radiobutton android:id="@+id/take_out" android:text="@string/take" /> <radiobutton android:id="@+id/sit_down" android:text="@string/sit" /> <radiobutton android:id="@+id/delivery" android:text="@string/deliveryy" /> </radiogroup> </tablerow> <button android:id="@+id/button1" android:text="@string/save" /> </tablelayout> <listview android:id="@+id/restaurants" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_above="@id/details" /> </relativelayout>
here java code:
lunchlist.java
package com.example.lunchlist; import java.util.list; import java.util.arraylist; import android.os.bundle; import android.app.activity; //import android.view.menu; import android.widget.arrayadapter; import android.widget.button; import android.widget.edittext; import android.widget.listview; import android.view.view; //import android.widget.edittext; //import android.widget.radiobutton; import android.widget.radiogroup; //import android.widget.toast; public class lunchlist extends activity { list<restaurant> model=new arraylist<restaurant>(); arrayadapter<restaurant> adapter=null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_lunch_list); button save=(button)findviewbyid(r.id.button1); save.setonclicklistener(onsave); listview list=(listview)findviewbyid(r.id.restaurants); adapter=new arrayadapter<restaurant>(this, android.r.layout.simple_list_item_1, model); list.setadapter(adapter); } private view.onclicklistener onsave=new view.onclicklistener() { public void onclick(view v) { restaurant r=new restaurant(); edittext name=(edittext)findviewbyid(r.id.name); edittext address=(edittext)findviewbyid(r.id.addr); r.setname(name.gettext().tostring()); r.setaddress(address.gettext().tostring()); radiogroup types=(radiogroup)findviewbyid(r.id.type); switch (types.getcheckedradiobuttonid()) { case r.id.sit_down: r.settype("sit_down"); break; case r.id.take_out: r.settype("take_out"); break; case r.id.delivery: r.settype("delivery"); break; } adapter.add(r); }; }; }
here restaurant.java page:
package com.example.lunchlist; public class restaurant { private string name=""; private string address=""; private string type=""; public string getname() { return(name); } public void setname(string name) { this.name=name; } public string getaddress() { return(address); } public void setaddress(string address) { this.address=address; } public string gettype() { return(type); } public void settype(string type) { this.type=type; } }
any appreciated
its problem adapter. passing restaurant
object adapter
should string
set text in textview
.
and item should not added adapter
instead must added arraylist
, must notify data change adapter calling adapter.notifydatasetchange()
i.e., lunchlist
class should follows
public class lunchlist extends activity { list<string> model = new arraylist<string>(); arrayadapter<string> adapter = null; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_lunch_list); button save = (button) findviewbyid(r.id.button1); save.setonclicklistener(onsave); listview list = (listview) findviewbyid(r.id.restaurants); adapter = new arrayadapter<string>(this, android.r.layout.simple_list_item_1, model); list.setadapter(adapter); } private view.onclicklistener onsave = new view.onclicklistener() { @override public void onclick(view v) { restaurant r = new restaurant(); edittext name = (edittext) findviewbyid(r.id.name); edittext address = (edittext) findviewbyid(r.id.addr); r.setname(name.gettext().tostring()); r.setaddress(address.gettext().tostring()); radiogroup types = (radiogroup) findviewbyid(r.id.type); switch ( types.getcheckedradiobuttonid() ) { case r.id.sit_down: r.settype("sit_down"); break; case r.id.take_out: r.settype("take_out"); break; case r.id.delivery: r.settype("delivery"); break; } model.add(r.gettype()); adapter.notifydatasetchanged(); }; }; }
note : if want display 3 (name, address , type) in listview, use custom listview
Comments
Post a Comment