0

我有一个带有自定义适配器的列表视图。我必须为其放置一个上下文菜单,但它不起作用。我把它放在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();
}

}
4

2 回答 2

2

删除您setOnItemLongClickListenerlistView并替换onContextItemSelected

@Override
    public boolean onContextItemSelected(MenuItem item) 
    {
        AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();

        if(item.getTitle().equals("Deletar"))
        {
           dDAO.open();
           dDAO.removerDespesasById(info.position);
           dDAO.close();
        }
    return true;
    }
于 2013-12-26T12:45:52.380 回答
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,如何创建上下文菜单...

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

于 2013-12-26T12:59:39.417 回答