ListView在android中使用数据库

瑞安159

我之前也问过类似的问题,但不确定如何措辞。从那以后,我看了一些教程,对我的问题有了更多的了解。我正在尝试将数据库中的信息显示到列表视图中,但无法弄清楚如何使其工作。任何帮助将是巨大的。

这是我的数据库适配器类:

public class DBAdapter {

public static final String KEY_ROWID = "id";
public static final String KEY_TITLE = "title";
public static final String KEY_WORKOUTDATE = "workoutDate";
public static final String KEY_EXERCISE_NOTES = "notes";

private static final String TAG = "WorkoutDBAdapter";
private DatabaseHelper mDBHelper;
private SQLiteDatabase mdb;

private static final String DATABASE_NAME = "WorkoutDB";
private static final String DATABASE_TABLE = "workouts";
private static final int DATABASE_VERSION = 1;

private final Context mCtx;

private static final String DATABASE_CREATE =
        "create table if not exists workouts " +
                "(id integer primary key autoincrement, " +
                "title VARCHAR not null, " +
                "workoutDate date, " +
                "notes VARCHAR );";

private static class DatabaseHelper extends SQLiteOpenHelper {

    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        Log.w(TAG, DATABASE_CREATE);
        db.execSQL(DATABASE_CREATE);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS workouts");
        onCreate(db);
    }
}


public DBAdapter(Context ctx) {
    this.mCtx = ctx;
}


public DBAdapter open() throws SQLException {
    mDBHelper = new DatabaseHelper(mCtx);
    mdb = mDBHelper.getWritableDatabase();
    return this;
}

public void close() {
    if (mDBHelper != null) {
        mDBHelper.close();
    }
}

//---retrieves all the records---
public Cursor getAllRecords() {
    Cursor mCursor = mdb.query(DATABASE_TABLE, new String[]{KEY_ROWID, KEY_TITLE,
            KEY_WORKOUTDATE, KEY_EXERCISE_NOTES}, null, null, null, null, null);

    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

}

这是我的AndroidListViewCursorAdaptorActivity类:

public class AndroidListViewCursorAdaptorActivity extends Activity {

private DBAdapter dbHelper;
private SimpleCursorAdapter dataAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.workouts);

    dbHelper = new DBAdapter(this);
    dbHelper.open();



    //Generate ListView from SQLite Database
    displayListView();

}

private void displayListView() {


    Cursor cursor = dbHelper.getAllRecords();

    // The desired columns to be bound
    String[] columns = new String[] {
            DBAdapter.KEY_TITLE,
            DBAdapter.KEY_WORKOUTDATE,
            DBAdapter.KEY_EXERCISE_NOTES,

    };

    // the XML defined views which the data will be bound to
    int[] to = new int[] {
            R.id.title,
            R.id.workoutDate,
            R.id.workoutDetails,

    };

    // create the adapter using the cursor pointing to the desired data
    //as well as the layout information
    dataAdapter = new SimpleCursorAdapter(
            this, R.layout.workout_info,
            cursor,
            columns,
            to,
            0);

    ListView listView = (ListView) findViewById(R.id.listView1);
    // Assign adapter to ListView
    listView.setAdapter(dataAdapter);

    };


}

这是xml文件,用于保存数据应放置的位置(我认为我不是100%确信这是否是正确的方法)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="6dip" >

<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="Title: "
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView1"
    android:layout_below="@+id/textView1"
    android:text="WorkoutDate: "
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/textView3"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignLeft="@+id/textView2"
    android:layout_below="@+id/textView2"
    android:text="Workout Details"
    android:textAppearance="?android:attr/textAppearanceMedium" />

<TextView
    android:id="@+id/workoutDetails"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:layout_alignBottom="@+id/textView3"
    android:layout_centerHorizontal="true" />

<TextView
    android:id="@+id/workoutDate"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:layout_alignBottom="@+id/textView2"
    android:layout_alignLeft="@+id/workoutDetails"
    android:layout_alignStart="@+id/workoutDetails" />

<TextView
    android:id="@+id/title"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView"
    android:layout_alignParentTop="true"
    android:layout_alignLeft="@+id/workoutDetails"
    android:layout_alignStart="@+id/workoutDetails" />

