Tuesday, 15 May 2012

android - get clicked item from listview -



android - get clicked item from listview -

i've got problem. before modification in xml file, listview able work perfectly. now, after modifications in xml, not working properly. listview custom. so, i've created separate xml render every single row in listview.

my single row.xml code:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:id="@+id/relativelayoutsinglerowmanageaddictions" android:layout_height="40dp" android:background="@drawable/box_midbg" > <textview android:id="@+id/textviewsinglerowmanageaddictions" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_centervertical="true" android:layout_marginleft="17dp" android:text="textview" android:textsize="20dp" android:textcolor="#ffffff" /> <imageview android:id="@+id/imageviewsinglerowmanageaddictions" android:layout_width="20dp" android:layout_height="20dp" android:layout_alignparentright="true" android:layout_centervertical="true" android:layout_marginright="18dp" android:src="@drawable/listing_arrow" /> </relativelayout>

my main.xml code listview resides:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/main_bg_edited" > <relativelayout android:id="@+id/relativelayout1" style="@style/top_bar_style" > <imageview android:id="@+id/imageviewmanageaddictionsback" android:layout_width="30dp" android:layout_height="30dp" android:layout_alignparentleft="true" android:layout_centervertical="true" android:src="@drawable/back_arrow" android:clickable="true" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" style="@style/header_style" android:text="manage addictions" /> <imageview android:id="@+id/imageviewmanageaddictionsadd" android:layout_width="25dp" android:layout_height="20dp" android:layout_marginright="3dp" android:layout_alignparentright="true" android:layout_centervertical="true" android:src="@drawable/plus_nav" android:clickable="true" /> </relativelayout> <listview android:id="@+id/listviewmanageaddictions" android:layout_width="290dp" android:layout_height="fill_parent" android:layout_below="@+id/relativelayout1" android:layout_centerhorizontal="true" android:layout_centerinparent="true" android:layout_margintop="12dp" android:divider="@android:color/transparent" android:dividerheight="2dp" /> </relativelayout>

and java code it:

package com.addictioncounterapp; import java.util.arraylist; import android.os.bundle; import android.view.view; import android.view.view.onclicklistener; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.arrayadapter; import android.widget.imageview; import android.widget.listview; import android.widget.textview; import android.widget.toast; import android.app.activity; import android.content.intent; import android.database.cursor; import android.database.sqlite.sqlitedatabase; public class manageaddictionlist extends activity { imageview iv_manage_addictions_back, iv_manage_addictions_add; listview listview; arraylist < string > arraylist_manage_addiction; arrayadapter < string > arrayadapter_manage_addiction; sqlitedatabase database; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_manage_addiction_list); loaddatabase(); iv_manage_addictions_back = (imageview) findviewbyid(r.id.imageviewmanageaddictionsback); iv_manage_addictions_back.setclickable(true); iv_manage_addictions_back.setonclicklistener(new onclicklistener() {@override public void onclick(view v) { startactivity(new intent(manageaddictionlist.this, settings.class)); } }); iv_manage_addictions_add = (imageview) findviewbyid(r.id.imageviewmanageaddictionsadd); iv_manage_addictions_add.setclickable(true); iv_manage_addictions_add.setonclicklistener(new onclicklistener() {@override public void onclick(view v) { intent intent = new intent(manageaddictionlist.this, mainactivity.class); intent.putextra("name", ""); intent.putextra("unit", ""); intent.putextra("attribute", ""); intent.putextra("limit", ""); intent.putextra("operation", "add addiction"); startactivity(intent); } }); arraylist_manage_addiction = new arraylist < string > (); managelist(); listview = (listview) findviewbyid(r.id.listviewmanageaddictions); if (arraylist_manage_addiction.isempty()) toast.maketext(getbasecontext(), "no addictions found manage. click on 'add' button create new addiction.", toast.length_short) .show(); else listview.setadapter(arrayadapter_manage_addiction); listview.setonitemclicklistener(new onitemclicklistener() {@override public void onitemclick(adapterview <? > arg0, view arg1, int arg2, long arg3) { string name = null, attribute = null, unit = null, limit = null; view parentview = (view) arg0.getparent(); name = ((textview) parentview.findviewbyid(r.id.textviewsinglerowmanageaddictions)) .gettext() + ""; toast.maketext(getbasecontext(), name, toast.length_short) .show(); int cat_id = 0; //--------fetching cat_id through name list-------- cursor cursor; cursor = database.query("category", new string[] { "cat_id" }, new string("cat_name=?"), new string[] { name }, null, null, null); if (cursor.getcount() > 0) { while (cursor.movetonext()) cat_id = cursor.getint(0); cursor.close(); } //--------fetching unit, attribute, limit through cat_id list-------- cursor = database.query("category_attribute", new string[] { "cat_attribute_name", "cat_attribute_unit", "cat_limit" }, new string("cat_id=?"), new string[] { cat_id + "" }, null, null, null); if (cursor.getcount() > 0) { while (cursor.movetonext()) { attribute = cursor.getstring(0); unit = cursor.getstring(1); limit = cursor.getstring(2); } cursor.close(); } intent intent = new intent(manageaddictionlist.this, mainactivity.class); intent.putextra("name", name); intent.putextra("unit", unit); intent.putextra("attribute", attribute); intent.putextra("limit", limit); intent.putextra("cat_id", cat_id); intent.putextra("operation", "edit addiction"); startactivity(intent); } }); } private void loaddatabase() { database = openorcreatedatabase("addictioncounter.db", sqlitedatabase.open_readonly, null); } private void managelist() { string[] columns = { "cat_name" }; cursor cursor; cursor = database.query("category", columns, null, null, null, null, null); if (cursor.getcount() > 0) { while (cursor.movetonext()) arraylist_manage_addiction.add(cursor.getstring(0)); cursor.close(); } arrayadapter_manage_addiction = new arrayadapter < string > (this, r.layout.single_row_manage_addictions, r.id.textviewsinglerowmanageaddictions, arraylist_manage_addiction); } }

the main bug behind that, when name using this:

view parentview = (view) arg0.getparent(); name = ((textview) parentview.findviewbyid(r.id.textviewsinglerowmanageaddictions)).gettext()+"";

the listview of record, gives name of first record only. example, if first row has textview named "gaming", when click on row, (for debugging purpose, used toast.maketext(...)) toasts "gaming" name every record in listview, though every record in listview unique. please help me out this.

lv.setonitemclicklistener(new onitemclicklistener() { public void onitemclick(adapterview<?> parent, view view, int position, long id) { // selected item string selected = ((textview) view.findviewbyid(r.id.your_textview_item_id)).gettext().tostring(); toast toast=toast.maketext(getapplicationcontext(), selected, toast.length_short); toast.show(); } });

android android-listview

No comments:

Post a Comment