我通过扩展一个布局并为该图像视图设置 id 以编程方式创建多个图像视图。我在 for 循环中执行的所有这些任务。我想在这些以编程方式创建的图像视图上设置点击监听器。我设置了 onclick 侦听器,但它不起作用。
我的代码:-
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">
布局2.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);
}
点击监听器
@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();
}
}