Delete button in ListView Android

2

I created a custom list using simpleCursorAdapter commonly accessed the elements by clicking on them as follows:

Listado = (ListView)findViewById(R.id.ListaAgregados);
Listado.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
     TextView codigo = (TextView) view.findViewById(R.id.mostrarCodigo);
     Log.d("ElementoSeleccionado>",codigo.getText.toString);


          }
        });

the elements are from the database Sqlite delete elements as follows:

basededatos.Eliminar(codigo); //codigo del elemento seleccionado.

so, what would the code look like when I clicked on the button?

    
asked by DoubleM 24.02.2017 в 09:02
source

1 answer

1

The truth is that I'm not sure if this is what you need but I just in case I tell you.

When you click on an item in the listview you get your position, so knowing this position you can check the adapter where you load the data and recover that position and you know what data is in that row. Something like this:

Listado = (ListView)findViewById(R.id.ListaAgregados);
Listado.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
     Object o = Listado.getItemAtPosition(position);
     String str=(String)o;//Esto suponiendo que la primera fila fuera una cadena string
     }
});

Here you can see how to retrieve data from a listview by clicking on an element of it. In this case we would recover a String assuming that it was the data type of your listView, otherwise you could do something like this:

ElTipoDeDatosEmpleado datos = Listado.getItemAtPosition(position);

I hope I have helped you or given you an idea of what you need to do.

Greetings.

    
answered by 24.02.2017 в 09:53