的onClick监听到ListView图片 - 机器人机器人、图片、onClick、ListView

2023-09-06 13:24:28 作者:死性不改

我有一个的ListView 与图像在右边。我想通过点击的ListView 图像执行的onClick 监听事件。请看到的图像,以供参考。

I have a ListView with an image over the right hand side. and I wanted to perform a onClick listener event by clicking the image on the ListView. Please see the image for reference.

我知道基本的的OnClick 监听器实现,但是这似乎是有点棘手我:P

I know basic OnClick listener Implementations, but this seems to be a little tricky to me :P

忘了提,通过单击实际的ListView 将shootup一个新的活动,所以我需要保持这两个模式。

Forgot to mention, by clicking the actual ListView will shootup a new activity, so I need to maintain both the schemas.

 listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            eventsData.remove(id);
            cursor.requery(); 
        }

    });   

在code。通过单击任何列表元素 eventsData.remove(ID)在上述执行删除; 是一个数据库,帮助执行这一任务。就像我现在说我需要一个方法,通过单击图像来执行同一过程伸出,而不是整个列表元素,我想列表元素做一些其他的操作以后。

The code above perform a deletion by clicking on any list element eventsData.remove(id); is a database helper for executing this task. like I said now I need a method to perform this same process juts by clicking the image, not the entire list element, I want the list element to do some other action later on.

我现在希望我清楚一点。

I hope now I'm clear a bit.

解决方案:

如果有人遇到同样的排序情况,然后下面是完整的code适配器。的

 class CustomAdapter extends ArrayAdapter<String> {
 CustomAdapter() {
  super(Activity.this, R.layout.row, R.id.label, items);
}

public View getView(final int position, View convertView,
                    ViewGroup parent) {
  View row=super.getView(position, convertView, parent);
  deleteImg=(ImageView)row.findViewById(R.id.icon);

  deleteImg.setImageResource(R.drawable.delete);      
  deleteImg.setOnClickListener(new OnClickListener() {
      String s = items[position];
    @Override
    public void onClick(View v) {
        Toast.makeText(context, s, Toast.LENGTH_SHORT).show();          
    }
});

  return(row);
}

}

我知道这个编码是有点蹩脚所以忍耐一下,我只是想显示实际的过程就是这样。

I know the coding is a bit crappy so bear with me, I just want to show the actual process that's it.

它的工作对我来说:)

推荐答案

您应该这样做在你的适配器的不是活动。的

You should do it In your Adapter not in Activity.

yourImg.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        // things you want to do 
    }
});