Android Studio 3.1.4,Java 1.8
我的pojo(第一个版本):
public class Product {
@PrimaryKey
private long id;
private long merchantId;
private String name;
// getter and setter
}
在我的RecyclerView.Adapter中:
@Override
public long getItemId(int index) {
Product product = (Product) getItem(index);
return product.getId();
}
这工作正常。 但是我这样更新我的pojo:
public class Product {
@PrimaryKey
private String uniqueId;
private long id;
private String name;
// geter and setter
}
现在我得到了错误:
E/AndroidRuntime( 3937): FATAL EXCEPTION: main
java.lang.IllegalStateException: Two different ViewHolders have the same stable ID. Stable IDs in your adapter MUST BE unique and SHOULD NOT change.
ViewHolder 1:ViewHolder{42b085c8 position=1 id=45, oldPos=-1, pLpos:-1 not recyclable(1)}
View Holder 2:ViewHolder{431beef0 position=0 id=45, oldPos=-1, pLpos:-1} android.support.v7.widget.RecyclerView{4250a110 VFED.V.. ......ID 0,0-720,866 #7f090214 app:id/sortRecyclerView}, adapter:com.myprojectui.adapter.PreviewShoppingBaseSortAdapter@430d1f80, layout:android.support.v7.widget.GridLayoutManager@42f4fe70, context:myproject.customer.ui.MainActivity@421f4460
at android.support.v7.widget.RecyclerView.handleMissingPreInfoForChangeError(RecyclerView.java:3826)
at android.support.v7.widget.RecyclerView.dispatchLayoutStep3(RecyclerView.java:3751)
at android.support.v7.widget.RecyclerView.dispatchLayout(RecyclerView.java:3421)
at android.support.v7.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:1699)
at android.support.v7.widget.RecyclerView$1.run(RecyclerView.java:346)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:749)
at android.view.Choreographer.doCallbacks(Choreographer.java:562)
at android.view.Choreographer.doFrame(Choreographer.java:531)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:735)
at android.os.Handler.handleCallback(Handler.java:730)
现在,我的私钥是字段 uniqueId ,其类型为 String 。 id 字段不再唯一。 uniqueId 字段是唯一的。
答案 0 :(得分:0)
请确保您尚未在分配给您的回收站视图的适配器上设置YourAdapter.setHasStableIds(true)
。