我正在显示一个列表。在onCreate我有代码
listView = (ListView) findViewById(R.id.list_view);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Log.v("list clicked", "list item no: " + position);
Toast.makeText(DisplayImagesActivity.this, "Btn delete position: " + position, Toast.LENGTH_LONG).show();
Intent intentImageDetails = new Intent(DisplayImagesActivity.this, ImageDetailActivity.class);
intentImageDetails.putExtra("image", imageBitmap);
intentImageDetails.putExtra("name", listImageInfo.get(position).getImage_name());
intentImageDetails.putExtra("amount", listImageInfo.get(position).getAmount());
intentImageDetails.putExtra("desc", listImageInfo.get(position).getImage_description());
intentImageDetails.putExtra("id", listImageInfo.get(position).getImage_id());
intentImageDetails.putExtra("date", listImageInfo.get(position).getDate_created());
startActivity(intentImageDetails);
}
});
和适配器代码是
public class MyImageListAdapter extends BaseAdapter {
private LayoutInflater inflater = null;
public MyImageListAdapter() {
inflater = (LayoutInflater) DisplayImagesActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return listImageInfo.size();
}
@Override
public Object getItem(int position) {
return listImageInfo.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if (convertView == null) {
vi = inflater.inflate(R.layout.list_row, null);
}
TextView tvName = (TextView) vi.findViewById(R.id.tv_name);
TextView tvTime = (TextView) vi.findViewById(R.id.tv_time);
ImageView image = (ImageView) vi.findViewById(R.id.iv_image);
final Button btnDelete = (Button) vi.findViewById(R.id.btn_delete);
image.setImageDrawable(listImageInfo.get(position).getImage());
tvName.setText("Name: " + listImageInfo.get(position).getImage_name());
tvTime.setText("Date: " + listImageInfo.get(position).getDate_created());
// btnDelete.setOnClickListener(new OnClickListener() {
// @Override
// public void onClick(View v) {
// final int position = listView.getPositionForView((View)
// v.getParent());
// Log.v("delete btn clicked", "delete btn no: " + position);
// Toast.makeText(DisplayImagesActivity.this,
// "Btn delete position: " + position, Toast.LENGTH_LONG).show();
// }
// });
// listImageInfo.get(position).getImage().getClass();
// image.setImageBitmap(bm)
return vi;
}
}
但是没有获得被叫列表视图点击代码,我正在做什么错误?
答案 0 :(得分:8)
在xml文件集中btn_delete
焦点为false,我认为当你单击List时按钮会被聚焦,所以你必须设置
android:focusable="false"
或
btnDelete.setFocusable(false);