简体   繁体   English

使用上下文操作栏获取每个选定项目的价值

[英]Getting value of each selected item using a contextual action bar

I am using a contextual action bar and wish to retrieve each value selected to pass to another activity after I click on the 'mail' button. 我使用的是上下文操作栏,希望在单击“邮件”按钮后检索选择的每个传递给另一个活动的值。 How can I do this?e 我该怎么做?

Code for the CAB. CAB的代码。

mAdapter = new SelectionAdapter(this, R.layout.activity_result, R.id.name, new String[] {TAG_NAME, TAG_ROOM_PRICE});
    setListAdapter(mAdapter);
    lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE_MODAL);
    getListView().setMultiChoiceModeListener(new AbsListView.MultiChoiceModeListener() {

        private int nr = 0;
        @Override
        public boolean onCreateActionMode(android.view.ActionMode mode, Menu menu) {
            nr = 0;
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.contextual_menu, menu);
            return true;
        }

        @Override
        public boolean onPrepareActionMode(android.view.ActionMode mode, Menu menu) {
            return false;
        }

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


            }
            return false;
        }

        @Override
        public void onDestroyActionMode(android.view.ActionMode mode) {
            mAdapter.clearSelection();
        }

        @Override
        public void onItemCheckedStateChanged(android.view.ActionMode mode, int position, long id, boolean checked) {
            if (checked) {
                nr++;
                mAdapter.setNewSelection(position, checked);
            } else {
                nr--;
                mAdapter.removeSelection(position);
            }
            mode.setTitle("No: of resorts selected: " + nr);
        }
    });
    lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
            //String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
            getListView().setItemChecked(position, !mAdapter.isPositionChecked(position));
            return true;
        }
    });

Selection adapter class which is used to define the object mAdapter 选择适配器类,用于定义对象mAdapter

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

I wish to retrieve the String value of every selection that I make 我希望检索我所做的每个选择的字符串值

I found the solution. 我找到了解决方案。

mAdapter.setNewSelection(position, checked);
                String get_list = resortsList.get(position).get(TAG_NAME);
                stringList.add(get_list);

And to remove the item selected when deselected:`int i; 并取消选中时删除选中的项目:

for(i = 0 ; i < stringList.size(); i++){
     if(stringList.get(i).equals(resortsList.get(position).get(TAG_NAME))){
          stringList.remove(i);
          Log.d("String List: ", stringList.toString());
          break;
          }
 }`

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM