Home > Software engineering >  Deleting the clicked item in the listview Android
Deleting the clicked item in the listview Android

Time:12-23

My code java. I'm getting data from the intent as an array. I am using arraylist,listview and custom adapter. I can show the incoming data using listview. I want the item I clicked to be deleted. The name of my delete button "deleteshop" in customadapter. How can I do that ?

My code;

        final ListView list = findViewById(R.id.list);

        final ArrayList<SubjectData> arrayList = new ArrayList<SubjectData>();

        final String[] cartList = getIntent().getStringArrayExtra("saleData");

        arrayList.add(new SubjectData("", ""));

        final int cartListLength = cartList.length;
        int counter = 0;
        String lastItem = "";


        for (String e : cartList) {
            counter  = 1;
            if (e == "" || e == null) {
                lastItem = cartList[counter-3];
                break;
            }

            String productPhoto = "";

            switch (e) {
                case "1 PC GREEN COLA x 10.00 TL":
                    productPhoto = "cc";
                    break;

                default:
                    productPhoto = "";
                    break;            }

          
        arrayList.add(new SubjectData(e,  productPhoto));;
        }
        arrayList.remove(arrayList.size()-1);
        arrayList.remove(arrayList.size()-1);
        arrayList.add(new SubjectData("*** GRAND TOTAL TL:"   lastItem   " ***",  "arrowgreen"));
        arrayList.add(new SubjectData("",  ""));
        arrayList.add(new SubjectData("",  ""));


        final CustomAdapter customAdapter = new CustomAdapter(this, arrayList);
        list.setAdapter(customAdapter);



SubjectData model class:


    String SubjectName;
    String Image;

    public SubjectData(String subjectName, String image) {
        this.SubjectName = subjectName;
        this.Image = image;
    }



Customadapter;


class CustomAdapter implements ListAdapter {
    ArrayList<SubjectData> arrayList;
    Context context;
    public CustomAdapter(Context context, ArrayList<SubjectData> arrayList) {
        this.arrayList=arrayList;
        this.context=context;
    }

    @Override
    public boolean areAllItemsEnabled() {
        return false;
    }

    @Override
    public boolean isEnabled(int position) {
        return true;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public void unregisterDataSetObserver(DataSetObserver observer) {

    }

    @Override
    public int getCount() {
        return arrayList.size();
    }

    @Override
    public Object getItem(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final SubjectData subjectData=arrayList.get(position);
        if(convertView==null){
            LayoutInflater layoutInflater = LayoutInflater.from(context);
            convertView=layoutInflater.inflate(R.layout.list_row, null);
        
            TextView tittle=convertView.findViewById(R.id.title);
            ImageView imag=convertView.findViewById(R.id.list_image);
            ImageView 
            deleteshop=convertView.findViewById(R.id.deleteshop);

            deleteshop.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                    Toast.makeText(context, "Deneme", 
            Toast.LENGTH_SHORT).show();
                }
            });
             
            tittle.setText(subjectData.SubjectName);
            Resources resources = context.getResources();
            final int resourceId = resources.getIdentifier(subjectData.Image, "drawable",
                    context.getPackageName());

           

            imag.setImageResource(resourceId);

          

        }
        return convertView;
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public int getViewTypeCount() {
        return arrayList.size();
    }


    @Override
    public boolean isEmpty() {
        return false;
    }

listview design;


<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"

    android:padding="5dip">

    <LinearLayout
        android:id="@ id/thumbnail"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_marginRight="5dip"
        android:padding="3dip">

        <ImageView
            android:id="@ id/list_image"
            android:layout_width="50dip"
            android:layout_height="50dip" />

    </LinearLayout>

    <TextView
        android:id="@ id/title"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_alignTop="@ id/thumbnail"
        android:layout_toRightOf="@ id/thumbnail"
        android:gravity="center"
        android:textColor="#040404"
        android:textSize="15dip"
        android:textStyle="bold"
        android:typeface="sans" />


    <ImageView
        android:layout_gravity="center"
        android:id="@ id/deleteshop"
        android:src="@drawable/negative"
        android:layout_width="25dp"
        android:layout_height="25dp" ></ImageView>



</LinearLayout>

CodePudding user response:

If you want to delete clicked item from your list view then you can use this code .I tested and its deleting smoothly , after deleting its updating list also.

    CustomAdapter customAdapter = new CustomAdapter(this,R.layout.list_row, arrayList);
    list.setAdapter(customAdapter);
    // after setting adapter put this code 
    // and update your ui again using set adapter
    Log.e("Custom","" arrayList.size());
    list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
            arrayList.remove(i);
            list.setAdapter(customAdapter);
        }
    });

This is not a best solution but ulternative , suppose if you want to update your adapter on click of deletebutton first create one interface , here is sample

public interface MyInterface{
public void deleteItem(int pos);
}

Now in your main activity use like this MainActivity extends AppCompatActivity implements MyInterface and place your interface method public void deleteItem(int pos){ arrayList.remove(pos); list.setAdapter(customAdapter); } After that call this method from your adapter like below

 deleteshop.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Toast.makeText(context, "Delete",
                        Toast.LENGTH_SHORT).show();
                 //Actually change your list of items here
                if (context instanceof SampleList) {
                    ((MainActivity)context).deleteItem(position);
                }
            }
        });
  • Related