android onClickListener with a bug -
android onClickListener with a bug -
i seek to implement onitemclicklistener , attached code , xml files . understanding when click textview , should homecoming me unique id ; here returning 0 . not sure why , in advance solutions .
my main activity :
public class mainactivity extends listactivity implements onitemclicklistener { public static final string number = "number" ; public static final string email ="email" ; public static final string name ="name"; arraylist<hashmap<string, string>> userlist = new arraylist<hashmap<string,string>>(); @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); (int i=0 ;i <=5;i++){ hashmap<string, string> map = new hashmap<string, string>(); map.put(number, i+""); map.put(email, i+"email"); map.put(name,i+"name"); userlist.add(map); } settextview(userlist); final listview lv = getlistview(); lv.settextfilterenabled(false); lv.setonitemclicklistener(this); } public void settextview(arraylist<hashmap<string, string>> userlist) { listadapter adapter = new simpleadapter( mainactivity.this, userlist, r.layout.list_item, new string[] {number , email ,name }, new int[] { r.id.username, r.id.ventdesc ,r.id.ventusername}); // updating listview setlistadapter(adapter); } @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { // todo auto-generated method stub string numberpressed = ((textview)findviewbyid(r.id.username)).gettext().tostring(); toast.maketext(mainactivity.this, "click: " + numberpressed, toast.length_short).show(); log.d("selected ",numberpressed); } }
main xml
<?xml version="1.0" encoding="utf-8"?> <textview xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:padding="10dp" android:textsize="20sp" > </textview>
list xml :
<?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:id="@+id/username" android:layout_width="fill_parent" android:layout_height="wrap_content" /> <!-- name label --> <textview android:id="@+id/ventusername" android:layout_width="match_parent" android:layout_height="wrap_content" /> <textview android:id="@+id/ventdesc" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingleft="6dip" android:paddingtop="6dip" android:textsize="17dip" android:textstyle="bold" /> </linearlayout>
ps: did seek debug code , nil seems helping me
change string numberpressed = ((textview)findviewbyid(r.id.username)).gettext().tostring(); string numberpressed = ((textview)view.findviewbyid(r.id.username)).gettext().tostring();
android android-listview onitemclicklistener
Comments
Post a Comment