gpt4 book ai didi

java - 从 SQLite 过滤记录以显示在列表中

转载 作者:行者123 更新时间:2023-12-01 18:05:41 25 4
gpt4 key购买 nike

我在SQLite数据库中有一些数据,我想在基于小逻辑的List中显示这些数据。

我的表中有三个字段,

first field : id
second field : title
third field : type

其中类型可以是:每月每年

这是我的代码:

    // Generate real data for each item
public List<ReminderItem> generateData(int count) {
ArrayList<ReminderItem> items = new ArrayList<>();

// Get all reminders from the database
List<Reminder> reminders = rb.getAllReminders();

// Initialize lists
List<String> Titles = new ArrayList<>();
List<String> Type = new ArrayList<>();
List<Integer> IDList= new ArrayList<>();

// Add details of all reminders in their respective lists
for (Reminder r : reminders) {
Titles.add(r.getTitle());
Type.add(r.getType());
IDList.add(r.getID());
}
return items;
}

在这种情况下,我需要你们的帮助,正如你们所看到的,我正在显示从数据库到列表的每一条每条记录,无论它所属的类型

现在,我只想在列表中显示记录,该列表仅属于“每月”类型

那么我到底需要做什么?如何仅在列表中显示“每月”类型的记录

我有一个 String 变量,即 strType = "Monthly";

最佳答案

您有两种选择来执行此操作。

First is directly get only those record from database which reminder type is "Monthly" by execute below sql statement.

SELECT id,title,type FROM table_name WHERE type = "Monthly"

Second is check reminder type is equal to "Monthly" when you add data into List.

for (Reminder r : reminders) {

String reminderType = r.getType();
if(reminderType.equalsIgnoreCase(strType)) {

Titles.add(r.getTitle());
Type.add(reminderType);
IDList.add(r.getID());
}
}

完整代码

// Generate real data for each item
public List<ReminderItem> generateData(int count) {

String strType = "Monthly";

ArrayList<ReminderItem> items = new ArrayList<>();

// Get all reminders from the database
List<Reminder> reminders = rb.getAllReminders();

// Initialize lists
List<String> Titles = new ArrayList<>();
List<String> Type = new ArrayList<>();
List<Integer> IDList= new ArrayList<>();

// Add details of all reminders which type="Monthly" in their respective lists
for (Reminder r : reminders) {

String reminderType = r.getType();
if(reminderType.equalsIgnoreCase(strType)) {

Titles.add(r.getTitle());
Type.add(reminderType);
IDList.add(r.getID());
}
}
return items;
}

关于java - 从 SQLite 过滤记录以显示在列表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36623197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com