是否有一些推荐的方法来为ParseObject创建ID以在RecyclerView中使用?

tac*_*lux 4 android parse-platform recycler-adapter android-recyclerview

我正在使用Parse对象ID创建一个哈希码,但是显然这有一些小的爆炸潜力。

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

    ...

    @Override
    public long getItemId(int position) {
        return mObjects.get(position).getObjectId().hashCode();
    }
}
Run Code Online (Sandbox Code Playgroud)

Xav*_*ier 5

Not specific to Parse, but you could create stable ids easily from the object id:

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());
    }
}
Run Code Online (Sandbox Code Playgroud)

(on a side note, do you need to implement getItemId? If your dataset is not changing, you don't have to)