0
maxIndex = alItemListDetails.size();
linItemTags = (LinearLayout) findViewById(R.id.linItemTags);
final ImageView image = new ImageView(this);
for (int i = 0; i < maxIndex; i++) {
image.setLayoutParams(new android.view.ViewGroup.LayoutParams(80, 60));
image.setMaxHeight(20);
image.setMaxWidth(20);
image.setTag(i);
image.setImageResource(R.drawable.box);
// Adds the view to the layout
linItemTags.addView(image);
}
image.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Common.displayToast(ItemDetailsActivity.this, "" + image.getTag());
}
});
從上面的代碼中,你可以看到,我已動態生成的ImageView並試圖處理單擊ImageView的事件。
但是,它給我的錯誤如下:
Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
你想創建圖像的列表? –
@vipinagrahari是 –
使用RecyclerView和ViewHolder會是一個更好的選擇。 –