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