dimanche 23 août 2015

Sqlite ordering method

I'm trying to do some kind of an ordering method for the items I have in a sqlite database. At first I have this RecyclerView that loads data from the database. Rows

Via the drag handle I drag the rows to the desired position. The thing I want to do is save that position and to be able to get it and rearrange the rows exactly the same next time I start the app.

I tried detecting when a row is moved via the onItemMove method of the RecyclerView.Adapter class and saving the new positions for the rows that swapped positions but the positions get messed up and I don't get the desired result. Maybe the solution is fine but my ordering method is messed up.. This is what I implemented before passing the data to the adapter

public static ArrayList<Package> orderPackages(ArrayList<Package> packages) {
        for(int i = 0; i < packages.size(); i++) {
            Package p = packages.get(i);
            int desiredPosition = p.getPosition();
            if(desiredPosition != -1) {
                if(desiredPosition < packages.size()) {
                    Package tempPackage = packages.get(i);
                    packages.remove(i);
                    packages.add(tempPackage.getPosition(), tempPackage);
                }
            }
        }
        return packages;
    }

So is there any better solution to remember the position where a row was moved so the next time I start the app the row will be placed in that same location in the adapter?

Aucun commentaire:

Enregistrer un commentaire