Error in list/BD

Asked

Viewed 66 times

0

I have an error in this excerpt (appears the whole stretch of red), what is wrong?

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

Java:

package com.example.vanessa.projetoinicial_vanessa;

import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;

import com.example.vanessa.projetoinicial_vanessa.BD.MedicamentosBd;
import com.example.vanessa.projetoinicial_vanessa.model.Medicamentos;

import java.util.ArrayList;

public class CadastroActivity extends AppCompatActivity {

ListView lista;
MedicamentosBd bdMed;
ArrayList<Medicamentos> lista_Madicamentos;
Medicamentos medicamento;
ArrayAdapter adapter;

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

    //Ao clicar no botão Cadastrar (da tela 2 Cadastro), deve ir para tela Formulario
    Button botaoCadastrar = (Button) findViewById(R.id.btn_cadastrar);
    botaoCadastrar.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Intent intent = new Intent(CadastroActivity.this, FormularioActivity.class);
            startActivity(intent);
        }
    });

    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.getItemAtPosition(position); //retorna o item, que é convertido (cast) para Medicamentos

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




}

@Override
public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){
    MenuItem menuDelete = menu.add("Deletar Este Medicamento");
    menuDelete.setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            bdMed = new MedicamentosBd(CadastroActivity.this);
            bdMed.deletarMedicamento(medicamento);
            bdMed.close();
            carregarMedicamento(); //assim que deleta, ele carrega para atualizar a lista
            return true;
        }
    });
}

protected void onResume(){
    super.onResume();
    carregarMedicamento();
}

public void carregarMedicamento(){ //carregar a lista de medicamentos
    bdMed = new MedicamentosBd(CadastroActivity.this);
    lista_Madicamentos = bdMed.getLista();
    bdMed.close();
    if(lista_Madicamentos != null){
        adapter = new ArrayAdapter<Medicamentos>(CadastroActivity.this,android.R.layout.simple_list_item_1,lista_Madicamentos);
        lista.setAdapter(adapter);
    }

}
}

Error:

error: incompatible types: <anonymous OnItemLongClickListener> cannot be converted to OnLongClickListener

setOnLongClickListener (android.view.View.OnLongClickListener) in View cannot be applied
to (anonymous android.widget.AdapterView.OnItemLongClickListener)

 

  • Buguei. I’m rusty from Android. I’ll delete my answer and let someone answer.

2 answers

0

Try this way:

lista.setOnLongClickListener(new OnLongClickListener()
        {
            @Override
            public boolean onLongClick(View v)
            {
                .
                .
                .
            }
        });

I hope it helps!

0

Oh error is in the incompatibility of you trying to pass the AdapterView.OnItemLongClickListenerto the interface setOnLongClickListener. Since you want to capture the medicine that was clicked, so you have to use the lista.setOnItemLongClickListener, just change the set.

It would probably look like this:

lista.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener(){
        @Override
        public boolean onItemLongClick(AdapterView<?> adapter, View view, int position, long id) {
            medicamento = (Medicamentos)adapter.getItemAtPosition(position);
            return false;
        }
    });

Browser other questions tagged

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