0

我有两个表LectureTimeTableLectureDayTable这些表的结构如下: _ID | start_time | end_time | location | course_ID_FK_ID | class_Day | classTime_ID_FK | course_ID_FK分别,我创建这些表如下:

创建 LectureTimeTable:

public static final String _ID = "_id";
public static final String COL_CLASS_START_TIME = "startTime";
public static final String COL_CLASS_END_TIME = "endTime";
public static final String COL_CLASS_LOCATION = "loc";
public static final String COL_COURSE_ID_FK = "cls_main_CourseId_fk";

public static final String TABLE_NAME = "lectureTimeTable";

// query for creating the TABLE_CLASS_DAYS_MAIN table.
public static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME
        + " (" + _ID + " INTEGER, " + COL_CLASS_START_TIME
        + " TEXT NOT NULL, " + COL_CLASS_END_TIME + " TEXT NOT NULL, "
        + COL_CLASS_LOCATION + " TEXT NOT NULL, " + COL_COURSE_ID_FK
        + " INTEGER, " + "FOREIGN KEY (" + COL_COURSE_ID_FK
        + ") REFERENCES " + Course.TABLE_NAME + " (" + Course._ID + "), "
        + " PRIMARY KEY (" + _ID + ", " + COL_COURSE_ID_FK + "));";

创建 LectureDayTable:

public static final String _ID = "_id";
public static final String COL_CLASS_DAY = "classDay";
public static final String COL_CLASS_TIME_ID_FK = "classTime_id_fk";
public static final String COL_COURSE_ID_FK = "course_id_fk";

public static final String TABLE_NAME = "LectureDayTable";

// query for creating the TABLE_CLASS_DAYS_MAIN table.
public static final String TABLE_CREATE = "CREATE TABLE " + TABLE_NAME
        + " (" + _ID + " INTEGER, " + COL_CLASS_DAY + " INTEGER NOT NULL, "
        + COL_CLASS_TIME_ID_FK + " INTEGER, " + COL_COURSE_ID_FK
        + " INTEGER, " + "FOREIGN KEY (" + COL_COURSE_ID_FK + ", "
        + COL_CLASS_TIME_ID_FK + ") REFERENCES "
        + CourseLectureTime.TABLE_NAME + " ("
        + CourseLectureTime.COL_COURSE_ID_FK + ", " + CourseLectureTime._ID
        + "), " + "PRIMARY KEY (" + _ID + ", " + COL_CLASS_TIME_ID_FK
        + ", " + COL_COURSE_ID_FK + "));";

现在我在 ListFragment 中显示此信息,我在其中显示我设计的课程的时间表,ListItem如图所示,我正在显示Location(in the picture bellow "se"),start_timeend_timefrom theLectureTimeTable并且当时我正在显示所有days来自LectureDayTable(in the following picture "Su" and "Mo")

ListFragment 的 Llist 项

我不知道我的方法是否不好,但我已经通过以下代码这样做了CursorAdapter

public class ScheduleListAdapter2 extends CursorAdapter {
private TextView classLocationTV;
private TextView classTimeTV;
private TextView sundayTV;
private TextView mondayTV;
private TextView tuesdayTV;
private TextView wednesdayTV;
private TextView thursdayTV;
private TextView fridayTV;
private TextView saturdayTV;
private ContentResolver mContentResolver;

private long timeID = 0;
private long courseId;
private AsyncQueryHandler mAsyncQueryHandler;
private Context context;

public ScheduleListAdapter2(Context context, Cursor c, boolean autoRequery,
        ContentResolver cr, long courseID) {
    super(context, c, autoRequery);
    mContentResolver = cr;
    this.courseId = courseID;
    this.context = context;
    Log.i(MyConstants.LOG_TAG,
            "--------------- ScheduleListAdapter constructor called-------------------");
    // TODO Auto-generated constructor stub
}

@Override
public View newView(Context context, Cursor mCursor, ViewGroup parent) {
    // when the view will be created for first tim*e,
    // we need to tell the adapters, how each item will look
    LayoutInflater inflater = LayoutInflater.from(parent.getContext());
    View retView = inflater.inflate(R.layout.listitem_schedule, parent,
            false);

    Log.i(MyConstants.LOG_TAG,
            "--------------- ScheduleListAdapter newView called-------------------");
    return retView;
}

@Override
public void bindView(View view, Context context, Cursor mCursor) {
    // TODO Auto-generated method stub

    Log.i(MyConstants.LOG_TAG,
            "--------------- ScheduleListAdapter bindView called-------------------");

    classLocationTV = (TextView) view.findViewById(R.id.locationTV);
    classLocationTV.setText(mCursor.getString(mCursor
            .getColumnIndex(CourseLectureTime.COL_CLASS_LOCATION)));

    classTimeTV = (TextView) view.findViewById(R.id.lectureTimeTV);
    classTimeTV.setText(mCursor.getString(mCursor
            .getColumnIndex(CourseLectureTime.COL_CLASS_START_TIME))
            + "-"
            + mCursor.getString(mCursor
                    .getColumnIndex(CourseLectureTime.COL_CLASS_END_TIME)));

    timeID = mCursor.getLong(mCursor.getColumnIndex(CourseLectureTime._ID));
    Log.i(MyConstants.LOG_TAG, "--------------- GOT THE TIME ID: " + timeID);

    sundayTV = (TextView) view.findViewById(R.id.sundayTV);
    mondayTV = (TextView) view.findViewById(R.id.mondayTV);
    tuesdayTV = (TextView) view.findViewById(R.id.tuesdayTV);
    wednesdayTV = (TextView) view.findViewById(R.id.wednesdayTV);
    thursdayTV = (TextView) view.findViewById(R.id.thursdayTV);
    fridayTV = (TextView) view.findViewById(R.id.fridayTV);
    saturdayTV = (TextView) view.findViewById(R.id.saturdayTV);

    mAsyncQueryHandler = new AsyncQueryHandler(mContentResolver) {

        private long mDay;

        @Override
        protected void onQueryComplete(int token, Object cookie,
                Cursor cursor) {
            super.onQueryComplete(token, cookie, cursor);

            Log.i(MyConstants.LOG_TAG,
                    "Returned lecture days: " + cursor.getCount());

            while (cursor.moveToNext()) {
                mDay = cursor.getLong(cursor
                        .getColumnIndex(CourseLectureDay.COL_CLASS_DAY));

                if (mDay == 0) {
                    sundayTV.setTextAppearance(
                            ScheduleListAdapter2.this.context,
                            R.style.enabledDayStyle);
                    sundayTV.setBackgroundResource(R.drawable.enabled_day_background);
                } else if (mDay == 1) {
                    mondayTV.setTextAppearance(
                            ScheduleListAdapter2.this.context,
                            R.style.enabledDayStyle);
                    mondayTV.setBackgroundResource(R.drawable.enabled_day_background);
                } else if (mDay == 2) {
                    tuesdayTV.setTextAppearance(
                            ScheduleListAdapter2.this.context,
                            R.style.enabledDayStyle);
                    tuesdayTV
                            .setBackgroundResource(R.drawable.enabled_day_background);
                } else if (mDay == 3) {
                    wednesdayTV.setTextAppearance(
                            ScheduleListAdapter2.this.context,
                            R.style.enabledDayStyle);
                    wednesdayTV
                            .setBackgroundResource(R.drawable.enabled_day_background);
                } else if (mDay == 4) {
                    thursdayTV.setTextAppearance(
                            ScheduleListAdapter2.this.context,
                            R.style.enabledDayStyle);
                    thursdayTV
                            .setBackgroundResource(R.drawable.enabled_day_background);
                } else if (mDay == 5) {
                    fridayTV.setTextAppearance(
                            ScheduleListAdapter2.this.context,
                            R.style.enabledDayStyle);
                    fridayTV.setBackgroundResource(R.drawable.enabled_day_background);
                } else if (mDay == 6) {
                    saturdayTV.setTextAppearance(
                            ScheduleListAdapter2.this.context,
                            R.style.enabledDayStyle);
                    saturdayTV
                            .setBackgroundResource(R.drawable.enabled_day_background);
                }

            }

        }
    };

    mAsyncQueryHandler
            .startQuery(
                    1,
                    new Object(),
                    CourseLectureDay.CONTENT_URI,
                    new String[] { CourseLectureDay.COL_CLASS_DAY },
                    CourseLectureDay.COL_CLASS_TIME_ID_FK + " = ? AND "
                            + CourseLectureDay.COL_COURSE_ID_FK + " = ? ",
                    new String[] { String.valueOf(timeID),
                            String.valueOf(courseId) }, null);

    }
}

通过给出房间位置开始时间结束时间一周中的几天,即星期一、星期二等来添加时间表(如果您有更好的想法,请建议我

问题是它有时没有在 FirstList 项目中显示正确的天数或ListItem显示最后一个 ListItem 的天数。我不知道我在这里做错了什么,请建议我更好的方法来实现这一点:

谢谢

4

0 回答 0