我正在尝试开发一个在主要活动中显示用户课程列表的应用程序。这些数据存储在 SQLite 中。如果用户希望添加更多课程,他们可以通过按下按钮将他们带到新活动。我创建了一个数据库,但是我在读取数据方面遇到了困难。我查看了很多文档,但似乎有各种方法没有任何一致的实现。有没有人有什么建议?
我尝试使用 SimpleCursorAdapter 获取数据,将其放入 int 数组并在列表视图中显示信息。
日志猫
10-21 02:08:20.042: E/AndroidRuntime(7044): Uncaught handler: thread main exiting due to uncaught exception
10-21 02:08:20.062: E/AndroidRuntime(7044): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mycoursetimetable/com.example.mycoursetimetable.MyCourses}: java.lang.ClassCastException: android.widget.LinearLayout
10-21 02:08:20.062: E/AndroidRuntime(7044): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2496)
10-21 02:08:20.062: E/AndroidRuntime(7044): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2512)
10-21 02:08:20.062: E/AndroidRuntime(7044): at android.app.ActivityThread.access$2200(ActivityThread.java:119)
10-21 02:08:20.062: E/AndroidRuntime(7044): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1863)
10-21 02:08:20.062: E/AndroidRuntime(7044): at android.os.Handler.dispatchMessage(Handler.java:99)
10-21 02:08:20.062: E/AndroidRuntime(7044): at android.os.Looper.loop(Looper.java:123)
10-21 02:08:20.062: E/AndroidRuntime(7044): at android.app.ActivityThread.main(ActivityThread.java:4363)
10-21 02:08:20.062: E/AndroidRuntime(7044): at java.lang.reflect.Method.invokeNative(Native Method)
10-21 02:08:20.062: E/AndroidRuntime(7044): at java.lang.reflect.Method.invoke(Method.java:521)
10-21 02:08:20.062: E/AndroidRuntime(7044): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
10-21 02:08:20.062: E/AndroidRuntime(7044): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
10-21 02:08:20.062: E/AndroidRuntime(7044): at dalvik.system.NativeStart.main(Native Method)
10-21 02:08:20.062: E/AndroidRuntime(7044): Caused by: java.lang.ClassCastException: android.widget.LinearLayout
10-21 02:08:20.062: E/AndroidRuntime(7044): at android.app.ListActivity.onContentChanged(ListActivity.java:234)
10-21 02:08:20.062: E/AndroidRuntime(7044): at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:201)
10-21 02:08:20.062: E/AndroidRuntime(7044): at android.app.Activity.setContentView(Activity.java:1622)
10-21 02:08:20.062: E/AndroidRuntime(7044): at com.example.mycoursetimetable.MyCourses.onCreate(MyCourses.java:39)
10-21 02:08:20.062: E/AndroidRuntime(7044): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-21 02:08:20.062: E/AndroidRuntime(7044): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2459)
10-21 02:08:20.062: E/AndroidRuntime(7044): ... 11 more
10-21 02:08:20.082: I/dalvikvm(7044): threadid=7: reacting to signal 3
10-21 02:08:20.082: E/dalvikvm(7044): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
10-21 02:08:22.082: I/Process(7044): Sending signal. PID: 7044 SIG: 9
activity_my_courses 布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<Button
android:id="@+id/labelAddCourseButton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="addCourseButton"
android:padding="10dp"
android:text="@string/CourseName" />
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
主要活动
public class MyCourses extends ListActivity {
static final String TEST = "com.example.mycoursetimetable.TEST";
String [] MODULE;
private database MODULEDATABASE;
ListView listContent;
Cursor cursor;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_courses);
listContent =(ListView)findViewById(R.id.contentlist);
MODULEDATABASE = new database(this);
cursor = MODULEDATABASE.getData();
String [] from = new String[]{database.KEY_MODULECODE,database.KEY_MODULENAME};
int[] to = new int[] {R.id.labelModuleCode, R.id.labelEditModuleFull}; cursorAdapter CA = new cursorAdapter(this,R.layout.listcourses, cursor, from, to, 0);
listContent.setAdapter(CA);
}
class cursorAdapter extends SimpleCursorAdapter {
private Context context;
private int layout;
public cursorAdapter(Context context,int layout, Cursor cursor,String [] from, int[] to, int flag)
{
super(context,layout, cursor, from, to, flag);
this.context = context;
this.layout = layout;
}
}
数据库类
公共类数据库{
public static final String KEY_ROWID = "_id";
public static final String KEY_MODULECODE = "module_code";
public static final String KEY_MODULENAME = "module_name";
public static final String KEY_MODULETYPE = "module_type";
public static final String KEY_MODULEDAY = "module_day";
public static final String KEY_MODULESTART = "module_start";
public static final String KEY_MODULEEND = "module_end";
public static final String KEY_MODULELOCATION = "module_location";
public static final String KEY_MODULECOMMENTS = "module_comments";
private static final String DATABASE_NAME = "module_database";
private static final String DATABASE_TABLE = "my_modules";
private static final int DATABASE_VERSION = 1;
private DbHelper myHelper;
private final Context myContext;
private SQLiteDatabase moduleDatabase;
private static class DbHelper extends SQLiteOpenHelper {
public DbHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + DATABASE_TABLE + " (" +
KEY_ROWID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
KEY_MODULECODE + " TEXT NOT NULL, " +
KEY_MODULENAME + " TEXT NOT NULL, " +
KEY_MODULELOCATION + " TEXT NOT NULL, " +
KEY_MODULECOMMENTS + " TEXT NOT NULL); "
);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
db.execSQL("DROP TABEL IF EXISTS " + DATABASE_TABLE);
onCreate(db);
}
}
public database(Context c){
myContext = c;
}
public database openToWrite()throws SQLException{
myHelper = new DbHelper(myContext);
moduleDatabase = myHelper.getWritableDatabase();
return this;
}
public database openToRead()throws SQLException{
myHelper = new DbHelper(myContext);
moduleDatabase = myHelper.getReadableDatabase();
return this;
}
public void close(){
myHelper.close();
}
public long createEntry(String moduleCode, String moduleName, String moduleLocation, String moduleComments) {
ContentValues cv = new ContentValues();
cv.put(KEY_MODULECODE,moduleCode);
cv.put(KEY_MODULENAME,moduleName);
cv.put(KEY_MODULELOCATION,moduleLocation);
cv.put(KEY_MODULECOMMENTS,moduleComments);
return moduleDatabase.insert(DATABASE_TABLE,null,cv);
// TODO Auto-generated method stub
}
public Cursor getData() {
String[] columns = new String [] {KEY_ROWID,KEY_MODULECODE, KEY_MODULENAME};
Cursor c = moduleDatabase.query(DATABASE_TABLE, columns, null, null, null, null, null);
return c;
}
}