【问题标题】:How to display a certain amount of entries from the database in TextViews如何在 TextViews 中显示一定数量的数据库条目
【发布时间】:2020-09-02 10:16:43
【问题描述】:

提前感谢您的帮助,希望我的解释足够清楚。

我一直在为 REST(恢复、教育、支持、培训)中心开发这个应用程序一段时间,并且我真的很接近完成该功能。这个应用程序是用 MVC 架构构建的,因此有明确的关注点分离。我希望在我的州开放时准备好它。

当我查看日志时,它会显示数据,但当它显示数据时,它只是最后一个条目。如何仅在 TextView 小部件中显示最后三个条目、四个条目等?

这是我尝试过的:

Frid​​ayActivity 调用数据库中的方法以在视图中显示附加到布局的字符串

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_friday);
        DatabaseHandler db = new DatabaseHandler(FridayActivity.this);
        groupsArrayList = new ArrayList<>();
        TextView fridayGroupNameOne = findViewById(R.id.friday_group_name_one);
        TextView fridayGroupStartTimeOne = findViewById(R.id.friday_group_start_time_one);
        TextView fridayGroupEndTimeOne = findViewById(R.id.friday_group_end_time_one);
        TextView fridayGroupNameTwo = findViewById(R.id.friday_group_name_two);
        TextView fridayGroupStartTimeTwo = findViewById(R.id.friday_group_start_time_two);
        TextView fridayGroupEndTimeTwo = findViewById(R.id.friday_group_end_time_two);
        TextView fridayGroupNameThree = findViewById(R.id.friday_group_name_three);
        TextView fridayGroupStartTimeThree = findViewById(R.id.friday_group_start_time_three);
        TextView fridayGroupEndTimeThree = findViewById(R.id.friday_group_end_time_three);

        db.fridayAddGroup(new Groups(0,"Friday","Daily reflection//Just for today", "9:00 AM", "10:00 AM"));
        db.fridayAddGroup(new Groups(1,"Friday","Step//Sponsorship", "12:00 PM", "1:00 PM"));
        db.fridayAddGroup(new Groups(2,"Friday","Re entry", "2:00 PM", "3:00 PM"));

        /* List all Groups and set to TextViews */
        List<Groups> groupsList = db.getAllFridayGroups();
        for(Groups groups: groupsList) {
            Log.d("friday_activity", "onCreate: "+groups.getGroupName() + " , "
                    +groups.getGroupStartTime() + " , "
                    +groups.getGroupEndTime());
            fridayGroupNameOne.setText(groups.getGroupName());
            fridayGroupEndTimeOne.setText(groups.getGroupStartTime());
            fridayGroupStartTimeOne.setText(groups.getGroupEndTime());
            fridayGroupNameTwo.setText(groups.getGroupName());
            fridayGroupStartTimeTwo.setText(groups.getGroupStartTime());
            fridayGroupEndTimeTwo.setText(groups.getGroupEndTime());
            fridayGroupNameThree.setText(groups.getGroupName());
            fridayGroupStartTimeThree.setText(groups.getGroupStartTime());
            fridayGroupEndTimeThree.setText(groups.getGroupEndTime());
            groupsArrayList.add(groups);
        }
        Log.d("friday_contact_count", "onCreate: " +db.getAllFridayGroups());
    }

Frid​​ayAcvity 中从数据库调用的第一个方法

public void fridayAddGroup(Groups groups) {
        SQLiteDatabase db = this.getWritableDatabase();
        ContentValues values = new ContentValues();

        values.put(DBUtil.KEY_GROUP_NAME,groups.getGroupName());
        values.put(DBUtil.KEY_GROUP_DAY,groups.getGroupDay());
        values.put(DBUtil.KEY_START_TIME,groups.getGroupStartTime());
        values.put(DBUtil.KEY_END_TIME,groups.getGroupEndTime());

        // Insert Row
        db.insert(DBUtil.FRIDAY_TABLE,null,values);
        Log.d("DB handler", "addContact: " + "item added");
        db.close();
    }

从数据库调用的第二个方法,用游标遍历数据

public List<Groups> getAllFridayGroups() {
        List<Groups> groupsList = new ArrayList<>();
        SQLiteDatabase db = this.getReadableDatabase();

        // Select all contacts from the database table
        String selectAll = "SELECT * FROM " + DBUtil.FRIDAY_TABLE;
        Cursor cursor = db.rawQuery(selectAll,null);

        // loop through the data
        if (cursor.moveToFirst()) {
            do {
                Groups groups = new Groups();
                groups.setId(Integer.parseInt(cursor.getString(0)));
                groups.setGroupDay(cursor.getString(1));
                groups.setGroupName(cursor.getString(2));
                groups.setGroupStartTime(cursor.getString(3));
                groups.setGroupEndTime(cursor.getString(4));

                // add contact objects to our list
                groupsList.add(groups);
            }while (cursor.moveToNext());
        }
        cursor.close();
        return groupsList;
    }

模型的类、字段变量和构造函数

public class Groups {
    private int id;
    private String groupDay;
    private String groupName;
    private String groupStartTime;
    private String groupEndTime;

    public Groups(int id, String groupDay, String groupName, String groupStartTime, String groupEndTime) {
        this.id = id;
        this.groupDay = groupDay;
        this.groupName = groupName;
        this.groupStartTime = groupStartTime;
        this.groupEndTime = groupEndTime;
    }

    public Groups() {
    }

SQL 架构

String FRIDAY_GROUP_TABLE = "CREATE TABLE " + DBUtil.FRIDAY_TABLE + "("
                + DBUtil.KEY_ID + " INTEGER PRIMARY KEY,"
                + DBUtil.KEY_GROUP_DAY + " TEXT,"
                + DBUtil.KEY_GROUP_NAME + " TEXT,"
                + DBUtil.KEY_START_TIME + " TEXT,"
                + DBUtil.KEY_END_TIME + " TEXT);";

表值