使用listview1继承锻炼XML

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">

<TextView android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:padding="10dp"
    android:text="@string/some_text" android:textSize="20sp" />

<EditText android:id="@+id/myFilter" android:layout_width="match_parent"
    android:layout_height="wrap_content" android:ems="10"
    android:hint="@string/some_hint">
    <requestFocus />
</EditText>

<ListView android:id="@+id/listView1" android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

这是堆栈跟踪。我收到新的错误消息?

04-30 08:05:52.741 2565-2565/course.labs.todomanager E/AndroidRuntime: FATAL EXCEPTION: main
                                                                   Process: course.labs.todomanager, PID: 2565
                                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{course.labs.todomanager/course.labs.todomanager.AndroidListViewCursorAdaptorActivity}: android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT _id, title, workoutDate, notes FROM workouts
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2325)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387)
                                                                       at android.app.ActivityThread.access$800(ActivityThread.java:151)
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303)
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                       at android.os.Looper.loop(Looper.java:135)
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5254)
                                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                                       at java.lang.reflect.Method.invoke(Method.java:372)
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
                                                                    Caused by: android.database.sqlite.SQLiteException: no such column: _id (code 1): , while compiling: SELECT _id, title, workoutDate, notes FROM workouts
                                                                       at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
                                                                       at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
                                                                       at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
                                                                       at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
                                                                       at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
                                                                       at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
                                                                       at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
                                                                       at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1316)
                                                                       at android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1163)
                                                                       at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1034)
                                                                       at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1202)
                                                                       at course.labs.todomanager.DBAdapter.getAllRecords(DBAdapter.java:91)
                                                                       at course.labs.todomanager.AndroidListViewCursorAdaptorActivity.displayListView(AndroidListViewCursorAdaptorActivity.java:43)
                                                                       at course.labs.todomanager.AndroidListViewCursorAdaptorActivity.onCreate(AndroidListViewCursorAdaptorActivity.java:36)
                                                                       at android.app.Activity.performCreate(Activity.java:5990)
                                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1106)
                                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2278)
                                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2387) 
                                                                       at android.app.ActivityThread.access$800(ActivityThread.java:151) 
                                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1303) 
                                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                       at android.os.Looper.loop(Looper.java:135) 
                                                                       at android.app.ActivityThread.main(ActivityThread.java:5254) 
                                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                                       at java.lang.reflect.Method.invoke(Method.java:372) 
                                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903) 
                                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698) 
恩佐基

我没有在您的代码中看到任何错误,您是否尝试过重新安装应用程序?这样您的SQL更改将被重新验证。

注意:我的回答是基于当前的EDIT以及对当前评论的响应。

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在android中使用本地数据库

在Android开发中使用数据库

使用本地数据库创建listview具有复制数据库android时出错

如何在Android中使用FirebaseListAdapter在ListView中显示Firebase数据库中项目的确切数量?

在Android的SQLite数据库中使用相同的数据库文件插入不同的表

Android在Service类中使用数据库

在Android中使用预填充的SQlite数据库

在Android Apps中使用大型数据库

在Flutter中使用预制数据库(Android Studio)

在 Android Studio 中使用 Room 数据库关联表

在Android Wear中使用现有数据库

我无法在android中的片段中使用数据库

ListView Android 中的 SQLite 数据库

Android如何从onListItemClick中获取ListView ID并使用它?ListView,通知,SQLite数据库

在itextsharp中使用数据库值

在Libgdx中使用SQLite数据库

在PHP数据库中使用fancybox

在Xamarin中使用本地数据库

在 Java 中使用 SQL 数据库

我在 Android Studio 中使用 Room 数据库,但似乎无法正确更新数据库中项目的字段

Android / MySQL:从 ListView 删除 MySQL 数据库中的数据

在Android的ListView中显示数据库的数据

无法从Android中的数据库填充listView中的数据

从数据库Android将数据加载到ListView中

在Android上的ListView中显示数据库中的数据

在Android中使用多个where条件从Firebase数据库中获取数据

在Android中使用SQLite数据库插入数据时出错

在 Android 房间数据库中使用嵌套关系返回空数据

Android Studio:使用 ListView 显示 SQLite 数据库行的 ArrayList