0

So I am working on a list view, and I want to implement a delete button. But for some reason, whenever I press the delete button on any list item whatsoever, the last item in the list gets deleted automatically. I tried out almost everything, but I am unable to understand how I can know the index of the item where the button was clicked so I can easily just delete that particular item.

This is the code :

// 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();

    }


}
Aadhitya
  • 83
  • 11

2 Answers2

1

ivDelete.setOnClickListener(new View.OnClickListener() {

Change that to:

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

public void onClick(View v) {

Change to:

public void onClick(View v) {
// int position = v.getTag();
int position = (Integer)v.getTag();
blackapps
  • 8,011
  • 2
  • 11
  • 25
  • I wasn't able to do it just as you told me, but I implemented the set and get tag thing. However, it won't build. Says it failed due to one error... Also, when I get the tag, it is of type object, not integer, so I can't directly equate that to int position as you did... EDIT : Some way or the other I tried to implement what you did properly, using parse Int , however still the same issue.. the last item gets deleted – Aadhitya Apr 14 '21 at 04:39
  • I knew that you had to fiddle around a bit with that tag thing before it works. It will work for you too. Sorry i have no code at hand now. Dont give up. It has been done before. Its pretty standard. – blackapps Apr 14 '21 at 05:57
  • Okay... but if you get the time, please try and help... I have been at this for almost 2 days now lol – Aadhitya Apr 14 '21 at 06:01
  • Here its done with a mytag class: https://stackoverflow.com/questions/23600599/how-to-use-settag-and-gettag-with-custom-adapter. But it can be done much simpler just by casting if its for one integer only. – blackapps Apr 14 '21 at 06:03
  • 1
    Its as simple as `int position = (Integer)v.getTag();`. See updated code. Just casting will do it. Forget the link – blackapps Apr 14 '21 at 09:05
0

Try to use below code :

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

            values .remove(position);
            notifyDataSetChanged();

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

        }
    });
NRUSINGHA MOHARANA
  • 1,489
  • 8
  • 13
  • I tried that, but I am getting this error :java.lang.IndexOutOfBoundsException: Index: 2, Size: 2 ( I had to press the delete button twice. On the first press, no response, but on the 2nd press, the app crashed with the above error message) – Aadhitya Apr 13 '21 at 14:19