    public static final int DATABASE_VERSION = 1;
    public static final String DATABASE_NAME = "rest_db";
    public static final String FRIDAY_TABLE = "friday_table";
    public static final String KEY_ID = "id";
    public static final String KEY_GROUP_NAME = "group_name";
    public static final String KEY_GROUP_DAY = "group_day";
    public static final String KEY_START_TIME = "start_time";
    public static final String KEY_END_TIME =  "end_time";

这是 FridayActivity 内 Log.d 调用的 logcat 输出:

2020-05-16 10:42:26.539 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 12:00 PM , 1:00 PM
2020-05-16 10:42:26.539 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 12:00 PM , 1:00 PM
2020-05-16 10:42:26.540 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 12:00 PM , 1:00 PM
2020-05-16 10:42:26.540 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 12:00 PM , 1:00 PM
2020-05-16 10:42:26.540 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 12:00 PM , 1:00 PM
2020-05-16 10:42:26.540 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 12:00 PM , 1:00 PM
2020-05-16 10:42:26.540 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 12:00 PM , 1:00 PM
2020-05-16 10:42:26.540 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 12:00 PM , 1:00 PM
2020-05-16 10:42:26.540 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 12:00 PM , 1:00 PM
2020-05-16 10:42:26.540 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 9:00 AM , 10:00 AM
2020-05-16 10:42:26.540 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 12:00 PM , 1:00 PM
2020-05-16 10:42:26.541 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 2:00 PM , 3:00 PM
2020-05-16 10:42:26.541 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 9:00 AM , 10:00 AM
2020-05-16 10:42:26.541 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 12:00 PM , 1:00 PM
2020-05-16 10:42:26.544 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 2:00 PM , 3:00 PM
2020-05-16 10:42:26.544 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 9:00 AM , 10:00 AM
2020-05-16 10:42:26.544 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 12:00 PM , 1:00 PM
2020-05-16 10:42:26.544 3934-3934/com.wesleyruede.testfour D/friday_activity: onCreate: Friday , 2:00 PM , 3:00 PM

数据显示在视图中:

额外问题: 如果你能告诉我为什么打电话给fridayGroupNameOne.setText(groups.getGroupName()); 会返回“星期五”而不是“每日反思//只为今天”,那就太棒了。

【问题讨论】:

    标签: java android database sqlite model-view-controller


    【解决方案1】:

    下面描述的不是完整的答案而是 cmets。但我希望这些 cmets 能帮助您解决问题:

    1. 当你写作时:
    for(Groups groups: groupsList) 
       { 
         fridayGroupNameOne.setText(groups.getGroupName());
         ....
       }
    

    你一直在覆盖 TextViews 的文本。如果您的列表中有 3 个项目 - 在第一次循环迭代时,您的代码将第一个项目中的组名放在那里,在最后一次迭代中 - 列表项中的组。这就是为什么您的 TextViews 仅包含数组中最后一项的文本。您的日志显示了所有三个项目,这与您的代码相对应。所以你应该改变你的遍历循环来实现你想要的(我认为这不是一个非常复杂的任务)。但也许您应该阅读我所有的 cmets 并完全更改这部分代码。

    1. 您的代码
    fridayGroupNameOne.setText(groups.getGroupName())
    

    我认为返回“星期五”而不是“每日反思//仅适用于今天”,因为在您的代码的另一部分:

    groups.setGroupName(cursor.getString(2));
    

    您设置 groupName 的索引 = 2。但最好使用 getColumnIndex 显式获取此索引,因为您无法确定该字段在光标中的位置是否为 2。

    1. 在您的示例中,您硬编码了 3 组 TextView(在屏幕上可见)。如果您想修复此类数据的数量,那么可以。但是如果你想在屏幕上显示的数据量取决于你的数据库数据,那么你最好用RecyclerView替换一组TextViews。深入那里需要很长时间,但你可以看看这个选项。

    【讨论】:

    • 1) 哦,哇,我没有注意到覆盖是问题所在。如果我将每个元素添加到数组中,然后从数组中索引元素,例如 list[4] == "2:30",它会起作用吗?我也觉得这不是一项复杂的任务,只是我不知道如何实现。 2)这是有道理的。所以问题是我没有说清楚。隐式值可以是可变的。 3) 感谢您推荐 RecyclerView,我只使用了数据库中的有限数据。如果一天内出现超过 4 或 5 个组,我可能不得不转到 RecyclerView 以获得有效的可滚动视图。
    • 1.是的,类似的东西。实际上你不需要数组,你已经从 db 获得了一个列表(来自 db.getAllFridayGroups())。您可以从该数据结构中获取带有索引的项目。
    • 我尝试登录 groupsList.get(Integer.parseInt(groups.getGroupName())).toString() 并导致应用程序崩溃。我正在考虑访问将每个条目与给定值一起存储的 groupsList。
    • 我不确定你想用这段代码得到什么。您从数据库中获得“groupsList”。假设它的大小是 3。要获取此列表中第一个组的名称,您只需编写:groupList.get(0).getGroupName(),第二个 - groupList(1).getGroupName() 等等。所以为了得到这个,没有必要使用循环或使用“组”变量
    • 哇哦!我现在觉得自己好傻。我的逻辑能力并不总是最好的,所以难怪我不会这么直截了当。我正在考虑访问 groupList 中每个条目的组值。尽管您的努力我可以像您说的那样通过删除 for 循环来降低代码的复杂性。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多