2013-04-11 98 views
0

我需要显示多个数据库表来分隔文字浏览。如何从SQLite对android进行排序?

,所以我需要从表中把所有“约会”和排序他们在MainActivity如txtMonday,txtTuesday,txtWednesday

数据库设计白天存储与其他一起在单独textviews显示细节:

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

我不明白你想要排序?请更好地解释 – 2013-04-11 12:27:39

+0

SQLiteDatabase的查询方法的最后一个参数包含SQL函数的“orderBy”参数。更多的信息在这里:http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html – AlexVogel 2013-04-11 12:43:16

+0

对不起Tenhouse,我想整理一天,并把时间/时长/描述放在正确的文本视图下天 – user2253722 2013-04-11 13:20:25

回答

1

在你的数据模型,你应该有一个操纵约会一类,所以当你检索所有约会从数据库中仅仅根据您的Appointment类的创建方式,将它们过滤为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 
    } 

应该是这样的。

+0

,我不得不像约会[i] .day那样对它进行排序,然后从retrieveAllAppointments方法中更改textview的文本值?因为我只能返回1个值或1个列表,并且不确定如何在返回后对其进行排序。 – user2253722 2013-04-12 03:41:12

+0

那么据我了解,你从数据库返回所有的约会。而'Appointment'类型的对象应该有一个属性'Appointment.Day'。是的,我的意思是说你可以尝试按'约会[i] .day'排序,然后决定将布局中的每个对象发送到哪里。 – 2013-04-12 06:41:07

+0

嘿,你能告诉我一些示例代码,我需要把它放在哪里?无法让这个工作很抱歉。 – user2253722 2013-04-12 12:10:37