为什么上下文菜单不起作用?

时间:2013-12-26 12:34:49

标签: android android-listview contextmenu android-contextmenu

我有一个使用custon适配器的listview。我必须为它设置一个上下文菜单,但它不起作用。我把onItemLongClick放到了列表中,它也没有用。我不知道如何触发contextmenu。如果我必须点击某个项目或长按它。我注册了一个长按来获取该项目的ID。

编辑我想我知道这个问题是什么。我的项目列表视图上有一个按钮。我从布局中删除此按钮,上下文菜单工作正常。但我需要这个按钮。为什么按钮在上下文菜单中导致问题?

这是班级:

public class HistoricoDespesasActivity extends Activity {

Helper h;
AlphaAnimation buttonClick; 
DespesasDAO dDAO;
ListView lv;
DespesaHistoricoAdapter adapter;
int idDespesasSelecionada;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_historico_despesas);

    lv = (ListView)findViewById(R.id.lvHistoricoDespesas);  

    TextView tvMarcaModelo = (TextView)findViewById(R.id.tvMarcaModeloCabecalho);
    TextView tvApelido = (TextView)findViewById(R.id.tvApelidoCabecalho);

    tvApelido.setVisibility(View.INVISIBLE);
    tvMarcaModelo.setVisibility(View.INVISIBLE);

    buttonClick = new AlphaAnimation(1, 0.5f);
    h = new Helper(this);
    h.mostraVeiculoAtivo();

    adapter = new DespesaHistoricoAdapter(this);

    dDAO = new DespesasDAO(this);
    dDAO.open();
    Cursor cursor = dDAO.consultarTodasDespesasByIdVeiculo(h.getId());

    int id;
    String data;
    String tipoDespesa = null;
    double valor;
    int tipo = 0;
    if(cursor != null && cursor.moveToFirst()){
        do {
            id = cursor.getInt(cursor.getColumnIndex(DespesasDAO.COLUNA_ID));
            data = cursor.getString(cursor.getColumnIndex(DespesasDAO.COLUNA_DESPESA_DATA));
            tipo = cursor.getInt(cursor.getColumnIndex(DespesasDAO.COLUNA_ITEM_ID));
            valor = cursor.getDouble(cursor.getColumnIndex(DespesasDAO.COLUNA_DESPESA_VALOR));

            if(tipo == 1){
                tipoDespesa = "Pedágio";
            } else if(tipo == 2){
                tipoDespesa = "Estacionamento";
            } else if(tipo == 3){
                tipoDespesa = "Lavagem";
            } else if(tipo == 4){
                tipoDespesa = "Diversos";
            }

            adapter.addDespesa(id, tipoDespesa, data, valor);

        } while (cursor.moveToNext());

        cursor.close();
        dDAO.close();
        lv.setAdapter(adapter);
    }
    lv.setLongClickable(true);
    lv.setOnItemLongClickListener(new OnItemLongClickListener() {
        @Override
        public boolean onItemLongClick(AdapterView<?> parent, View view,
                int position, long id) {

            idDespesasSelecionada = (Integer) parent.getItemAtPosition(position);
            return true;
        }
    });
    registerForContextMenu(lv);

}

@Override
public void onCreateContextMenu(ContextMenu menu, View v,
        ContextMenuInfo menuInfo) {     
    super.onCreateContextMenu(menu, v, menuInfo);
    menu.setHeaderTitle("Despesas");
    menu.add(0, v.getId(), 0, "Deletar");

}

@Override
public boolean onContextItemSelected(MenuItem item) {       

    if(item.getTitle().equals("Deletar")){
        dDAO.open();
        dDAO.removerDespesasById(idDespesasSelecionada);
        dDAO.close();
    }



    onCreate(new Bundle());
    return super.onContextItemSelected(item);
}

@Override
protected void onResume() {
    onCreate(new Bundle());
    super.onResume();
}

}

2 个答案:

答案 0 :(得分:2)

移除<{1>} listView 并将setOnItemLongClickListener替换为

onContextItemSelected

答案 1 :(得分:1)

像这样改变onCreateContextMenu:

@Override
public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {
    super.onCreateContextMenu(menu, v, menuInfo);
    MenuInflater inflater =getMenuInflater();
    inflater.inflate(R.menu.more_tab_menu, menu);
}

请参阅此主题:

Android, How to create Context Menu...

编辑: 使用按钮。图像按钮和列表视图是可单击的。如果您使用Button并设置android:focusable="false" android:focusableInTouchMode="false"正常工作。