Alex M. Alex M. - 1 month ago 14
Android Question

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

I have created a ListView in which i display some records from a database. When someone clicks an item from the ListView, i need that item to be removed completely from the ListView. If i use

view.setVisibility(View.GONE);
or
view.setVisibility(View.INVISIBLE);
ony the text disappears. I need the entire item to be removed. I am using ListAdapter interface instead of SimpleAdapter class.

How can i do that? Here is my code:

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};

ListAdapter 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) {
view.setVisibility(View.GONE);
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
}


}

Thanks in advance!

Answer
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();
    }
}
Comments