Search code examples
androidlistviewselecteditemdelete-row

Delete ListView row Item with button click


I have a simple listview with a delete button in each row

Now When I click delete button the selected listview row should be deleted.

This is my Listview:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
           android:layout_width="match_parent"
           android:layout_height="match_parent" >

 <ListView android:id="@+id/itemslistview"
           android:layout_height="match_parent"
           android:layout_width="match_parent"
           android:layout_marginTop="75dp"/>
</RelativeLayout>

ListViewRow:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="horizontal"
          android:layout_width="match_parent"
          android:layout_height="match_parent">

    <TextView
          android:id="@+id/textView"
          android:layout_width="wrap_content" 
          android:layout_height="wrap_content" 
          android:layout_weight="2"
          android:padding="10dp"
          android:textSize="16sp"
          android:ems="10" 
          android:textColor="#800080" />
    <ImageView
           android:id="@+id/btndelete"
           android:layout_width="wrap_content" 
           android:layout_height="wrap_content"
           android:layout_weight="1" 
           android:src="@drawable/delete"
           android:layout_toRightOf="@+id/textView"
           android:layout_marginTop="5dp"/>
</LinearLayout>

This is how I'm trying to delete:

lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,
                                                   int position, long id) {
        listrowposition= position;
        Button del =(Button)findViewById(R.id.btndelete);
        findViewById(R.id.deleteicon).setOnClickListener(
                                        new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                MyList.remove(listrowposition);
                adp.notifyDataSetChanged();
                Toast.makeText(getApplicationContext(),
                               "Deleted ListItem Number " + listrowposition,
                               Toast.LENGTH_LONG).show();
            }
        });
        Toast.makeText(getApplicationContext(),
                       "Click ListItem Number " + listrowposition,
                       Toast.LENGTH_LONG).show();

    }
});

Solution

  • try this in your code :

    lv.setOnItemClickListener(new OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
    listrowposition= position;
    Button del =(Button)view.findViewById(R.id.deleteicon);
    del.setOnClickListener(
        new View.OnClickListener() {
            @Override
            public void onClick(View arg0) {
                MyList.remove(listrowposition);
                adp.notifyDataSetChanged();
                Toast.makeText(getApplicationContext(),
                          "Deleted ListItem Number " + listrowposition, Toast.LENGTH_LONG)
                          .show();
            }
        });
    
    
    Toast.makeText(getApplicationContext(), "Click ListItem Number " + listrowposition, Toast.LENGTH_LONG)
    .show();
    
    }
    });