【问题标题】:How to know the index of the item of the list, on which the button was clicked如何知道单击按钮的列表项的索引
【发布时间】:2021-07-08 13:46:22
【问题描述】:

所以我正在处理一个列表视图,我想实现一个删除按钮。但是由于某种原因,每当我按下任何列表项上的删除按钮时,列表中的最后一项都会自动删除。我几乎尝试了所有内容,但我无法理解如何知道单击按钮的项目的索引,因此我可以轻松删除该特定项目。

这是代码:

// full subtask adapter code

package com.example.taskmasterv3;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.ArrayList;

public class SubtaskAdapter extends ArrayAdapter<subtask> {


    private final Context context;
    private ArrayList<subtask> values;


    public SubtaskAdapter(Context context, ArrayList<subtask> list) {

        //since your are using custom view,pass zero and inflate the custom view by overriding getview

        super(context, 0 , list);
        this.context = context;
        this.values = list;
    }



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

        //check if its null, if so inflate it, else simply reuse it
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.subtask_item, parent, false);
        }



        //use convertView to refer the childviews to populate it with data
        TextView tvSubtaskName = convertView.findViewById(R.id.tvSubtaskName);
        ImageView ivPri = convertView.findViewById(R.id.ivPri);
        ImageView ivTime = convertView.findViewById(R.id.ivTime);
        ImageView ivDelete = convertView.findViewById(R.id.ivDelete);

        tvSubtaskName.setText(values.get(position).getSubtaskName());

        if (values.get(position).isPriHigh()) {
            ivPri.setImageResource(R.drawable.priority_high);
        } else if (values.get(position).isPriMed()) {
            ivPri.setImageResource(R.drawable.priority_med);
        } else if (values.get(position).isPriLow()) {
            ivPri.setImageResource(R.drawable.priority_low);
        }

        if (values.get(position).isTimeMore()) {
            ivTime.setImageResource(R.drawable.time_symbol_more);
        } else if (values.get(position).isTimeMed()) {
            ivTime.setImageResource(R.drawable.time_symbol_med);
        } else if (values.get(position).isTimeLess()) {
            ivTime.setImageResource(R.drawable.time_symbol_less);
        }




        // Delete button for subtasks (NOT WORKING)

        ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                
             SubtaskAdapter.this.remove(SubtaskAdapter.this.getItem(position));
             notifyDataSetChanged();



            }
        });

        //return the view you inflated
        return convertView;
    }




    //to keep adding the new subtasks try the following
    public void addANewSubTask(subtask newSubTask){
        ArrayList<subtask> newvalues = new ArrayList<>(this.values);
        newvalues.add(newSubTask);
        this.values = newvalues;
        notifyDataSetChanged();

    }


}

【问题讨论】:

    标签: java android listview


    【解决方案1】:

    ivDelete.setOnClickListener(new View.OnClickListener() {

    将其更改为:

    ivDelete.setOnClickListener(new View.OnClickListener() {
    ivDelete.setTag(position);
    

    public void onClick(View v) {

    改为:

    public void onClick(View v) {
    // int position = v.getTag();
    int position = (Integer)v.getTag();
    

    【讨论】:

    • 我不能像你告诉我的那样做,但我实现了 set and get tag 的东西。但是,它不会构建。说它由于一个错误而失败......另外,当我得到标签时,它是对象类型,而不是整数,所以我不能像你那样直接将它等同于 int 位置......编辑:某种方式或其他我尝试使用 parse Int 来实现您正确执行的操作,但仍然是同样的问题.. 最后一项被删除
    • 我知道在它起作用之前你必须对那个标签的东西进行一些摆弄。它也对你有用。抱歉,我现在手头没有代码。不要放弃。以前已经做过了。它非常标准。
    • 好的...但是如果您有时间,请尝试帮助...我已经在这里待了将近 2 天了,哈哈
    • 这里使用了一个 mytag 类:stackoverflow.com/questions/23600599/…。但是如果它只用于一个整数,它就可以简单得多。
    • 就像int position = (Integer)v.getTag(); 一样简单。请参阅更新的代码。只需铸造就可以了。忘记链接
    【解决方案2】:

    尝试使用以下代码:

            ivDelete.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
    
                values .remove(position);
                notifyDataSetChanged();
    
             //SubtaskAdapter.this.remove(SubtaskAdapter.this.getItem(position));
             //notifyDataSetChanged();
    
            }
        });
    

    【讨论】:

    • 我试过了,但我收到了这个错误:java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 (我必须按两次删除按钮。第一次按下时,没有响应,但在第二次按下时,应用程序因上述错误消息而崩溃)
    猜你喜欢
    • 1970-01-01
    • 2014-08-04
    • 1970-01-01
    • 2011-03-17
    • 1970-01-01
    • 2023-01-25
    • 1970-01-01
    • 1970-01-01
    • 2020-04-23
    相关资源
    最近更新 更多