This is my adapter class where I want to call startActionMode. I call it inside setActionMode method but got these errors:
Cannot cast from Context to ActivityFragment.
The method startActionMode(ActivityFragment.ActionModeCallback) is undefined for the type ActivityFragment.
public class ListAdapter extends ArrayAdapter<ListGettersSetters>
{
ArrayList<ListGettersSetters> arrayListGettersSetters;
LayoutInflater layoutInflater;
Context context;
int Resource, i = 0, j = 0, checkedItemsCount = 0;
Animation animation1;
Animation animation2;
CheckBox flipCheckBox;
viewHolder holder;
ActionMode actionMode;
boolean isActionModeShowing;
static class viewHolder
{
public CheckBox imageView;
public TextView textViewName;
public TextView textViewData;
public TextView textViewDetails;
}
public ListAdapter(Context context, int resource, ArrayList<ListGettersSetters> arrayListGettersSetters)
{
super(context, resource, arrayListGettersSetters);
this.arrayListGettersSetters = arrayListGettersSetters;
Resource = resource;
this.context = context;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
animation1 = AnimationUtils.loadAnimation(context, R.anim.to_middle);
animation2 = AnimationUtils.loadAnimation(context, R.anim.from_middle);
isActionModeShowing = false;
}
@Override
public int getCount()
{
return arrayListGettersSetters.size();
}
@Override
public ListGettersSettersgetItem(int position)
{
return arrayListGettersSetters.get(position);
}
@Override
public long getItemId(int position)
{
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
holder = new viewHolder();
if(convertView == null)
{
convertView = layoutInflater.inflate(Resource, null);
holder.imageView = (CheckBox) convertView.findViewById(R.id.id_for_checkBox);
holder.textViewName = (TextView) convertView.findViewById(R.id.id_for_name_textView);
holder.textViewData = (TextView) convertView.findViewById(R.id.id_for_data_textView);
holder.textViewDetails = (TextView) convertView.findViewById(R.id.id_for_details_textView);
convertView.setTag(holder);
}
else
{
holder = (viewHolder) convertView.getTag();
}
holder.textViewName.setText(getItem(position).getName());
holder.textViewData.setText(getItem(position).getData());
holder.textViewDetails.setText(getItem(position).getDetails());
holder.imageView.setTag(position);
holder.imageView.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
flipCheckBox = (CheckBox) view;
flipCheckBox.clearAnimation();
flipCheckBox.setAnimation(animation1);
flipCheckBox.startAnimation(animation1);
setAnimListners(arrayListGettersSetters.get(Integer.parseInt(view.getTag().toString())));
}
});
return convertView;
}
private void setAnimListners(final ListGettersSetters listGettersSetters)
{
AnimationListener animationListener = new AnimationListener()
{
@Override
public void onAnimationStart(Animation animation)
{
if (animation == animation1)
{
flipCheckBox.clearAnimation();
flipCheckBox.setAnimation(animation2);
flipCheckBox.startAnimation(animation2);
}
else
{
listGettersSetters.setIsChecked(!listGettersSetters.isChecked());
setCount();
setActionMode();
}
}
public void setCount()
{
if (listGettersSetters.isChecked())
{
checkedItemsCount++;
}
else
{
if (checkedItemsCount != 0)
{
checkedItemsCount--;
}
}
Log.v("Checked items count", checkedItemsCount + "");
}
private void setActionMode()
{
if (checkedItemsCount > 0)
{
if (!isActionModeShowing)
{
actionMode = ((ActivityFragment) context).startActionMode(new ActivityFragment.ActionModeCallback(context));
isActionModeShowing = true;
}
}
else if (actionMode != null)
{
actionMode.finish();
isActionModeShowing = false;
}
if (actionMode != null)
{
actionMode.setTitle(String.valueOf(checkedItemsCount));
}
notifyDataSetChanged();
}
@Override
public void onAnimationRepeat(Animation animation)
{
}
@Override
public void onAnimationEnd(Animation animation)
{
}
};
animation1.setAnimationListener(animationListener);
animation2.setAnimationListener(animationListener);
}
This is my ActivityFragment class also in which i have implemented a class named ActionModeCallback which is called in my adapter class. Also when i take context of ActivityFragment in this inner class then also get the same errors.
public class ActivityFragment extends ListFragment
{
@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState)
{
View view = layoutInflater.inflate(R.layout.folders_fragment_listview, null, false);
return view;
}
public static final class ActionModeCallback implements ActionMode.Callback
{
Context context;
public ActionModeCallback(Context context)
{
this.context = context;
}
@Override
public boolean onActionItemClicked(ActionMode actionMode, MenuItem menuItem)
{
Toast toast = null;
ArrayList<FoldersFragmentGettersSetters> selectedListItems = new ArrayList<FoldersFragmentGettersSetters>();
StringBuilder selectedItems = new StringBuilder();
for (FoldersFragmentGettersSetters foldersFragmentGettersSetters : ((ActivityFragment ) context).listAdapter.arrayListGettersSetters)
{
if (foldersFragmentGettersSetters.isChecked())
{
selectedListItems.add(foldersFragmentGettersSetters);
}
}
if (menuItem.getTitle().equals("Delete"))
{
toast = Toast.makeText(context, "Delete: " + selectedItems.toString(), Toast.LENGTH_SHORT);
}
else if (menuItem.getTitle().equals("Archive"))
{
toast = Toast.makeText(context, "Archive: " + selectedItems.toString(), Toast.LENGTH_SHORT);
}
else if (menuItem.getTitle().equals("Mark unread"))
{
toast = Toast.makeText(context, "Mark unread: " + selectedItems.toString(), Toast.LENGTH_SHORT);
}
else if (menuItem.getTitle().equals("Move"))
{
toast = Toast.makeText(context, "Move: " + selectedItems.toString(), Toast.LENGTH_SHORT);
}
else if (menuItem.getTitle().equals("Remove star"))
{
toast = Toast.makeText(context, "Remove star: " + selectedItems.toString(), Toast.LENGTH_SHORT);
}
if (toast != null)
{
toast.show();
}
actionMode.finish();
return true;
}
@Override
public boolean onCreateActionMode(ActionMode actionMode, Menu menu)
{
menu.add("Delete").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Archive").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Mark unread").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Move").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
menu.add("Remove star").setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM);
return true;
}
@Override
public void onDestroyActionMode(ActionMode actionMode)
{
((ActivityFragment ) context).inboxAdapter.checkedItemsCount = 0;
((ActivityFragment ) context).inboxAdapter.isActionModeShowing = false;
for (FoldersFragmentGettersSetters foldersFragmentGettersSettersItem : ((InboxFragment) context).inboxList)
{
foldersFragmentGettersSettersItem.setIsChecked(false);
}
((ActivityFragment ) context).listAdapter.notifyDataSetChanged();
Toast.makeText(context, "Action mode closed", Toast.LENGTH_SHORT).show();
}
@Override
public boolean onPrepareActionMode(ActionMode actionMode, Menu menu)
{
return false;
}
}
}
Modify the constructor of your ListAdapter from
public ListAdapter(Context context, int resource, ArrayList<ListGettersSetters> arrayListGettersSetters)
to
public ListAdapter(ActivityFragment context, int resource, ArrayList<ListGettersSetters> arrayListGettersSetters)
One good approach would be to use callback.
startActionModeCallback
.startActionModeCallback
.ActivityFragment
, pass it as listener and implement its callback method in fragment.Hope it makes sense to you. You can ask me for clarification. Good luck!
EXAMPLE
Adapter Class
public class ListAdapter extends ArrayAdapter<ListGettersSetters>
{
Interface StartActionInterface
{
public void startActionMode();
}
//declare other instance variables and add following
StartActionInterface startActionModeListener;
//change your constructor as:
public ListAdapter(Context context, int resource, ArrayList<ListGettersSetters> arrayListGettersSetters, StartActionInterface listener)
{
//normal code
startActionModeListener = listener;
}
}
ActivityFragment
public class ActivityFragment extends ListFragment implements StartActionInterface
{
@Override
public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle savedInstanceState)
{
View view = layoutInflater.inflate(R.layout.folders_fragment_listview, null, false);
////////initialize adapter and pass `this` in last argument
return view;
}
//Implement callback method. Also implement `setActionMode()` method here in fragment.
void startActionMode()
{
setActionMode();
}
}
Here in function setActionMode()
, replace following line:
if (!isActionModeShowing)
{
actionMode = ((ActivityFragment) context).startActionMode(new ActivityFragment.ActionModeCallback(context));
isActionModeShowing = true;
}
with:
if (!isActionModeShowing)
{
actionMode = getActivity().startActionMode(new ActionModeCallback(getActivity()));
isActionModeShowing = true;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With