2014-02-06 95 views
3

到目前爲止,我已設法遵循this教程併成功運行它。但是我現在想要做的是實際刪除我的列表視圖中的選定項目。這是我正在使用的代碼:Android使用上下文操作欄刪除選定的項目

private String[] data = {"One", "Two", "Three", "Four", "Five", "Six", "Seven", "Eight", "Nine","Ten"}; 

private SelectionAdapter mAdapter; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    mAdapter = new SelectionAdapter(this, 
       R.layout.row_list_item, R.id.textView1, data); 
    setListAdapter(mAdapter); 
    getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL); 

    getListView().setMultiChoiceModeListener(new MultiChoiceModeListener() { 

     private int nr = 0; 

     @Override 
     public boolean onPrepareActionMode(ActionMode mode, Menu menu) { 
      // TODO Auto-generated method stub 
      return false; 
     } 

     @Override 
     public void onDestroyActionMode(ActionMode mode) { 
      // TODO Auto-generated method stub 
      mAdapter.clearSelection(); 
     } 

     @Override 
     public boolean onCreateActionMode(ActionMode mode, Menu menu) { 
      // TODO Auto-generated method stub 

      nr = 0; 
      MenuInflater inflater = getMenuInflater(); 
      inflater.inflate(R.menu.contextual_menu, menu); 
      return true; 
     } 

     @Override 
     public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
      // TODO Auto-generated method stub 
      switch (item.getItemId()) { 

       case R.id.item_delete: 
        nr = 0; 
        mAdapter.clearSelection(); 
        mode.finish(); 
      } 
      return true; 
     } 

     @Override 
     public void onItemCheckedStateChanged(ActionMode mode, int position, 
       long id, boolean checked) { 
      // TODO Auto-generated method stub 
      if (checked) { 
        nr++; 
        mAdapter.setNewSelection(position, checked);     
       } else { 
        nr--; 
        mAdapter.removeSelection(position);     
       } 
       mode.setTitle(nr + " selected"); 

     } 
    }); 

    getListView().setOnItemLongClickListener(new OnItemLongClickListener() { 

     @Override 
     public boolean onItemLongClick(AdapterView<?> arg0, View arg1, 
       int position, long arg3) { 
      // TODO Auto-generated method stub 

      getListView().setItemChecked(position, !mAdapter.isPositionChecked(position)); 
      return false; 
     } 
    }); 
} 

private class SelectionAdapter extends ArrayAdapter<String> { 

    private HashMap<Integer, Boolean> mSelection = new HashMap<Integer, Boolean>(); 

    public SelectionAdapter(Context context, int resource, 
      int textViewResourceId, String[] objects) { 
     super(context, resource, textViewResourceId, objects); 
    } 

    public void setNewSelection(int position, boolean value) { 
     mSelection.put(position, value); 
     notifyDataSetChanged(); 
    } 

    public boolean isPositionChecked(int position) { 
     Boolean result = mSelection.get(position); 
     return result == null ? false : result; 
    } 

    public Set<Integer> getCurrentCheckedPosition() { 
     return mSelection.keySet(); 
    } 

    public void removeSelection(int position) { 
     mSelection.remove(position); 
     notifyDataSetChanged(); 
    } 

    public void clearSelection() { 
     mSelection = new HashMap<Integer, Boolean>(); 
     notifyDataSetChanged(); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     View v = super.getView(position, convertView, parent);//let the adapter handle setting up the row views 
     v.setBackgroundColor(getResources().getColor(android.R.color.background_light)); //default color 

     if (mSelection.get(position) != null) { 
      v.setBackgroundColor(getResources().getColor(android.R.color.holo_blue_light));// this is a selected position so make it red 
     } 
     return v; 
    } 
} 

問題是如何刪除所選項目?我無法弄清楚,如果你們中的任何人都知道這一點,可以提供幫助,我很樂意欣賞它。謝謝。

回答

0

您必須親自編寫功能:在onActionItemClicked中,您必須編寫將從您的ArrayList中刪除選定項目的邏輯。

不要忘記調用notifyDatasetChanged()後您的適配器上... ;-)

+0

好吧,我這樣做,但我的問題是如何讓選擇的位置? – Dunkey

0

您可以在剛剛獲取長按該項目ListView控件中的項目設置OnLongClickListener

然後,您可以在您的OnActionItemClicked中引用/刪除該對象。

+1

你能提供一些樣例實現嗎?謝謝。 – Dunkey

0

我不知道你是否已經解決您的問題,但這裏是我工作:

  1. 在你的類創建一個私人(或公共)變量:private int selectedPosition;
  2. 在你onItemLongClick()方法(據我所見,那是你選擇物品的地方),放:

    selectedPosition = position;

  3. OnActionItemClicked()

    你可以得到你的適配器,並做到這一點:

    MyAdapter適配器=(MyAdapter)this.getAdapter(); adapter.remove(adapter.getItem(selectedPosition);

    adapter.notifyDataSetChanged();

你通過選項的位置,以您的適配器這樣

希望有所幫助。 !

+0

你好,我嘗試過這一個,但我得到了不受支持的操作感覺 – Dunkey

+0

嗯有趣。你把你的變量變爲公開還是私人?也許你可以發表一個你做過的例子 – lulu

1

在你的代碼中,而不是使用string [] data你可以使用ArrayList<String> data,因爲它是動態調整大小的數組,其中的對象實際上被刪除並且相應地調整列表(數組)的大小。是改變你可以使用下面的方法。

@Override 
public boolean onActionItemClicked(ActionMode mode, MenuItem item) { 
     List<Integer> keyList = new ArrayList<Integer>(noteAdptr.getCurrentCheckedPosition()); 

     Collections.sort(keyList, new Comparator<Integer>() { 

      @Override 
      public int compare(Integer lhs, Integer rhs) { 

       return lhs.compareTo(rhs); 

      } 
     }); 
     Collections.reverse(keyList); 

     switch (item.getItemId()) { 

      case R.id.item_delete: 
       nr = 0; 
       for (Integer i:keyList){ 
       Log.e("", "items selected is : "+i); 
       data.remove((int)i); 
       } 
       mAdapter .notifyDataSetChanged(); 
       mAdapter.clearSelection(); 
       mode.finish(); 
     } 
     return true; 
    } 

希望這有助於

相關問題