2010-08-25 69 views
0

Listitem不會從數據庫中檢索。它從另一個班級轉移過來。我將如何從listview中刪除listitem?

+0

請詳細說明你的問題? – Praveen 2010-08-25 09:36:21

+0

lView.setAdapter(new ArrayAdapter (this, android.R.layout.simple_list_item_multiple_choice,lvItems)); \t \t \t \t \t lView.setChoiceMode(ListView.CHOICE_MODE_SINGLE) 我的列表項數組中:private string lvItems [] = { 「隨收隨付你漫遊」, 「國際漫遊」}; () public void remove() pos = lView.getCheckedItemPosition(); 如果(POS == 0){ \t/*刪除項目* /}} – User358218 2010-08-27 01:24:15

+0

我希望除去該特定項目時 [如果(POS == 0)] POS = lView.getCheckedItemPosition() ; – User358218 2010-08-27 01:29:49

回答

3

您不「從列表視圖中刪除listitem」。您修改支持ListViewListAdapter所擁有的數據。如果適配器是ArrayAdapter,請致電remove()ArrayAdapter。如果適配器是CursorAdapter,請從數據庫中刪除該項並requery()Cursor。等等。

+0

你可以給一個示例代碼? – User358218 2010-08-27 02:48:29

0

更好,如果你使用),一個SimpleAdapter它可以通過消除要刪除&只是調用adapter.notifyDataSetChanged(什麼需要一個ArrayList.Then您更新列表一樣:

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

    }