How to dynamically remove items from ListView on a button click?
I am working on an app that requires removing items from a ListView
on a button event.
I tried to remove it from ArrayList
and create the whole new adapter and load the list again. As my list is huge, doing this will affect the performance of my app. So I was wondering if there is any other way by which I could remove an item from my list dynamically.
Edit: I tried what you said.
When I removed one item it worked perfectly, but as I increase the number of selected items it starts giving me IndexOutOfBoundException.
Here's my code:
public void onClick(View view)
{
SparseBooleanArray checkedPositions = new SparseBooleanArray();
checkedPositions.clear();
checkedPositions = lv.getCheckedItemPositions();
int size = checkedPositions.size();
if(size != 0)
{
for(int i = 0; i <= size; i++)
{
开发者_开发百科 if(checkedPositions.valueAt(i))
{
list.remove(notes.getItem(checkedPositions.keyAt(i)));
notes.notifyDataSetChanged();
}
}
}
else{}
}
Here, notes
is a reference to an object of SimpleAdapter.
Well you just remove the desired item from the list using the remove()
method of your ArrayAdapter
.
A possible way to do that would be:
Object toRemove = arrayAdapter.getItem([POSITION]);
arrayAdapter.remove(toRemove);
Another way would be to modify the ArrayList
and call notifyDataSetChanged()
on the ArrayAdapter
.
arrayList.remove([INDEX]);
arrayAdapter.notifyDataSetChanged();
This worked for me. Hope it helps someone. :)
SimpleAdapter adapter = (SimpleAdapter) getListAdapter();
this.resultsList.remove((int) info.id);
adapter.notifyDataSetChanged();
adapter.remove(arraylist.get(position));
adapter.notifyDataSetChanged();
or you can call again
setListAdapter(adapter)
Have a button on list and let it onclick feature in xml like to get postion first
public void OnClickButton(View V){
final int postion = listView.getPositionForView(V);
System.out.println("postion selected is : "+postion);
Delete(postion);
}
public void Delete(int position){
if (adapter.getCount() > 0) {
//Log.d("largest no is",""+largestitemno);
//deleting the latest added by subtracting one 1
comment = (GenrricStoring) adapter.getItem(position);
//Log.d("Deleting item is: ",""+comment);
dbconnection.deleteComment(comment);
List<GenrricStoring> values = dbconnection.getAllComments();
//updating the content on the screen
this.adapter = new UserItemAdapter(this, android.R.layout.simple_list_item_1, values);
listView.setAdapter(adapter);
}
else
{
int duration = Toast.LENGTH_SHORT;
//for showing nothing is left in the list
Toast toast = Toast.makeText(getApplicationContext(),"Db is empty", duration);
toast.setGravity(Gravity.CENTER, 0, 0);
toast.show();
}
}
private List<DataValue> datavalue=new ArrayList<Datavalue>;
@Override
public View getView(int position, View view, ViewGroup viewGroup) {
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
datavalue.remove(position);
notifyDataSetChanged();
}
});
}
}
List<String> entries;
private ArrayAdapter<String> categoryAdapter;
//Your list of entries {Example: <"category1","category2","category3">}
entries = new ArrayList<String>();
categoryAdapter = new ArrayAdapter<String>(ViewBeaconsActivity.this,
android.R.layout.simple_list_item_1, entries);
//Remove that specific category from the list
entries.remove(categoryName);
//Notify the adapter that your dataset has changed.
categoryAdapter.notifyDataSetChanged();
You can remove item from list view like this: or you can choose on your Button event which item have to be removed
public class Third extends ListActivity {
private ArrayAdapter<String> adapter;
private List<String> liste;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
liste = new ArrayList<String>();
Collections.addAll(liste, values);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, liste);
setListAdapter(adapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
liste.remove(position);
adapter.notifyDataSetChanged();
}
}
As for your last question, here's the problem illustrated with a simple example:
Let's say that your list contains 5 elements: list = [1, 2, 3, 4, 5]
and your list of items to remove (i.e. the indices) is indices_to_remove = [0, 2, 4]
. In the first iteration of the loop you remove the item at index 0, so your list becomes list = [2, 3, 4, 5]
. In the second iteration, you remove the item at index 2, so your list becomes list = [2, 3, 5]
(as you can see, this removes the wrong element). Finally, in the third iteration, you try to remove the element at index 4, but the list only contains three elements, so you get an out of bounds exception.
Now that you see what the problem is, hopefully you will be able to come up with a solution. Good luck!
<ImageView
android:id="@+id/btnDelete"
android:layout_width="35dp"
android:layout_height="match_parent"
android:layout_alignBottom="@+id/editTipo"
android:layout_alignParentRight="true"
android:background="@drawable/abc_ic_clear"
android:onClick="item_delete_handler"/>
And create Event item_delete_handler,
精彩评论