I create multiple imageviews programatically by inflating one layout and set id to that imageview. all these task i perform in for loop. i want to set on click listener on these programatically created imageviews. i set onclick listener but its 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);
for (int i = 0; i < 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 you sure you want to delete this food?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
// delete food item
DataBaseHelper dataBaseHelper = DataBaseHelper.getInstance(getApplicationContext());
try {
dataBaseHelper.openDataBaseC("M");
for (int i = 0; i < 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) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
}
add onClick inside loop. like :
imageview_delete_breakfast.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// do what you want for individual click
}
});