0

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>
5
  • 1
    I dont understand what you want to sort? explain better please Commented Apr 11, 2013 at 12:27
  • The last argument of the query method of SQLiteDatabase holds the "orderBy" parameter of the SQL function. More infos here: developer.android.com/reference/android/database/sqlite/… Commented Apr 11, 2013 at 12:43
  • Ah sorry Tenhouse, I want to sort them by day, and put the Time / duration / description under the correct textview day Commented Apr 11, 2013 at 13:20
  • But how will i be able to use 'OrderBy day' to separate the rows to the individual day textviews. Commented Apr 11, 2013 at 13:34
  • So for the only way i think i can do it is by creating multiple RetrieveAllAppointments and just change them to 'RetrieveMondayAppointments' / 'RetrieveTuesdayAppointments' ect ect and just filter out all the days that are not monday or tuesday or w/e Commented Apr 11, 2013 at 13:36

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

4 Comments

So, i have to sort it like appointments[i].day and then change the text values of the textviews from the retrieveAllAppointments method? Because i can only return 1 value or 1 list and not sure how to sort it once its been returned.
Well as far as I understood you return all the appointments from the DB. And an object of type Appointment should have a property Appointment.Day. And yes, I am saying that you could try to sort it by appointments[i].day and then decide where to send each objects on your layout.
Hey man, you able to show me some example code and where i need to put it? Cant get this working sorry.
Check the update, I've posted a piece of code, hope it helps you.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.