0

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

    }
});

5 Answers 5

5

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

}
});
Sign up to request clarification or add additional context in comments.

Comments

1

You need to use the View in the onClick param :

Button del =(Button) view.findViewById(R.id.btndelete);

But beware, findViewById considered not performance wise because it takes a long time to operate. Better use a holder pattern and set the onClick inside your adapter.

4 Comments

If I use this the app is crahsing and not getting logcat error
@coder restart your eclipse, its impossible to crashed without error
I think I'm unable to get the button Id and do I need to add any focusable to detect the button ?
@coder i need to look at your logcat to be sure...the id should be detected. You can try to add android:focusable=true in your btndelete
1

Try this way,hope this will help you to solve your problem.

First define position as final in getView() parameter then try to remove list item from you list data holder listDetail using remove method and notify your adapter using notifyDataSetChanged method like :

holder.removeButton.setOnClickListener(new OnClickListener()
{ 
    @Override
    public void onClick(View v)
    {
      listDetail.remove(position);
      notifyDataSetChanged();
      Log.i("Delete Button Clicked",*************************************************");
      Toast.makeText(context, "Delete button Clicked",Toast.LENGTH_LONG).show();
    }
});

Comments

0

First to get click events of both Listview and Button, you need to set Focusable() and FocusableInTouchMode() attributes of Button to FALSE.

Now, In your custom adapter's getView() method you can get reference to your Button in list. In that setTag(position) for your button.

Set onClickListener to this Button, When you get onClick(View view) callback you can call getTag() method of view (It's the position of list item).

Finally, Using that position you can remove the element from your list and call notifyDataSetChanged() of adapter.

Comments

0

Delete with simple animation.

listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {

      @Override
      public void onItemClick(AdapterView<?> parent, final View view,
          int position, long id) {
        final String item = (String) parent.getItemAtPosition(position);
        view.animate().setDuration(2000).alpha(0)
            .withEndAction(new Runnable() {
              @Override
              public void run() {
                list.remove(item);
                adapter.notifyDataSetChanged();
                view.setAlpha(1);
              }
            });
      }

    });
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.