BD / list error

0

I was following a tutorial on how to do CRUD, but an error appeared for me (in the tutorial it is ok)

How do I resolve this?

    lista = (ListView) findViewById(R.id.lista_Madicamentos);
    registerForContextMenu(lista);


    lista.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> adapter, View view, int position, long id) {
            Medicamentos medicamentoEscolhido = (Medicamentos) adapter.getItemIdAtPosition(position);

            Intent i = new Intent(CadastroActivity.this, FormularioActivity.class);
            i.putExtra("medicamento-escolhido", medicamentoEscolhido);
        }
    });

    lista.setOnLongClickListener(new AdapterView.OnItemLongClickListener() { //clique longo, para selecionar a linha
        @Override
        public boolean onItemLongClick(AdapterView<?> adapter, View view, int position, long id) {
            medicamento = (Medicamentos)adapter.getItemAtPosition(position);
            return false;
        }
    });

About error: In the line

Medicamentos medicamentoEscolhido = (Medicamentos) adapter.getItemIdAtPosition(position);

It gives the error:

Inconvertible types; cannot cast 'long' to 'com.example.(...).Medicamentos'

And it gives the suggestion that chosen medicine is long.

How do I resolve this? Thank you

    
asked by anonymous 13.10.2018 / 22:45

1 answer

0

The method name is spelled wrong.

Should not be getItemIdAtPosition() and yes getItemAtPosition() .

getItemIdAtPosition() returns a long , which is the item ID.

getItemAtPosition() returns the item, which can be converted to Medicamentos .

    
13.10.2018 / 22:55