Remove Listview Items in Android

How to Delete Item From Listview on Android

I assume you want to remove an item on the list when it gets button press - In the function where the button press gets called, get item position and remove the item like so:

this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
//Do some
return true;
}

});

kissiler.remove(position)

and then call

adapter.notifyDataSetChanged()

That will solve your problem.

Full code:

this.getListView().setLongClickable(true);
this.getListView().setOnItemLongClickListener(new OnItemLongClickListener() {
public boolean onItemLongClick(AdapterView<?> parent, View v, int position, long id) {
//Do some
kissiler.remove(position);
adapter.notifyDataSetChanged();
return true;
}});

EDIT: I made a terrible mistake and used id instead of position - obviously you'll have to use the position variable in the functions parameters as it gives an integer where the item is located in your listview.

How to remove an item from an ListView programmatically in Android?


JsonObjectRequest jsonObjectRequest = new JsonObjectRequest(Request.Method.GET, showProductsUrl, new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
try {
JSONArray jsonArray = response.getJSONArray("products");

for (int i = 0; i < jsonArray.length(); i++) {
JSONObject jsonObject = jsonArray.getJSONObject(i);
final String productId = jsonObject.getString("id");
final String productName = jsonObject.getString("productName");
HashMap<String, String> product = new HashMap<>();
product.put(ID, productId);
product.put(PRODUCT_NAME, productName);
productList.add(product);

String[] from = {ID, PRODUCT_NAME};
int[] to = {R.id.id, R.id.productName};

//NOTE: See I changed it to SimpleAdapter so you can call the notifydatasetchanged method after removing the element from the list.
SimpleAdapter adapter = new SimpleAdapter(getApplicationContext(), productList, R.layout.list_products_to_buy, from, to);

ListView listView = (ListView) findViewById(R.id.listView);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
productList.remove(position);
adapter.notifyDataSetChanged();
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}

How to dynamically remove items from ListView on a button click?

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

How to remove listview all items

Call setListAdapter() again. This time with an empty ArrayList.

Want to remove the selected item from Listview

Try to implement the onitemclickListener and get the item id and delete the item clicked in the arrayadapter and next call adapter.notifyDataSetChanged();

Android List View remove all items

You can do myListView.setAdapter(null).



Related Topics



Leave a reply



Submit