不会从数据库中检索 Listitem。它是从另一个类传递过来的。
问问题
1696 次
2 回答
3
您不会“从列表视图中删除列表项”。您修改ListAdapter
支持ListView
. 如果适配器是,ArrayAdapter
请调用remove()
. ArrayAdapter
如果适配器是CursorAdapter
,则从数据库requery()
和Cursor
. 等等。
于 2010-08-25T09:41:54.540 回答
0
如果您使用带有 ArrayList 的 SimpleAdapter 会更好。然后通过删除要删除的内容来更新列表,只需调用 adapter.notifyDataSetChanged().Like:
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();
}
于 2011-10-04T19:53:04.810 回答