我已将数据存储在SQLite
数据库中。我需要在RecyclerView
中显示数据库中的数据。但是,第一次在RecyclerView中检索并显示数据时,它会按存储在数据库中的Sequence顺序检索并在Recyclerview上进行设置。当我尝试从中获取相同的数据时数据库并在RecyclerView上设置其自动更改序列。
这是从SQLite
数据库
public ArrayList<ReservationEntry> getDbReservation(){
SQLiteDatabase db = this.getWritableDatabase();
String query="SELECT * FROM "+ RESERVATIONS;
Cursor c=db.rawQuery(query,null,null);
ArrayList<ReservationEntry> reserveList=new ArrayList<>();
if(c.moveToFirst()){
do{
ReservationEntry entry=new ReservationEntry();
entry.setName(c.getString(0));
entry.setNoOfPeople(c.getString(1));
entry.setTime(c.getString(2));
entry.setDate(c.getString(3));
entry.setBirthday(c.getString(4));
entry.setAniversary(c.getString(5));
reserveList.add(entry);
}while (c.moveToNext());
}
return reserveList;
}
这是设置RecyclerViewAdapter
private void readDatabaseReservation(final ArrayList<ReservationEntry> dbList) {
adapter=newReservationAdapter(dbList,getActivity().getApplicationContext());
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
}
如何解决这个问题?
答案 0 :(得分:0)
您必须添加适配器此功能才能更改数据并调用notifyDataSetChanged。
因此将其添加到您的适配器
public void setData(ArrayList<ReservationEntry> dbList){
this.yourListData = dbList;
notifyDataSetChanged();
}
并像这样使用它
private void readDatabaseReservation(final ArrayList<ReservationEntry> dbList) {
adapter.setData(dbList);
}
希望有帮助。有关更多信息,请添加您的适配器代码