如何在Android中以编程方式从ListView中删除项目?

时间:2016-11-02 12:18:49

标签: android listview listadapter

我创建了一个ListView,其中我显示了数据库中的一些记录。当有人点击ListView中的项目时,我需要从ListView中完全删除该项目。如果我使用view.setVisibility(View.GONE);view.setVisibility(View.INVISIBLE);,文字就会消失。我需要删除整个项目。我正在使用ListAdapter接口而不是SimpleAdapter类。

我该怎么做?这是我的代码:

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

}

提前致谢!

3 个答案:

答案 0 :(得分:0)

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                productList.remove(position);
                listView.getAdapter().notifyDataSetChanged();
            }
        });

答案 1 :(得分:0)

您只需使用ArrayAdapter的remove()方法从列表中删除所需的项目。

可能的方法是:

Object toRemove = arrayAdapter.getItem([POSITION]);
arrayAdapter.remove(toRemove);

答案 2 :(得分:0)

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