Synchronize Listview Ids with database

Asked

Viewed 624 times

0

I’m working on a college project where I use a database and a Listview. The problem is when I need to recover the data from one of the Listview items, as I cannot synchronize the Listview Ids with the BD Ids. In this case, if I register some items in the BD and then remove them, the Listview Ids no longer match those of the BD, making it an error when removing or viewing.

Is there any way to tie Listview items to a unique ID, which does not change if the order or quantity of Listview items changes?

  • How so não consigo sincronizar os IDs da ListView com os IDs do BD?

  • If I create 2 items, in BD they are registered as 1 and 2. In Listview it is 0 and 1. So far it is quiet, but when I delete item 0, for example, the item that was 1 has ID 0, making it impossible to recover data from BD.

  • Use the cursorAdapter, and whenever you delete an item from the list, reload it

1 answer

0


Depending on the items you have in ListView it would be better to have even associated objects. That is, each item in the list would be an object and that object would have the ID coming from the Database.

public class Objeto{

private int ID;
private ...;
...;
}

Then I’d have to create an Adptador of Object with the override of getPosition(Object o)

public class AdaptadorObjeto extends ArrayAdapter<Objeto> {

    @Override
    public int getPosition(Objeto o) {
        return super.getPosition(o);
    }
}

From there and through the current position, the touch can do what you want with the element, can delete from the database because it has the ID of the same.

Source Android Arrayadpter

I hope I’ve helped.

  • My project already uses an object to popular Listview. I just don’t fully understand the override part. Where do I put this code? Inside the class that defines my object?

  • You can put the code of how you fill in the ListView?

  • Of course: ArrayAdapter<Evento> eventos;&#xA;eventos = new ArrayAdapter<Evento>(this, android.R.layout.simple_list_item_1, bd.lerEventos());&#xA;setListAdapter(eventos);

  • And how are you going to "fetch" the list item to delete?

  • I think that is the biggest problem, because I need a new Activity to perform such operation (job requirement). In case, I make a Bundle within the onListItemClick and pass the dice to another Activity: Bundle info = new Bundle();&#xA;Evento selecionado = bd.lerEvento(position + 1);&#xA;...&#xA;info.putInt("posicao", position + 1);&#xA;&#xA;Intent visualizar = new Intent(this, Tela_Visualizacao.class);&#xA;visualizar.putExtras(info);&#xA;startActivity(visualizar);

  • In the other Activity it looks like this: posicao = dados.getInt("posicao");&#xA;public void removerEvento(int posicao) {&#xA;BancoDeDados bd = new BancoDeDados(this);&#xA;Evento praRemover = bd.lerEvento(posicao);&#xA;bd.apagarEvento(praRemover);&#xA;finish();&#xA;}

  • You inside the onListItemClick you need to fetch the Event, example: Evento ev = (Evento) listviw.getItemAtPosition(position); : ev.getID(); .

  • You have here a basic example.

  • 1

    Man, just using the .getItemAtPosition(position); already worked! Stopped giving error, I imagine I will not need to use the code you told me. Thank you very much!

Show 4 more comments

Browser other questions tagged

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