user2253722
user2253722

Reputation: 25

How to sort results on android from SQLite?

i need to display multiple database tables to seperate textviews.

So i need to pull all 'appointments' from a table and sort them to display in separate textviews on the mainActivity such as txtMonday, txtTuesday, txtWednesday

The database is designed to store the day along with the other details:

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);";

This is how i attempt to call it through MainActivity: (I also will be calling it with 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;        
}

Also for the days, i used radio buttons to choose between monday / tue / wed / thur / fri so i store the day with :

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();
      }

    });

and the XML/strings for them:

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

Upvotes: 0

Views: 117

Answers (1)

Cosmin Ionascu
Cosmin Ionascu

Reputation: 7578

In your datamodel you should have a class that manipulates the Appointments, so when you retrieve all your appointments from the database just filter them by appointments[i].Day, or something like that, based on how your Appointment class is created. You don't need to explicitly create different DB selects for each of them.

  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
  }

Should be something like this.

Upvotes: 1

Related Questions