我为我的列表视图(listview)编写了一个自定义适配器(custom adapter)。该适配器包含一个文本视图和一个图像按钮。我已经在单击图像按钮时实现了弹出菜单(popup menu)。一切都正常工作。但是当从弹出菜单选择选项时,logcat显示一条单行消息“Attempted to finish an input event but input event receiver has already been disposed”,什么也没发生。
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;
}
}
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();
}
}
我理解这条消息的意思是,在 onMenuItemClick() 执行之前,某些东西正在捕食事件。我正在 Nexus 5 Android 5.0.1 上运行我的应用程序。我在这里找到了一个类似问题的解决方案。但是我不知道如何将这种方法用于我的问题。
我尝试使用上下文菜单而不是弹出菜单,但是在单击上下文菜单项后仍然出现相同的消息“尝试完成输入事件,但输入事件接收器已被丢弃”。
请帮助我...!!