简体   繁体   English

如何知道在哪个项目中单击了哪个按钮

[英]How to know which button in which item was clicked

I have a delete button in each item in a listview that gives the user a chance to delete an item they created. 我在列表视图的每个项目中都有一个删除按钮,使用户有机会删除他们创建的项目。 I need to know though which button was clicked in which item- how do i get the item that a user clicked a button in? 我需要知道在哪个项目中单击了哪个按钮-如何获得用户单击按钮的项目? (i use a custom adapter)?Thanks (我使用自定义适配器)?谢谢

not the cleanest method but you can set your button onclick listener in your customadapter getview method 不是最干净的方法,但是可以在customadapter getview方法中将按钮设置为onclick侦听器

  @Override
public View getView(int position, View convertView, ViewGroup parent) {

   ...

    Button btnDelete= (Button) convertView.findViewById(R.id.btnDelete);

       btnDelete.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //execute your codes here

        }
    });

}

Set a tag on the item. 在项目上设置标签。 You can use an identifier of the tag (even an iterated value) to identify it 您可以使用标签的标识符(甚至是迭代值)来识别它

You can use setTag(Object tag) to set the item as tag of the button when you create each cell of the listview. 创建列表视图的每个单元格时,可以使用setTag(Object tag)将该项目设置为按钮的标签。 And when the button got clicked use getTag () to get the object (you need to cast it to your item type). 当单击按钮时,请使用getTag ()获取对象(您需要将其getTag ()为项目类型)。

Then you can do whatever you want with your item. 然后,您可以对商品进行任何操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM