谢谢您的帮助,希望我的解释足够清楚。
一段时间以来,我一直在为REST(恢复,教育,支持,培训)中心开发此应用程序,而且我真的很接近完成该功能。此应用程序使用MVC架构构建,因此存在明显的关注点分离。我想在我的国家开放之前准备好它。
当我查看日志时,它显示数据,但是当它显示数据时,它只是最后一个条目。我如何才能仅将最后三个条目,四个条目等显示在TextView小部件中?
这是我尝试过的:
FridayActivity从数据库中调用方法以在视图中显示附加到布局的字符串
@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());
}
在FridayAcvity中从数据库调用的第一个方法
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());
返回“星期五”而不是“每天反思//今天就是”,那真是太棒了。
答案 0 :(得分:1)
下面描述的不是评论而是完整的答案。但我希望这些评论能帮助您解决问题:
for(Groups groups: groupsList)
{
fridayGroupNameOne.setText(groups.getGroupName());
....
}
您一直都在覆盖TextView的文本。如果列表中有3个项目-在第一次循环迭代中,您的代码将第一个项目的组名放在该列表中,而在最后一个迭代中-将列表项的组名放在此处。这就是为什么您的TextViews仅包含数组中最后一项的文本。您的日志显示了所有三个项目,这与您的代码相对应。因此,您应该在其中更改循环以实现所需的功能(我认为这不是一个非常复杂的任务)。但是也许您应该阅读我的所有注释并完全更改代码的这一部分。
fridayGroupNameOne.setText(groups.getGroupName())
我认为返回“星期五”,而不是“每天反思//仅今天”,因为在代码的另一部分:
groups.setGroupName(cursor.getString(2));
您将groupName的索引设置为2。但是最好使用getColumnIndex显式获取此索引,因为您不能确定该字段在光标中的位置是2。