Android使用上下文操作栏删除所选项目

时间:2014-02-06 06:23:38

标签: android listview

到目前为止,我已设法遵循this教程并成功运行它。但我现在要做的是实际删除列表视图中的选定项目。这是我正在使用的代码:

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;
    }
}

问题是如何删除所选项目?我无法理解,如果你们中的任何人都知道这一点并且可以提供帮助,我很乐意欣赏它。感谢。

4 个答案:

答案 0 :(得分: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;
    }

希望这有帮助

答案 1 :(得分:0)

您必须自己编写功能代码:在onActionItemClicked中,您必须编写将删除ArrayList中所选项目的逻辑。

在此之后不要忘记在适配器上调用notifyDatasetChanged() ...; - )

答案 2 :(得分:0)

您可以在ListView中的项目上设置OnLongClickListener,只获取长按的项目。

之后,您可以在OnActionItemClicked

中引用/删除该对象

答案 3 :(得分:0)

我不确定你是否已经解决了问题,但这对我有用:

  1. 在班级中创建私人(或公共)变量:private int selectedPosition;
  2. 在您的onItemLongClick()方法中(据我所知,那是您选择项目的那个),请输入:

    selectedPosition = position;

  3. OnActionItemClicked()中,您可以获得适配器并执行此操作:

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

    adapter.notifyDataSetChanged();

  4. 这样您就可以将所选项目的位置传递给适配器。

    希望有所帮助!