我尝试在ListView中长按弹出菜单。我可以调用弹出菜单,但是当我点击此弹出菜单的选项时,onContextItemSelected()
不起作用。
这是我的代码:
package com.pa.homeautomationblank.fragments;
import android.app.Fragment;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.pa.homeautomationblank.R;
import com.pa.homeautomationblank.listeners.OnItemCentraisLongClickListener;
import com.pa.homeautomationblank.listeners.OnReconhecerCentraisClickListener;
import com.pa.homeautomationblank.model.Central;
import com.pa.homeautomationblank.model.dao.CentralDAO;
public class CentraisFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_centrais, container,false);
RelativeLayout rLay = (RelativeLayout) view.findViewById(R.id.layout_centrais_bottom);
Button btnReconhecerCentrais = (Button) rLay.findViewById(R.id.btnReconhecerCentrais);
btnReconhecerCentrais.setOnClickListener(new OnReconhecerCentraisClickListener());
CentralDAO cDAO = new CentralDAO(view.getContext());
ArrayAdapter<Central> adapter = new ArrayAdapter<Central>(view.getContext(), android.R.layout.simple_list_item_1 , cDAO.selectAll());
rLay = (RelativeLayout) view.findViewById(R.id.layout_centrais_conteudo);
ListView lv = (ListView) rLay.findViewById(R.id.lvCentrais);
lv.setAdapter(adapter);
lv.setOnItemLongClickListener(new OnItemCentraisLongClickListener());
registerForContextMenu(lv);
return (view);
}
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.lvCentrais){
menu.setHeaderTitle(R.string.operacoes);
String[] menuitems = getResources().getStringArray(R.array.menu_acoes_central);
for (int i=0;i<menuitems.length;i++){
menu.add(Menu.NONE,i,i,menuitems[i]);
}
}
}
@Override
public boolean onContextItemSelected(MenuItem item){
int menuindex = item.getItemId();
switch (menuindex) {
case 0:
Toast.makeText(getView().getContext(), "Rename", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(getView().getContext(), "Remove", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getView().getContext(), "invalid option!", Toast.LENGTH_SHORT).show();
break;
}
return super.onContextItemSelected(item);
}
}
我在方法中放了断点,但没有调用该事件。
提前致谢!
答案 0 :(得分:5)
我解决了我的问题。
我将此方法的实现更改为onContextItemSelected
到setOnMenuItemClickListener
方法内的onCreateContextMenu
,如以下代码所示:
@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
super.onCreateContextMenu(menu, v, menuInfo);
if (v.getId()==R.id.lvCentrais){
menu.setHeaderTitle(R.string.operacoes);
String[] menuitems = getResources().getStringArray(R.array.menu_acoes_central);
for (int i=0;i<menuitems.length;i++){
menu.add(Menu.NONE,i,i,menuitems[i]);
}
for (int i=0;i<menu.size();i++){
menu.getItem(i).setOnMenuItemClickListener(new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
int menuindex = item.getItemId();
switch (menuindex) {
case 0:
Toast.makeText(getView().getContext(), "Rename", Toast.LENGTH_SHORT).show();
break;
case 1:
Toast.makeText(getView().getContext(), "Remove", Toast.LENGTH_SHORT).show();
break;
default:
Toast.makeText(getView().getContext(), "invalid option!", Toast.LENGTH_SHORT).show();
break;
}
return false;
}
});
}
}
}
感谢您的帮助!
答案 1 :(得分:5)
对于仍在寻找解决方法的任何人,一旦我通过创建一个匿名OnMenuItemClickListener
来回答onContextItemSelected(MenuItem item)
并将其设置在菜单中的所有项目上,我就解决了这个问题。
@Override
public void onCreateContextMenu(ContextMenu menu, View view, ContextMenuInfo menuInfo) {
// Creation/inflate menu here
OnMenuItemClickListener listener = new OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
onContextItemSelected(item);
return true;
}
};
for (int i = 0, n = menu.size(); i < n; i++)
menu.getItem(i).setOnMenuItemClickListener(listener);
}