Synchronize ListView IDs with database

0

I'm working on a project for college where I use a database and a ListView. The problem is when I need to retrieve the data from one of the ListView items because I can not synchronize the ListView IDs with the DB IDs. In case, if I register some items in the DB and then remove them, the ListView IDs no longer correspond to the DB's, causing it to error when removing or viewing.

Is there a way to bind ListView items to a unique ID, which does not change if the order or number of ListView items changes?

    
asked by anonymous 07.04.2014 / 18:00

1 answer

0

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

public class Objeto{

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

Then you would have to create a Object Adpt- er 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, touch can do whatever you want with the element, and can delete from the database because it has the ID of the same.

Source Android ArrayAdpter

I hope I have helped.

    
07.04.2014 / 18:21