格式化简单光标适配器

时间:2015-01-08 14:53:14

标签: android sqlite listview simplecursoradapter

我正在开发一个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);
    }
}

我得到的只是每个项目列表中第一次填充课程名称,虽然课程应该都在同一个区块但是时间块不同..

有人可以帮助我吗?

1 个答案:

答案 0 :(得分:0)

我不知道你是否已经解决了这个问题,但看起来你错误地使用了光标适配器和listview。而不是创建包含所有课程名称的布局,而是创建一个将由listview的一行使用的布局。然后,当您设置适配器时,可以使用所需的内容填充每个文本视图。每个被拉出的课程将应用相同的格式并正确打印出来。