android - 在 RecyclerView 中显示数据时,来自 SQLite 的数据序列发生了变化
问题描述
我已将数据存储在SQLite
数据库中。我需要从数据库中显示该数据RecyclerView
。但是第一次在 RecyclerView 中检索和显示数据时,它会在存储在数据库中的序列中检索并在 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();
}
如何解决这个问题?
解决方案
您必须添加您的适配器此函数来更改数据并调用 notifyDataSetChanged。
所以把它添加到你的适配器
public void setData(ArrayList<ReservationEntry> dbList){
this.yourListData = dbList;
notifyDataSetChanged();
}
并像这样使用它
private void readDatabaseReservation(final ArrayList<ReservationEntry> dbList) {
adapter.setData(dbList);
}
希望有帮助。更多添加您的适配器代码
推荐阅读
- sql - Oracle SQL/PLSQL:具有重复数据的分层递归查询
- reactjs - 样式化的组件 - 组件中的样式在我刷新之前不会应用。刷新一个组件会破坏另一个组件的样式
- javascript - 我正在尝试使用 facebook 进行身份验证,但收到错误 TokenError: Malformed auth code
- docker - 如何使用 Docker Registry API 提取有关容器的信息?未经授权
- c - 尝试使用字符串搜索随机访问文件
- reactjs - 如何在单元测试期间更新 React 组件道具?
- sql-server - 用户“NT AUTHORITY\ANONYMOUS LOGON”的 SQL Server 登录失败
- vue.js - 根据数组中某些值的存在有条件地渲染图标
- javascript - 如何从 chrome 开发控制台捕获错误?
- python - 当我尝试在 python 中合并两个数据帧时如何修复“内存错误”