Android deletes selected item using action context bar

So far, I have been able to complete this tutorial and successfully run it. But now I want to remove the selected items in my list. This is the code I'm using:

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

Question: how to delete the selected item / s? I cannot understand this, and if any of you know this and can help, I would gladly welcome him. Thank.

+3
source share
4 answers

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

: onActionItemClicked , ArrayList.

notifyDatasetChanged() ...; -)

0

OnLongClickListener ListView, , .

/ OnActionItemClicked.

0

I'm not sure that you have already solved your problem, but here is what worked for me:

  • Create a private (or public) variable in your class: private int selectedPosition;
  • In your method onItemLongClick()(as far as I can see, the one where you select the element), put:

    selectedPosition = position;

  • in OnActionItemClicked()you can get your adapter and do this:

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

    adapter.notifyDataSetChanged ();

Thus, you passed the position of the selected item to your adapter.

Hope this helps!

0
source

All Articles