问题描述
这是我的适配器类,我想打电话给startActionMode。我把它叫做setActionMode方法中,但得到这些错误:
-
无法从上下文转换到ActivityFragment。
-
该方法startActionMode(ActivityFragment.ActionModeCallback)是未定义的类型
ActivityFragment。
公共类ListAdapter扩展ArrayAdapter< ListGettersSetters>
{
ArrayList的< ListGettersSetters> arrayListGettersSetters;
LayoutInflater layoutInflater;
上下文语境;
INT资源,I = 0,J = 0,checkedItemsCount = 0;
动画动画1;
动画animation2;
复选框flipCheckBox;
viewHolder持有人;
ActionMode actionMode;
布尔isActionModeShowing; 静态类viewHolder
{
公共复选框ImageView的;
公众的TextView textViewName;
公众的TextView textViewData;
公众的TextView textViewDetails;
} 公共ListAdapter(上下文的背景下,INT资源的ArrayList< ListGettersSetters> arrayListGettersSetters)
{
超级(上下文,资源,arrayListGettersSetters);
this.arrayListGettersSetters = arrayListGettersSetters;
资源=资源;
this.context =背景;
layoutInflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 动画1 = AnimationUtils.loadAnimation(背景下,R.anim.to_middle);
animation2 = AnimationUtils.loadAnimation(背景下,R.anim.from_middle); isActionModeShowing = FALSE;
} @覆盖
公众诠释getCount将()
{
返回arrayListGettersSetters.size();
} @覆盖
公共ListGettersSettersgetItem(INT位置)
{
返回arrayListGettersSetters.get(位置);
} @覆盖
众长getItemId(INT位置)
{
返回的位置;
} @覆盖
公共查看getView(最终诠释的立场,观点convertView,父母的ViewGroup)
{
持有人=新viewHolder();
如果(convertView == NULL)
{
convertView = layoutInflater.inflate(资源,NULL); holder.imageView =(复选框)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(保持器);
}
其他
{
支架=(viewHolder)convertView.getTag();
} holder.textViewName.setText(的getItem(位置).getName());
holder.textViewData.setText(的getItem(位置).getData());
holder.textViewDetails.setText(的getItem(位置).getDetails());
holder.imageView.setTag(位置);
holder.imageView.setOnClickListener(新OnClickListener()
{
公共无效的onClick(查看视图)
{
flipCheckBox =(复选框)视图。
flipCheckBox.clearAnimation();
flipCheckBox.setAnimation(动画1);
flipCheckBox.startAnimation(动画1);
setAnimListners(arrayListGettersSetters.get(的Integer.parseInt(view.getTag()的toString())));
}
}); 返回convertView;
} 私人无效setAnimListners(最终ListGettersSetters listGettersSetters)
{
AnimationListener animationListener =新AnimationListener()
{
@覆盖
公共无效onAnimationStart(动画动画)
{
如果(动画==动画1)
{
flipCheckBox.clearAnimation();
flipCheckBox.setAnimation(animation2);
flipCheckBox.startAnimation(animation2);
}
其他
{
listGettersSetters.setIsChecked(listGettersSetters.isChecked()!);
setCount();
setActionMode();
}
} 公共无效setCount()
{
如果(listGettersSetters.isChecked())
{
checkedItemsCount ++;
}
其他
{
如果(checkedItemsCount!= 0)
{
checkedItemsCount--;
}
} Log.v(选中的项数,checkedItemsCount +);
} 私人无效setActionMode()
{
如果(checkedItemsCount大于0)
{
如果(!isActionModeShowing)
{
actionMode =((ActivityFragment)上下文).startActionMode(新ActivityFragment.ActionModeCallback(上下文));
isActionModeShowing = TRUE;
}
}
否则,如果(actionMode!= NULL)
{
actionMode.finish();
isActionModeShowing = FALSE;
} 如果(actionMode!= NULL)
{
actionMode.setTitle(将String.valueOf(checkedItemsCount));
} notifyDataSetChanged();
} @覆盖
公共无效onAnimationRepeat(动画动画)
{
} @覆盖
公共无效onAnimationEnd(动画动画)
{
}
};
animation1.setAnimationListener(animationListener);
animation2.setAnimationListener(animationListener);
}
这是我的ActivityFragment类还住在我所实现的一类名为ActionModeCallback这就是所谓的在我的适配器类。此外,当我把ActivityFragment的情况下在这个内部类的话也得到了同样的错误。
公共类ActivityFragment扩展ListFragment
{
@覆盖
公共查看onCreateView(LayoutInflater layoutInflater,ViewGroup中ViewGroup中,捆绑savedInstanceState)
{
查看查看= layoutInflater.inflate(R.layout.folders_fragment_listview,空,假); 返回视图。
} 公共静态final类ActionModeCallback实现ActionMode.Callback
{
上下文语境; 公共ActionModeCallback(上下文的背景下)
{
this.context =背景;
} @覆盖
公共布尔onActionItemClicked(ActionMode actionMode,菜单项菜单项)
{
吐司面包= NULL;
ArrayList的< FoldersFragmentGettersSetters> selectedListItems =新的ArrayList< FoldersFragmentGettersSetters>(); StringBuilder的selectedItems =新的StringBuilder(); 对于(FoldersFragmentGettersSetters foldersFragmentGettersSetters:((ActivityFragment)上下文).listAdapter.arrayListGettersSetters)
{
如果(foldersFragmentGettersSetters.isChecked())
{
selectedListItems.add(foldersFragmentGettersSetters);
}
} 如果(menuItem.getTitle()。等于(删除))
{
敬酒= Toast.makeText(背景下,删除+ selectedItems.toString(),Toast.LENGTH_SHORT);
}
否则,如果(menuItem.getTitle()。等于(归档))
{
敬酒= Toast.makeText(背景下,归档+ selectedItems.toString(),Toast.LENGTH_SHORT);
}
否则,如果(menuItem.getTitle()。等于(标记为未读))
{
敬酒= Toast.makeText(背景下,标记为未读:+ selectedItems.toString(),Toast.LENGTH_SHORT);
}
否则,如果(menuItem.getTitle()。等于(移动))
{
敬酒= Toast.makeText(背景下,移动+ selectedItems.toString(),Toast.LENGTH_SHORT);
}
否则,如果(menuItem.getTitle()。等于(删除星))
{
敬酒= Toast.makeText(背景下,删除星:+ selectedItems.toString(),Toast.LENGTH_SHORT);
}
如果(干杯!= NULL)
{
toast.show();
}
actionMode.finish();
返回true;
} @覆盖
公共布尔onCreateActionMode(ActionMode actionMode,菜单菜单)
{
menu.add(删除)setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)。
menu.add(归档)setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)。
menu.add(标记为未读)setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)。
menu.add(移动)setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)。
menu.add(删除星)setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM)。 返回true;
} @覆盖
公共无效onDestroyActionMode(ActionMode actionMode)
{
((ActivityFragment)上下文).inboxAdapter.checkedItemsCount = 0;
((ActivityFragment)上下文).inboxAdapter.isActionModeShowing = FALSE;
对于(FoldersFragmentGettersSetters foldersFragmentGettersSettersItem:((InboxFragment)上下文).inboxList)
{
foldersFragmentGettersSettersItem.setIsChecked(假);
}
((ActivityFragment)上下文).listAdapter.notifyDataSetChanged(); Toast.makeText(背景下,操作模式闭,Toast.LENGTH_SHORT).show();
} @覆盖
在prepareActionMode公共布尔(ActionMode actionMode,菜单菜单)
{
返回false;
}
}
}
一个好的方法是使用回调。
- 您的适配器类中创建回调接口(侦听器)。
- 声明实例变量的接口,例如
startActionModeCallback
。 - 在适配器构造,通过侦听器对象,并将其分配给
startActionModeCallback
。 - 当你将在
ActivityFragment
创建适配器的情况下,把它作为听众和实施其片段回调方法。 - 呼叫回调方法,在适配器中的任何地方,它会按片段被倾听。
希望这对你有意义。你可以问我要澄清。祝你好运!
示例
适配器类别
公共类ListAdapter扩展ArrayAdapter< ListGettersSetters>
{接口StartActionInterface
{
公共无效startActionMode();
}
//声明其它的实例变量,并添加以下StartActionInterface startActionModeListener;//改变你的构造函数为:公共ListAdapter(上下文的背景下,INT资源的ArrayList< ListGettersSetters> arrayListGettersSetters,StartActionInterface听众)
{
//正常code
startActionModeListener =侦听器;
}}
ActivityFragment
公共类ActivityFragment扩展ListFragment实现StartActionInterface
{
@覆盖
公共查看onCreateView(LayoutInflater layoutInflater,ViewGroup中ViewGroup中,捆绑savedInstanceState)
{
查看查看= layoutInflater.inflate(R.layout.folders_fragment_listview,空,假);////////初始化适配器,并通过`this`在最后一个参数 返回视图。
}//实现回调方法。这里还实现了在片段`setActionMode()`方法。无效startActionMode()
{
setActionMode();
}}
下面的函数 setActionMode()
,替换以下行:
如果(!isActionModeShowing)
{
actionMode =((ActivityFragment)上下文).startActionMode(新ActivityFragment.ActionModeCallback(上下文));
isActionModeShowing = TRUE;
}
与
如果(!isActionModeShowing)
{
actionMode = getActivity()startActionMode(新ActionModeCallback(getActivity()))。
isActionModeShowing = TRUE;
}
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;
}
}
}
One good approach would be to use callback.
- Create callback interface (a listener) inside your adapter class.
- Declare instance variable for interface, e.g.
startActionModeCallback
. - In adapter constructor, pass listener object and assign it to
startActionModeCallback
. - When you will create adapter's instance in
ActivityFragment
, pass it as listener and implement its callback method in fragment. - Call callback method anywhere in your adapter, and it will be listened by 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;
}
这篇关于如何从它的适配器类扩展ListFragment活动的背景下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!