我正在尝试使用FirebaseRecyclerAdapter在回收器视图中从Firebase实时数据库检索数据。但是,每次尝试时,都会出现如下错误:
E / SQLiteDatabase:插入错误 android.database.sqlite.SQLiteConstraintException:唯一约束失败:pending_ops.tag,pending_ops.target_class,pending_ops.target_package,pending_ops.user_id(代码2067) 在android.database.sqlite.SQLiteConnection.nativeExecuteForLastInsertedRowId(本地方法) 在android.database.sqlite.SQLiteConnection.executeForLastInsertedRowId(SQLiteConnection.java:782) 在android.database.sqlite.SQLiteSession.executeForLastInsertedRowId(SQLiteSession.java:788) 在android.database.sqlite.SQLiteStatement.executeInsert(SQLiteStatement.java:86) 在android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1473) 在android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1341) 在aose.a(:com.google.android.gms @ 201817013 @ 20.18.17(020700-311416286):78) 在aort.b(:com.google.android.gms @ 201817013 @ 20.18.17(020700-311416286):11) 在aort.a(:com.google.android.gms @ 201817013 @ 20.18.17(020700-311416286):100) 在aooq.run(:com.google.android.gms @ 201817013 @ 20.18.17(020700-311416286):11) 在rxf.b(:com.google.android.gms @ 201817013 @ 20.18.17(020700-311416286):12) 在rxf.run(:com.google.android.gms @ 201817013 @ 20.18.17(020700-311416286):7) 在java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112) 在java.util.concurrent.ThreadPoolExecutor $ Worker.run(ThreadPoolExecutor.java:587) 在scz.run(:com.google.android.gms @ 201817013 @ 20.18.17(020700-311416286):0) 在java.lang.Thread.run(Thread.java:818)
这是我的模型课:
int date;
int value;
public datapoint() {
}
public datapoint(int date, int value) {
this.date = date;
this.value = value;
}
public int getDate() {
return date;
}
public int getValue() {
return value;
}
}
这是我的回收站视图适配器类:
private List<datapoint> datapointList;
public MyAdapter(List<datapoint> datapointList){
this.datapointList = datapointList;
}
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from ( parent.getContext ()).inflate(R.layout.list_data,parent,false) ;
return new ViewHolder ( view );
}
@Override
public void onBindViewHolder(@NonNull MyAdapter.ViewHolder holder, int position) {
datapoint dp = datapointList.get ( position );
holder.txtdate.setText ( dp.getDate () );
holder.txtvalue.setText ( dp.getValue () );
}
@Override
public int getItemCount() {
return datapointList.size ();
}
public class ViewHolder extends RecyclerView.ViewHolder {
private TextView txtdate,txtvalue;
public ViewHolder(@NonNull View itemView) {
super ( itemView );
txtdate = (TextView) itemView.findViewById ( R.id.datetxt );
txtvalue = (TextView) itemView.findViewById ( R.id.valuetxt );
}
}
}
这就是我从Firebase检索数据的方式:
private List<datapoint> datapointList;
private RecyclerView rv;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.activity_main2 );
rv = (RecyclerView)findViewById ( R.id.myRecyclerview);
rv.setHasFixedSize ( true );
rv.setLayoutManager ( new LinearLayoutManager ( this ) );
datapointList= new ArrayList<> ();
final DatabaseReference nm = FirebaseDatabase.getInstance ().getReference().child ("fieldX").child ( "temperature" );
nm.addListenerForSingleValueEvent ( new ValueEventListener () {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.exists ()){
for(DataSnapshot npsnapshot : dataSnapshot.getChildren ()){
datapoint d = npsnapshot.getValue (datapoint.class);
datapointList.add ( d );
}
adapter = new MyAdapter ( datapointList );
rv.setAdapter ( adapter );
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
} );
}
}
我是新手,为什么会出现SQL错误?我什至没有使用它。我的代码有问题吗?