Reputation: 13250
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();
}
});
Upvotes: 0
Views: 5234
Reputation: 24848
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();
}
});
Upvotes: 1
Reputation: 15336
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);
}
});
}
});
}
Upvotes: 0
Reputation: 179
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.
Upvotes: 0
Reputation: 3627
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();
}
});
Upvotes: 5
Reputation: 10948
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
.
Upvotes: 1