How to remove an item from an ListView programmatically in Android?
<p>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 <code>view.setVisibility(View.GONE);</code> or <code>view.setVisibility(View.INVISIBLE);</code> ony the text disappears. I need the entire item to be removed. I am using ListAdapter interface instead of SimpleAdapter class.</p>

<p>How can i do that? Here is my code:</p>

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

        for (int i = 0; i &lt; jsonArray.length(); i++) {
            JSONObject jsonObject = jsonArray.getJSONObject(i);
            final String productId = jsonObject.getString("id");
            final String productName = jsonObject.getString("productName");
            HashMap&lt;String, String&gt; product = new HashMap&lt;&gt;();
            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&lt;?&gt; parent, View view, int position, long id) {
                    view.setVisibility(View.GONE);
                }
            });
        }
    } catch (JSONException e) {
        e.printStackTrace();
    }
}
</code></pre>

<p>}</p>

<p>Thanks in advance!</p>