我如何从列表视图中删除listitem?

时间:2010-08-25 09:29:00

标签: android eclipse listitem

不会从数据库中检索Listitem。它是从另一个类传递的。

2 个答案:

答案 0 :(得分:3)

您没有“从列表视图中删除列表项目”。您可以修改支持ListAdapter的{​​{1}}所拥有的数据。如果适配器是ListView,请在ArrayAdapter上致电remove()。如果适配器是ArrayAdapter,请从数据库中删除该项目,然后CursorAdapter requery()。等等。

答案 1 :(得分:0)

如果您使用带有ArrayList的SimpleAdapter,那就更好了。然后通过删除要删除的内容来更新列表。只需调用adapter.notifyDataSetChanged()。赞:

static final ArrayList<HashMap<String,String>> list =new ArrayList<HashMap<String,String>>();


public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.newentrypagelayout);
        //create base adapter for listview
        adapter= new SimpleAdapter(
                this,
                list,
                R.layout.custom_list_row,
                new String[] {"pen","price","color"},
                new int[] {R.id.text1,R.id.text2, R.id.text3}

                );
            populateList();
            setListAdapter(adapter);


}

public void populateList()
{      
sHashMap<String,String> temp = new HashMap<String,String>();
            temp.put("pen","MONT Blanc");
            temp.put("price", "200.00$");
            temp.put("color", "Silver, Grey, Black");
            list.add(temp);
            HashMap<String,String> temp1 = new HashMap<String,String>();
            temp1.put("pen","Gucci");
            temp1.put("price", "300.00$");
            temp1.put("color", "Gold, Red");
            list.add(temp1);
            HashMap<String,String> temp2 = new HashMap<String,String>();
            temp2.put("pen","Parker");
            temp2.put("price", "400.00$");
            temp2.put("color", "Gold, Blue");
            list.add(temp2);
            HashMap<String,String> temp3 = new HashMap<String,String>();
            temp3.put("pen","Sailor");
            temp3.put("price", "500.00$");
            temp3.put("color", "Silver");
            list.add(temp3);
            HashMap<String,String> temp4 = new HashMap<String,String>();
            temp4.put("pen","Porsche Design");
            temp4.put("price", "600.00$");
            temp4.put("color", "Silver, Grey, Red");
            list.add(temp4);

}
now if you need to delete an item.Get the selected index(the item that has been selected) remove it from the array list & call the method I told before.Like:

public void itemDeleteButtonClicked(View v)
     {
            int index=itemsListView.getSelectedItemPosition();
            list.remove(index);
            adapter.notifyDataSetChanged();

     }