Gop*_*opi 24 android listview popupmenu
我的listview有一个自定义适配器.适配器包含textview和图像按钮.单击图像按钮后,我实现了一个弹出菜单.一切都很好.但是当从弹出菜单中选择选项时,logcat显示单行消息"尝试完成输入事件但输入事件接收器已被处理"并且没有发生任何事情.
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context context, int resourceId) {
super(context, resourceId);
}
public MyAdapter(Context context, int resourceId, List<String> string) {
super(context, resourceId, string);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if(v == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
v = inflater.inflate(R.layout.adapter_layout, null);
}
String str = getItem(position);
if(str != null) {
TextView textView = (TextView)v.findViewById(R.id.editText1);
textView.setText(str);
ImageButton button = (ImageButton)v.findViewById(R.id.imageButton1);
button.setOnClickListener(new Custom_Adapter_Button_Click_Listener(getItemId(position), getContext()));
}
return v;
}
}
Run Code Online (Sandbox Code Playgroud)
onclicklistener接口是
public class Custom_Adapter_Button_Click_Listener implements OnClickListener, OnMenuItemClickListener {
long position;
Context context;
public Custom_Adapter_Button_Click_Listener(long id, Context appcontext) {
position = id;
context = appcontext;
}
@Override
public boolean onMenuItemClick(MenuItem item) {
AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo();
int index = info.position;
Log.d("ItemClicked", "selected index : " + index);
switch(item.getItemId()) {
case R.id.option :
Toast.makeText(context, "Selected index : " + index, Toast.LENGTH_SHORT).show();
return true;
default :
Toast.makeText(context, "Default", Toast.LENGTH_SHORT).show();
return false;
}
}
@Override
public void onClick(View v) {
PopupMenu popup = new PopupMenu(context, v);
MenuInflater popupInflater = popup.getMenuInflater();
popupInflater.inflate(R.menu.contextmenu, popup.getMenu());
popup.show();
}
}
Run Code Online (Sandbox Code Playgroud)
我从消息中理解的是,在onMenuItemClick()获取执行之前,有些事情正在吃事件.我在nexus 5 android 5.0.1上运行我的应用程序.
我从这里找到了类似问题的解决方案.但我没有得到如何使用这种方法来解决我的问题.
我尝试使用上下文菜单而不是弹出菜单,但是在点击上下文菜单项后,我仍然有相同的消息"尝试完成输入事件但输入事件接收器已被处理".
请帮我...!!
我从另一个角度来看这个问题; 尝试从菜单启动服务.默认情况下,调试消息不太相关.我的解决方案是消除logcat中的过滤器然后我得到另一条消息,它首先无法启动服务(我忘了把它放在我的清单文件中).
在您的情况下,您可能需要将toast显示包装到类中:
public class DisplayToast implements Runnable {
private final Context mContext;
private final String mText;
public DisplayToast(Context mContext, String text) {
this.mContext = mContext;
mText = text;
}
public void run() {
Toast.makeText(mContext, mText, Toast.LENGTH_SHORT).show();
}
}
Run Code Online (Sandbox Code Playgroud)
并通过一个Handler对象调用它:
mHandler.post(new DisplayToast(this, "Epic message!"));
Run Code Online (Sandbox Code Playgroud)
或者,更好的是,使用Handler.postDelayed()方法.
HTH,