Error in BD/list

Asked

Viewed 25 times

0

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

How do I fix it?

    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 the error: On the line

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

He makes the mistake:

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

And suggests that medicationSchool is the long type.

How do I fix it? Thank you

  • you need to see if the getItemIdAtPosition method returns the drug object by name I would say it is returning only the Id...

1 answer

0


Method name spelled wrong.

Shouldn’t getItemIdAtPosition() and yesgetItemAtPosition().

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

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

  • Ahh, I was really wrong, thank you very much. Now, there is another error in the second section of the code. I will post below

  • Another mistake, another question. Sorry, it’s house rules.

  • To accept this answer click on the "v" that appears below the number, on the left side of the post. It is the way the site works.

  • Oh yes, even about the same code? It’s error in the second section I sent

  • Even about the same code, the questions and answers are punctual. This helps in the organization of the site.

  • Okay, thank you very much ^^

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.