0

我需要显示多个数据库表来分隔文本视图。

所以我需要从表中提取所有“约会”并将它们排序以显示在 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();

预约资料来源:

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>
4

1 回答 1

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
  }

应该是这样的。

于 2013-04-11T14:39:00.047 回答