-1
我正在開發一個android項目,允許學生使用他們的android設備添加/刪除課程。我在在顯示按天排序的ListView中安排一個麻煩。我不能得到正確的TextView課程列表 以下是我迄今所做..格式化一個簡單的光標適配器
activity_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="3dp"
android:background="@drawable/list_item"
android:orientation="vertical" >
<TextView
android:id="@+id/txDay"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="sans-serif-light"
android:padding="10dp"
android:text="@string/day"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#E74C3C"
android:textSize="45sp"
android:textStyle="bold" />
<TextView
android:id="@+id/tv8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/tv8"
android:textColor="#F9F1F8" />
<TextView
android:id="@+id/tvCourseName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/course"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tv9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="@string/tv9"
android:textColor="#F9F1F8" />
<TextView
android:id="@+id/tvCourseName2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/course"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tv11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="@string/tv11"
android:textColor="#F9F1F8" />
<TextView
android:id="@+id/tvCourseName3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/course"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tv12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="@string/tv12"
android:textColor="#F9F1F8" />
<TextView
android:id="@+id/tvCourseName4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/course"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="@string/tv2"
android:textColor="#F9F1F8" />
<TextView
android:id="@+id/tvCourseName5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/course"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="@+id/tv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:text="@string/tv3"
android:textColor="#F9F1F8" />
<TextView
android:id="@+id/tvCourseName6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/course"
android:layout_marginBottom="5dp"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
activity_courses.xml
<?xml version="1.0" encoding="utf-8"?>
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/list_courses"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#2C3E50" >
</ListView>
我的數據庫
public class DBHelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Registration.db";
// Table SEM_COURSE
public static final String SEM_COURSE_TABLE_NAME = "sem_course";
public static final String SEM_COURSE_COLUMN_STD_ID = "stdID";
public static final String SEM_COURSE_COLUMN_SEM_ID = "semID";
public static final String SEM_COURSE_COLUMN_COURSE_ID = "courseID";
public static final String SEM_COURSE_COLUMN_TIME_ID = "timePeriodID";
public static final String SEM_COURSE_COLUMN_ROOM_ID = "roomID";
public static final String SEM_COURSE_COLUMN_INSTRUCTOR_ID = "instructorID";
public static final String SEM_COURSE_COLUMN_DAY = "day";
public DBHelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
// SEM_COURSE TABLE CREATION
db.execSQL("CREATE TABLE IF NOT EXISTS " + SEM_COURSE_TABLE_NAME +
"(" + SEM_COURSE_COLUMN_SEM_ID + " INTEGER REFERENCES " + SEMESTER_TABLE_NAME + "(" + SEMESTER_COLUMN_ID + ") ON UPDATE CASCADE ON DELETE CASCADE, " +
SEM_COURSE_COLUMN_INSTRUCTOR_ID + " INTEGER REFERENCES " + INSTRUCTOR_TABLE_NAME + "(" + INSTRUCTOR_COLUMN_ID + ") ON UPDATE CASCADE ON DELETE CASCADE, " +
SEM_COURSE_COLUMN_COURSE_ID + " INTEGER REFERENCES " + COURSE_TABLE_NAME + "(" + COURSE_COLUMN_ID + ") ON UPDATE CASCADE ON DELETE CASCADE, " +
SEM_COURSE_COLUMN_ROOM_ID + " INTEGER REFERENCES " + ROOM_TABLE_NAME + "(" + ROOM_COLUMN_ID + ") ON UPDATE CASCADE ON DELETE SET NULL, " +
SEM_COURSE_COLUMN_TIME_ID + " INTEGER REFERENCES " + TIMEPERTIOD_TABLE_NAME + "(" + TIMEPERTIOD_COLUMN_ID + ") ON UPDATE CASCADE ON DELETE CASCADE, " +
SEM_COURSE_COLUMN_STD_ID + " INTEGER REFERENCES " + STUDENT_TABLE_NAME + "(" + STUDENT_COLUMN_ID + ") ON UPDATE CASCADE ON DELETE CASCADE, " +
SEM_COURSE_COLUMN_DAY + " TEXT, " + "PRIMARY KEY(" + SEM_COURSE_COLUMN_SEM_ID + ", " + SEM_COURSE_COLUMN_COURSE_ID + ", " + SEM_COURSE_COLUMN_INSTRUCTOR_ID + ", " + SEM_COURSE_COLUMN_ROOM_ID
+ ", " + SEM_COURSE_COLUMN_STD_ID + ", " + SEM_COURSE_COLUMN_TIME_ID + "));");
}
public Cursor executeQuery(String sql) {
SQLiteDatabase db = getReadableDatabase();
Cursor cur = db.rawQuery(sql, null);
return cur;
}
}
ShowSchedule.java
public class ShowSchedule extends Activity {
private DBHelper db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_courses);
db = new DBHelper(this);
String stdID = HomePage.STD_ID;
String[] columns = new String[] {
DBHelper.COURSE_COLUMN_NAME
};
int[] to = new int[] {
R.id.tvCourseName1,
};
// select courseName from course where course.courseID in (select courseID from sem_course where sem_course.stdID = stdID)
String query = "select " + DBHelper.COURSE_COLUMN_ID + " as _id, " + DBHelper.COURSE_COLUMN_NAME + " from " + DBHelper.COURSE_TABLE_NAME +
" where " + DBHelper.COURSE_COLUMN_ID + " in (select " + DBHelper.SEM_COURSE_COLUMN_COURSE_ID
+ " as _id from " + DBHelper.SEM_COURSE_TABLE_NAME + " where " +
DBHelper.SEM_COURSE_COLUMN_STD_ID + " = " + stdID + ")";
ListView mainList = (ListView) findViewById(R.id.list_courses);
Cursor cursor = db.executeQuery(query);
// CoursesAdapter adapter = new CoursesAdapter(this, cursor);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.activity_list_item,
cursor, columns, to, 0);
mainList.setAdapter(adapter);
}
}
什麼,我得到的是隻有在充滿課程名稱每個項目列表中的第一次塊,雖然課程都應該在同一個塊,但不同的時間塊..
任何人都可以請幫我?
你正在引用'tvCourseName1',所以這是填充的文本視圖。 – njzk2
嘗試打印提取的光標數據 – VVB