Вопрос

I have a listview with a custon adapter. I have to put a context menu for it, but it's not working. And I put the onItemLongClick to the list and it's not working too. I don't know how to trigger the contextmenu. If I have to click on an item or long click on it. I do register a long click to get the id from the item.

EDIT I think i figure out whats the problem. I have a button on my item listview. I remove this button from the layout and the context menu worked fine. But I need this button. Why the button was causing problem in the context menu?

This is the class:

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

change onCreateContextMenu like this :

@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);
}

see this topic :

Android, How to create Context Menu...

EDIT : use button. image Button and list view is clickable. if you use Button and set android:focusable="false" android:focusableInTouchMode="false" work fine.

Другие советы

Remove your setOnItemLongClickListener of listView and replace onContextItemSelected with this

@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;
    }
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top