android - Handle multiple imgeview with single onclick (View view) method -


i create multiple imageviews programatically inflating 1 layout , set id imageview. these task perform in loop. want set on click listener on these programatically created imageviews. set onclick listener not working.

my code:-

mainlinearlayout.xml

 <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/linear_layout_breakfast" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="@color/colorlightwhiteshade" android:orientation="horizontal"> 

layout2.xml

 <?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="horizontal" android:gravity="center" android:weightsum="10" android:padding="@dimen/margin_small" android:layout_width="match_parent" android:layout_height="match_parent">   <textview     android:id="@+id/textview_breakfast_food_name_layout2"     android:layout_width="0dp"     android:layout_height="wrap_content"     android:layout_weight="9"     android:layout_alignparentleft="true"     android:gravity="left"     android:ellipsize="end"     android:singleline="true"     android:layout_marginleft="5dp"     android:textcolor="@android:color/black"     android:text="food name" />  <imageview     android:id="@+id/delete_button_layout2"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_weight="0.5"     android:layout_toleftof="@+id/edit_button_layout2"     android:gravity="center"     android:src="@drawable/btn_delete"/>  <imageview      android:id="@+id/edit_button_layout2"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_weight="0.5"     android:layout_alignparentright="true"     android:gravity="center"     android:src="@drawable/btn_edit"/>   </linearlayout> 

mainactivity.class

  linearlayout layout =(linearlayout) findviewbyid(r.id.linear_layout_breakfast);  (int = 0; < breakfastarraylistsize; i++) {         view view = layoutinflater.from(this).inflate(r.layout.layout2_breakfast, null);         textview textview_food_name = (textview) view.findviewbyid(r.id.textview_breakfast_food_name_layout2);         log.d("textview_food_name id", string.valueof(1 + (i * 3)));         textview_food_name.setid(1 + (i * 3));  textview_food_name.settext(foodbreakfastdiarydetailsarraylist.get(i).get("foodname"));         log.d("foodname", foodbreakfastdiarydetailsarraylist.get(i).get("foodname"));         layout.addview(view);          imageview_delete_breakfast = (imageview) view.findviewbyid(r.id.delete_button_layout2);         imageview_delete_breakfast.setid(2 + (i * 3));         imageview_delete_breakfast.setonclicklistener(this);         log.d("imageview_delete_breakfast id", string.valueof(diaryscreenactivity.this.imageview_delete_breakfast.getid()));      layout.setorientation(linearlayout.vertical);       string foodid_breakfast = "";         foodid_breakfast = foodbreakfastdiarydetailsarraylist.get(i).get("foodid");         int imageviewdeletepos = getindexofvalue(foodid_breakfast, foodbreakfastdiarydetailsarraylist);         log.d("imageviewdeletepos", string.valueof(imageviewdeletepos));         imageview imageview_edit = (imageview) view.findviewbyid(r.id.edit_button_layout2);         imageview_edit.setid(3 + (i * 3));         imageview_edit.setonclicklistener(this); } 

onclicklistener

@override public void onclick(view v) { if (v.getid() == diaryscreenactivity.this.imageview_delete_breakfast.getid()) {  log.d("imageview_delete_breakfast id", string.valueof(diaryscreenactivity.this.imageview_delete_breakfast.getid()));         new alertdialog.builder(this)                 .settitle("delete food")                 .setmessage("are sure want delete food?")                 .setpositivebutton(android.r.string.yes, new dialoginterface.onclicklistener() {                     public void onclick(dialoginterface dialog, int which) {                         // continue delete                          // delete food item                         databasehelper databasehelper = databasehelper.getinstance(getapplicationcontext());                         try {                             databasehelper.opendatabasec("m");                              (int = 0; < foodbreakfastdiarydetailsarraylist.size(); i++) {                                  string foodid_breakfast = foodbreakfastdiarydetailsarraylist.get(i).get("foodid");                                 log.d("foodid_breakfast", foodid_breakfast);                                 int imageviewdeletepos = getindexofvalue(foodid_breakfast, foodbreakfastdiarydetailsarraylist);                                 log.d("imageviewdeletepos", string.valueof(imageviewdeletepos));                             }                              databasehelper.closec("m");                          } catch (sqlexception e) {                             // todo auto-generated catch block                             e.printstacktrace();                         }                      }                 })                 .setnegativebutton(android.r.string.no, new dialoginterface.onclicklistener() {                     public void onclick(dialoginterface dialog, int which) {                         // nothing                     }                 })                 .seticon(android.r.drawable.ic_dialog_alert)                 .show();    } } 

add onclick inside loop. :

imageview_delete_breakfast.setonclicklistener(new view.onclicklistener() {                 @override                 public void onclick(view v) {                     // want individual click                 }             }); 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

javascript - Get parameter of GET request -

javascript - Twitter Bootstrap - how to add some more margin between tooltip popup and element -