Is there a recommended way to create an identifier for ParseObject for use in RecyclerView?

I am creating a hash code using the Parse object id, but obviously it has little potential to explode.

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<MyParseObject> mObjects;

    ...

    @Override
    public long getItemId(int position) {
        return mObjects.get(position).getObjectId().hashCode();
    }
}
+4
source share
2 answers

Not defined for Parse, but you can easily create persistent identifiers from an object identifier:

public class StableNumericalIdProvider {
    private int idProvider;
    private final Map<String, Integer> numericalIds = new HashMap<>();

    public int id(String stringId) {
        Integer numericalId = numericalIds.get(stringId);
        if (numericalId == null) {
            numericalId = idProvider++;
            numericalIds.put(stringId, numericalId);
        }
        return numericalId;
    }
}

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {
    private List<MyParseObject> mObjects;

    private StableNumericalIdProvider idProvider;

    @Override
    public long getItemId(int position) {
        return idProvider.id(mObjects.get(position).getObjectId());
    }
}

(on the side of the note, do you need to implement getItemId? If your dataset does not change, you do not need to)

+4
source

Why do you need to provide identifiers?

setHasStableIds(true) recyclerview () getItemId, :

public int getItemId(int position) {
    return position;
}
0

Source: https://habr.com/ru/post/1648032/


All Articles