SQLite 数据库通过适配器获取所有数据到 Recyclerview。(寻找首选项)

Posted

技术标签:

【中文标题】SQLite 数据库通过适配器获取所有数据到 Recyclerview。(寻找首选项)【英文标题】:SQLite database fetch all data to Recyclerview via adapter.(looking for preferences) 【发布时间】:2019-08-02 10:35:51 【问题描述】:

我正在使用 SQLite 数据库和带有适配器的 Recyclerview。 我的问题是,我如何将 SQLite 数据库中的数据转换/解析为 JSON(我喜欢将所有数据存储到我的构造函数并通过适配器获取,然后使用 Recyclerview 显示适配器)。

ps。其他详情请咨询我,非常感谢:)

无论如何,这是我的代码:

我的代码:

               //sqliteselectReader
               DatabaseHelper databaseHelper = new 
               DatabaseHelper(getContext());
                SQLiteDatabase db = databaseHelper.getWritableDatabase();
                Cursor c=databaseHelper.selectDisplaySchedule(db);


 my recyclerview binds with adapter:

              List<ScheduleDisplayDetails> get ="**my problem is here**"//I would like to fetch my data from database here on my getter setter. 


                        layoutManager = new 
              LinearLayoutManager(getContext());
                        RecyclerView recyclerView = inflatedView.
                        findViewById(R.id.scheduleRecycler);
                        recyclerView.setLayoutManager(layoutManager);
                        recyclerView.addItemDecoration(new 
              DividerItemDecoration(getContext(), 
              LinearLayoutManager.VERTICAL));
                        ScheduleModuleAdapter recyclerViewAdapter =
                        new ScheduleModuleAdapter(get);

                        recyclerView.setAdapter(recyclerViewAdapter);

我的数据库查询:

    public Cursor selectDisplaySchedule(SQLiteDatabase db) 
    String sql = "SELECT ScheduleId, ScheduleLocation, ScheduleLatitude, 
    ScheduleLongitude, ScheduleExpectedTimeOfTransaction, 
    ScheduleEstimatedTimeOfDeparture, ScheduleStatus from tFleetSchedule;";
    return (db.rawQuery(sql, null));

这些是我想要存储的数据(我已经通过 sqlite 浏览器打开,抱歉其他数据是机密的):

这是我的 GetterSetter:

public class ScheduleDisplayDetails 

private String ID;
private String Location;
private String Latitude;
private String Longitude;
private String ExpectedTimeOfTransaction;
private String EstimatedTimeOfDeparture;
private String Status;

public ScheduleDisplayDetails(String ID, String location, String latitude, String longitude, String expectedTimeOfTransaction, String estimatedTimeOfDeparture, String status) 
    this.ID = ID;
    Location = location;
    Latitude = latitude;
    Longitude = longitude;
    ExpectedTimeOfTransaction = expectedTimeOfTransaction;
    EstimatedTimeOfDeparture = estimatedTimeOfDeparture;
    Status = status;


public String getID() 
    return ID;


public void setID(String ID) 
    this.ID = ID;


public String getLocation() 
    return Location;


public void setLocation(String location) 
    Location = location;


public String getLatitude() 
    return Latitude;


public void setLatitude(String latitude) 
    Latitude = latitude;


public String getLongitude() 
    return Longitude;


public void setLongitude(String longitude) 
    Longitude = longitude;


public String getExpectedTimeOfTransaction() 
    return ExpectedTimeOfTransaction;


public void setExpectedTimeOfTransaction(String expectedTimeOfTransaction) 
    ExpectedTimeOfTransaction = expectedTimeOfTransaction;


public String getEstimatedTimeOfDeparture() 
    return EstimatedTimeOfDeparture;


public void setEstimatedTimeOfDeparture(String estimatedTimeOfDeparture) 
    EstimatedTimeOfDeparture = estimatedTimeOfDeparture;


public String getStatus() 
    return Status;


public void setStatus(String status) 
    Status = status;

这是我的适配器:

   public class ScheduleModuleAdapter extends 
   RecyclerView.Adapter<ScheduleModuleAdapter.MyViewHolder> 

private List<ScheduleDisplayDetails> schedlist;

public class MyViewHolder extends RecyclerView.ViewHolder 
    public TextView location, schedule_status, id, expected, estimated;


    public MyViewHolder(View view) 
        super(view);
        location = view.findViewById(R.id.schedule_location);
        estimated = view.findViewById(R.id.schedule_estimated_time);
        schedule_status = view.findViewById(R.id.schedule_Status);
        expected = view.findViewById(R.id.schedule_expected_time);
        id = view.findViewById(R.id.schedule_ID);


    



public ScheduleModuleAdapter(List<ScheduleDisplayDetails> scheduleList) 
    this.schedlist = scheduleList;


@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) 
    View itemView = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.schedule_module_card_view, parent, false);

    return new MyViewHolder(itemView);


@Override
public void onBindViewHolder(MyViewHolder holder, int position) 
    ScheduleDisplayDetails sched = schedlist.get(position);
    holder.location.setText(sched.getLocation());
    holder.schedule_status.setText(sched.getStatus());
    holder.estimated.setText(sched.getEstimatedTimeOfDeparture());
    holder.expected.setText(sched.getExpectedTimeOfTransaction());
    holder.id.setText(sched.getID());

     


@Override
public int getItemCount() 
    return schedlist.size();

【问题讨论】:

【参考方案1】:

只需循环 tru Cursor,每次迭代创建每个对象并设置值:

//sqliteselectReader
               DatabaseHelper databaseHelper = new 
               DatabaseHelper(getContext());
                SQLiteDatabase db = databaseHelper.getWritableDatabase();
                Cursor c=databaseHelper.selectDisplaySchedule(db);

ArrayList<ScheduleDisplayDetails> get = new ArrayList<>(); initialize your list

while(c.moveToNext()) 
    ScheduleDisplayDetails details = new ScheduleDisplayDetails(); // you should create an empty constructor also on your object

    details.setID(c.getString(c.getColumnIndexOrThrow("ScheduleId")));
    details.setLocation(c.getString(c.getColumnIndexOrThrow("ScheduleLocation")));
    //set other details also....

    get.add(details); // add the object on your list


//then setup your recyclerview and adapter...

【讨论】:

谢谢各位,我是开发新手.. 也在尝试解决这个问题,但我不知道逻辑。顺便说一句,非常感谢:D 这是这个问题的最佳答案。 乐于助人:)

以上是关于SQLite 数据库通过适配器获取所有数据到 Recyclerview。(寻找首选项)的主要内容,如果未能解决你的问题,请参考以下文章

Android NullPointerException 检索 SQlite 数据以通过 SimpleCursorAdapter 列出 [重复]

Android - 添加到 SQLite 数据库后不会更新 ListView,除非重新启动应用程序

从 SQLITE 回收器视图获取图像路径错误

如何将自定义适配器添加到活动中以使列表出现在活动中?

设置模型类和自定义适配器以从 onListItemClick 获取 SQLite DB rowID

自定义适配器不显示数据