How to get activity context extending ListFragment from its adapter class?

This is my adapter class where I want to call startActionMode. I call it inside the setActionMode method, but got these errors:

  • Unable to pass from context to ActivityFragment.

  • The startActionMode method (ActivityFragment.ActionModeCallback) is undefined for the type of 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, in which I implemented a class called ActionModeCallback, which is called in my adapter class. Also, when I take the ActivityFragment context in this inner class, then I 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; } } } 
+5
source share
2 answers

Change the constructor of your ListAdapter with

 public ListAdapter(Context context, int resource, ArrayList<ListGettersSetters> arrayListGettersSetters) 

to

 public ListAdapter(ActivityFragment context, int resource, ArrayList<ListGettersSetters> arrayListGettersSetters) 
+3
source

One good approach is to use a callback.

  • Create a callback interface (listener) inside your adapter class.
  • Declare an instance variable for an interface, for example. startActionModeCallback .
  • In the adapter constructor, pass the listener object and assign it startActionModeCallback .
  • When you instantiate the adapter in the ActivityFragment , pass it as a listener and implement its callback method in the fragment.
  • Call the callback method anywhere in your adapter and it will be listened to by the snippet.

Hope this 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 the setActionMode() function, replace the following line:

  if (!isActionModeShowing) { actionMode = ((ActivityFragment) context).startActionMode(new ActivityFragment.ActionModeCallback(context)); isActionModeShowing = true; } 

from:

 if (!isActionModeShowing) { actionMode = getActivity().startActionMode(new ActionModeCallback(getActivity())); isActionModeShowing = true; } 
+2
source

Source: https://habr.com/ru/post/1210551/


All Articles