I have been trying to use Contextual Action Bar on checkBox click to delete multiple rows from a listView... The listview has a checkbox and a textview in each row. In the adapter class, I'm calling startActionMode() as follows:
**@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
View view=convertView;
Log.v("ConvertView", String.valueOf(position));
final ToPayModel state = toPayList.get(position);
if (convertView == null)
{
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.to_pay_item, null);
holder = new ViewHolder();
holder.name = (TextView) convertView.findViewById(R.id.tvToPay);
holder.check = (CheckBox) convertView.findViewById(R.id.toPaycheck);
convertView.setTag(holder);
//holder.name.setOnCheckedChangeListener();
}
else
{
holder = (ViewHolder) convertView.getTag();
}
holder.check.setOnClickListener( new View.OnClickListener()
{
public void onClick(View v)
{
CheckBox cb = (CheckBox) v;
ToPayModel toPayModel = (ToPayModel) cb.getTag();
Toast.makeText(context, "Clicked on Checkbox: " + cb.getText() + " is " + cb.isChecked(),
Toast.LENGTH_LONG).show();
toPayModel.setCheck(cb.isChecked());
if(state.isCheck()){
mActionMode=context.startActionMode(new ActionBarCallback());
}
else{
mActionMode.finish();
}
}
});
//holder.code.setText(" (" + state.getCode() + ")");
holder.name.setText(state.getName());
holder.check.setTextColor(Color.BLACK);
holder.check.setChecked(state.isCheck());
holder.check.setTag(state);
return convertView;
}
public static final class ActionBarCallback implements ActionMode.Callback{
public ActionBarCallback(){
}
@Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.contextual_menu,menu);
return true;
}
@Override
public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
mode.setTitle("Checkbox Selected");
return false;
}
@Override
public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
switch(item.getItemId()){
case R.id.delete_item:
// removeSelection();
return true;
default: return false;
}
}
@Override
public void onDestroyActionMode(ActionMode mode) {
mode.finish();
}
}**
The Adapter class extends BaseAdapter
public class ToPayListAdapter extends BaseAdapter {
static Context context;
LayoutInflater inflater;
ActionMode mActionMode=null;
public ArrayList<ToPayModel> toPayList;
public ToPayListAdapter(Context context,ArrayList<ToPayModel> toPayList){
this.context=context;
this.toPayList=toPayList;
}
But the problem is, there is an error when i call startActionMode(). It says cannot resolve the method. Is it because I'm calling from within the getView method... I'm helpless... Any response will be appreciated. Thank you in advance.
The method startActionMode
is defined for Activity
but is not defined for Context
(see the documentation).
In your first chunk of code, if you are passing an Activity
as the context, you can cast it to Activity
as follows:
mActionMode=((Activity)context).startActionMode(new ActionBarCallback());
Or change the declaration
Context context;
to:
Activity context;
Edit from the comments:
If you are starting the ActionMode.Callback
from a View.OnClickListener()
in a Fragment
, you can retrieve the the Activity
this fragment is currently associated with using getActivity()
:
YourFragmentClass.this.getActivity().startActionMode(...);