我需要显示多个数据库表来分隔文本视图。
所以我需要从表中提取所有'约会'并对它们进行排序,以便在mainActivity的单独文本视图中显示,例如txtMonday,txtTuesday,txtWednesday
数据库旨在存储当天以及其他详细信息:
private static final String DATABASE_CREATE =
"create table " + TABLE_AP + "(" + COLUMN_ID + " integer primary key autoincrement, "
+ COLUMN_DAY + " text not null, "
+ COLUMN_TIME + " text not null, "
+ COLUMN_DURATION + " text not null, "
+ COLUMN_DESCRIPTION + " text not null);";
这是我尝试通过MainActivity调用它的方式: (我也将用onCreate调用它)
public void onResume (){
APData = new AppointmentDataSource(this);
APData.open();
List<Appointment> appointments = APData.retrieveAllAppointments();
APData.close();
AppointmentDataSource:
public List<Appointment> retrieveAllAppointments () {
List<Appointment> appointments = new ArrayList<Appointment>();
Cursor cursor = database.query(MySQLiteHelper.TABLE_AP, , null, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
Appointment ap = cursorToBk(cursor);
appointments.add(ap);
cursor.moveToNext();
}
cursor.close();
return appointments;
}
同样在这些日子里,我使用单选按钮在星期一/星期二/星期五/星期四/星期五之间进行选择 所以我把这一天存储在:
createButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
findRadioGroup = (RadioGroup) findViewById(R.id.radioDay);
int selectedId = findRadioGroup.getCheckedRadioButtonId();
radioButton = (RadioButton) findViewById(selectedId);
String day=radioButton.getText().toString();
String time=txtTime.getText().toString();
String duration=txtDuration.getText().toString();
String description=txtDescription.getText().toString();
APData.insert(day, time, duration, description);
APData.close();
finish();
}
});
以及它们的XML /字符串:
<string name="RadioMon">Mon</string>
<string name="RadioTue">Tue</string>
<string name="RadioWed">Wed</string>
<string name="RadioThu">Thur</string>
<string name="RadioFri">Fri</string>
答案 0 :(得分:1)
在您的数据模型中,您应该有一个操作约会的类,因此当您从数据库中检索所有约会时,只需按appointments[i].Day
或类似的方式过滤它们,具体取决于您的约会类的创建方式。您不需要为每个选择显式创建不同的数据库选择。
public void onResume (){
APData = new AppointmentDataSource(this);
APData.open();
List<Appointment> appointments = APData.retrieveAllAppointments();
APData.close();
TextView tvMonday = (TextView)findViewById(R.id.tvMonday);
TextView tvTuesday = (TextView)findViewById(R.id.tvTuesday);
... (all your days textViews).
for(Iterator<Appointment> i = appointments.iterator(); i.hasNext();){
Appointment item = i.next();
if(item.Day.equals("Monday") tvMonday.append(item.ToString());
//same for the rest of your textViews
}
应该是这样的。