对 ListView 图像的 onClick 侦听器 - Android

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10715201/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 04:43:49  来源:igfitidea点击:

onClick listener to a ListView Image - Android

androidlistviewonclicklistener

提问by Fahid Mohammad

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

ListView在右手边有一个图像。我想onClick通过单击ListView. 请参阅图像以供参考。

enter image description here

在此处输入图片说明

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

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

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

忘了提,通过点击实际ListView会触发一个新的活动,所以我需要维护这两个模式。

 listView.setOnItemClickListener(new OnItemClickListener() {

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

    });   

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.

上面的代码通过单击任何列表元素来执行删除操作eventsData.remove(id);是执行此任务的数据库助手。就像我现在说的,我需要一种方法来通过单击图像而不是整个列表元素来执行相同的过程,我希望列表元素稍后执行一些其他操作。

I hope now I'm clear a bit.

我希望现在我清楚了一点。



The Solution:

解决方案:

If anybody come across the same sort of situation then here is the complete code for the adapter.

如果有人遇到同样的情况,那么这里是适配器的完整代码。

 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.

我知道编码有点蹩脚,所以请耐心等待,我只想展示实际过程就是这样。

It's working for me :)

它对我有用:)

采纳答案by C. Leung

You should do it In your Adapternot in Activity.

你应该在你的not in 中做到这一点AdapterActivity

yourImg.setOnClickListener(new OnClickListener() {

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

回答by Satheesh

Create one interface for communicating from adapter to activity

创建一个接口用于从适配器到活动的通信

public interface ListenerActivity        
{    
public void Remove();    
}

In Your Adapter Class:

在您的适配器类中:

class CustomAdapter extends ArrayAdapter<String> {

ListenerActivity listener;

CustomAdapter(YourActivity obj) {
  super(Activity.this, R.layout.row, R.id.label, items);

  listener=obj;
}
 public View getView(final int position, View convertView,
                ViewGroup parent) {
ImageId.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {
        listener.Remove();
    }
});
}

In Your Activity:

在您的活动中:

class MyActivity extends Activity implements ListenerActivity
{
 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
}
 @Override
    public void Remove() {

//Do your remove functionality here
    }         
}

回答by ghost rider3

Adding to the last answer and reering to this answer, I added some code to refresh the view after deleting an item or others actions:

添加到最后一个答案并重新考虑这个答案,我添加了一些代码以在删除项目或其他操作后刷新视图:

//in your adapter you have to get list of items:

 public CartListAdapter(Context context, int resourceId,
        List<CartItemModel> items) {
    super(context, resourceId, items);
    this.context = context;
    this.items= items;
}

//after that you have to set a listner on the imageView like this:

public View getView(int position, View convertView, ViewGroup parent) {
   holder.imageViewDelete.setOnClickListener(new imageViewClickListener(position));
   // some other lines code ...
   // here we get the item to remove
    rowItem= getItem(position);
    }

//and here the definition of the listner:

private class imageViewClickListene implements OnClickListener {
   int position;
    public imageViewClickListene( int pos)
        {
            this.position = pos;
        }

    public void onClick(View v) {

    // here we  remove the selected item    
        items.remove(rowItem);
            // here we refresh the adapter
        CartListAdapter.this.notifyDataSetChanged();

}
}

It works for me, enjoy !!

它对我有用,享受!