2013-10-11 234 views
1

如何使用下面的代碼從列表視圖中刪除多行,並使用MultiChoiceModeListener。Android從ListView中刪除多個項目

MainActivity

list.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); 
     list.setMultiChoiceModeListener(new MultiChoiceModeListener() { 

      @Override 
      public void onItemCheckedStateChanged(ActionMode mode, 
        int position, long id, boolean checked) { 
       // Here you can do something when items are selected/de-selected 
       final int checkedCount = list.getCheckedItemCount(); 
       // Update the title in the CAB 
       mode.setTitle(checkedCount + " Selected"); 

      } 
    @Override 
      public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
       switch (item.getItemId()) { 
       case R.id.menu_clear: 
        deleteSelectedItems(item.getItemId()); 
        mode.finish(); 
        return true; 
       default: 
        return false; 
       } 
      } 
      public void deleteSelectedItems(int id) { 
       adapter.remove(adapter.getItem(id)); //The method remove(Object) is undefined for the type ListViewAdapter 
      } 

ListViewAdapter

public class ListViewAdapter extends BaseAdapter { 

    // Declare Variables 
    Context context; 
    String[] rank; 
    String[] country; 
    String[] population; 
    LayoutInflater inflater; 
    View itemView; 

    public ListViewAdapter(Context context, String[] rank, String[] country, 
      String[] population) { 
     this.context = context; 
     this.rank = rank; 
     this.country = country; 
     this.population = population; 
    } 

    @Override 
    public int getCount() { 
     return rank.length; 
    } 

    @Override 
    public Object getItem(int position) { 
     return null; 
    } 

    @Override 
    public long getItemId(int position) { 
     return 0; 
    } 

    public View getView(int position, View convertView, ViewGroup parent) { 

     // Declare Variables 
     TextView txtrank; 
     TextView txtcountry; 
     TextView txtpopulation; 

     inflater = (LayoutInflater) context 
       .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 

     itemView = inflater.inflate(R.layout.listview_item, parent, false); 


     txtrank = (TextView) itemView.findViewById(R.id.rank); 
     txtcountry = (TextView) itemView.findViewById(R.id.country); 
     txtpopulation = (TextView) itemView.findViewById(R.id.population); 


     txtrank.setText(rank[position]); 
     txtcountry.setText(country[position]); 
     txtpopulation.setText(population[position]); 

     return itemView; 
    } 

EDITED

@Override 
      public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
       switch (item.getItemId()) { 
       case R.id.menu_clear: 

        int count = adapter.getCount(); 
        for (int i = 0; i < count; i++) { 
        adapter.remove(adapter.getItem(i)); 
        } 
        adapter.notifyDataSetChanged();//The method remove(Object) is undefined for the type ListViewAdapter 
        mode.finish(); 
        return true; 
       default: 
        return false; 
       } 
      } 
+0

您的問題是什麼? – CommonsWare

+0

我想從列表視圖中刪除多行。 –

+1

這不是一個問題。 – CommonsWare

回答

0

onItemCheckedStateChanged您記錄進行了檢查,所有的位置或ID的(以列表或東西)。

然後當某個菜單選項被點擊時,你循環遍歷所有的位置或ID並從數據集中刪除它們。

刪除它們後在您的適配器上調用notifyDatasetChanged

+0

所以你的意思是一切都會在deleteSelectedItems(int id)類中完成? –

+0

那麼這不是一個類,這就是所謂的方法,但沒有你不必在那裏做。在你的'onActionItemClicked'中做一個for循環,然後循環id的列表,然後把id傳給那個方法,然後從你的數據集中刪除這個項目。 – tyczj

+0

我得到這個錯誤對於ListViewAdapter類型,方法remove(Object)是未定義的。我需要做什麼 ?請幫忙 –