【问题标题】:ListView from SQLite Error in Android App (Solve)Android 应用程序中 SQLite 错误的 ListView(解决)
【发布时间】:2014-04-11 10:05:42
【问题描述】:

我需要帮助!!!!!!!!!!!!!!!

Database Generation 在这个我用参数和列表视图适配器生成表:

public class TimeKeepingDB {

    private static final String DATABASE_NAME = "TimeKeepingDB.db";


    public static final String DATABASE_TABLESTUDY = "study";
        public static final String KEY_ROWSTUDYID = "_id";
        public static final String KEY_STUDYCODE = "code_study";
        public static final String KEY_STUDYNAME = "description_study";
        public static final String KEY_STUDANALYST = "analyst_study";
        public static final String KEY_STUDYPHOTO = "photo_study";

        private static final int DATABASE_VERSION  = 1;

        private DbHelper ourHelper;
        private final Context ourContext;
        private static SQLiteDatabase ourDatabase;

    private static class DbHelper extends SQLiteOpenHelper{

    public DbHelper(Context context) {
                super(context, DATABASE_NAME, null, DATABASE_VERSION);
                // TODO Auto-generated constructor stub
            }

        @Override
        public void onCreate(SQLiteDatabase db) {
            // Create Table Studies
            db.execSQL(" CREATE TABLE " + DATABASE_TABLESTUDY + " (" +
                    KEY_ROWSTUDYID + " INTEGER PRIMARY KEY AUTOINCREMENT, " +
                    KEY_STUDYCODE + " TEXT UNIQUE NOT NULL, " +
                    KEY_STUDYNAME + " TEXT, " +
                    KEY_STUDANALYST + " TEXT, " +
                    KEY_STUDYPHOTO + " BLOB);"          
            );          

        }
        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            // TODO Auto-generated method stub
            db.execSQL(" DROP TABLE IF EXIST " + DATABASE_TABLESTUDY);
            onCreate(db);

        }
    }
    public TimeKeepingDB(Context c){
        ourContext = c;
    }
    public TimeKeepingDB open(){
        ourHelper = new DbHelper(ourContext);
        ourDatabase = ourHelper.getWritableDatabase();
        return this;
    }
    public void close(){
        ourHelper.close();
    }


    //Read a list of all studies
    public static Cursor ListViewStudies() {
        // TODO Auto-generated method stub
        Cursor mCursor = ourDatabase.query(DATABASE_TABLESTUDY, 
                new String[] {KEY_ROWSTUDYID + " AS _id",
                KEY_STUDYCODE, 
                KEY_STUDYNAME},
                null, null, null, null, null);

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

Main Activity,它被称为列表视图:

public class MainActivity extends Activity {

    private SimpleCursorAdapter dataAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TimeKeepingDB info = new TimeKeepingDB(this);
        info.open();
        //displayListViewStudies();
        info.close();
    }

    private void displayListViewStudies() {
        // TODO Auto-generated method stub
        Cursor cursor = TimeKeepingDB.ListViewStudies();
        //startManagingCursor(cursor);
        String[] from = new String[] {
                TimeKeepingDB.KEY_ROWSTUDYID,
                TimeKeepingDB.KEY_STUDYCODE,
                TimeKeepingDB.KEY_STUDYNAME,
              };
        int[] to = new int[] {
                R.id.idStudy,
                R.id.CodeStudy,
                R.id.Studydescription,
              };
        dataAdapter = new SimpleCursorAdapter(this, R.layout.activity_main, cursor, from, to, 0);
        ListView listView1 = (ListView) findViewById(R.id.listStudies);
          // Assign adapter to ListView
          listView1.setAdapter(dataAdapter);

    }

}

列表视图的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >

<ListView
    android:id="@+id/listStudies"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true" >
</ListView>

<TextView
    android:id="@+id/idStudy"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="TextView" />

<TextView
    android:id="@+id/CodeStudy"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="TextView" />

<TextView
    android:id="@+id/Studydescription"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:text="TextView" />

我得到的错误是下一个:

04-12 07:37:07.615: D/AndroidRuntime(2305): 关闭 VM 04-12 07:37:07.665: W/dalvikvm(2305): threadid=1: 线程以未捕获的异常退出 (group=0xb2cfcb08) 04-12 07:37:07.705:E/AndroidRuntime(2305):致命异常:主要 04-12 07:37:07.705: E/AndroidRuntime(2305): 进程: com.example.timekeeping, PID: 2305 04-12 07:37:07.705: E/AndroidRuntime(2305): java.lang.RuntimeException: 无法启动活动 ComponentInfo{com.example.timekeeping/com.example.timekeeping.MainActivity}: java.lang.IllegalArgumentException: 列'_idstudy' 不存在 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 android.app.ActivityThread.access$700(ActivityThread.java:135) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 android.os.Handler.dispatchMessage(Handler.java:102) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 android.os.Looper.loop(Looper.java:137) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 android.app.ActivityThread.main(ActivityThread.java:4998) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 java.lang.reflect.Method.invokeNative(Native Method) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 java.lang.reflect.Method.invoke(Method.java:515) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593) 04-12 07:37:07.705: E/AndroidRuntime(2305): at dalvik.system.NativeStart.main(Native Method) 04-12 07:37:07.705:E/AndroidRuntime(2305):原因:java.lang.IllegalArgumentException:列“_idstudy”不存在 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 android.database.AbstractCursor.getColumnIndexOrThrow(AbstractCursor.java:303) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 android.support.v4.widget.SimpleCursorAdapter.findColumns(SimpleCursorAdapter.java:317) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 android.support.v4.widget.SimpleCursorAdapter.(SimpleCursorAdapter.java:92) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 com.example.timekeeping.MainActivity.displayListViewStudies(MainActivity.java:42) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 com.example.timekeeping.MainActivity.onCreate(MainActivity.java:24) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 android.app.Activity.performCreate(Activity.java:5243) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) 04-12 07:37:07.705: E/AndroidRuntime(2305): 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140) 04-12 07:37:07.705: E/AndroidRuntime(2305): ... 11 更多

【问题讨论】:

  • 您缺少的关键元素似乎是使用 SimpleCursorAdapterListView 连接到您的数据库。
  • 你得到的错误是什么?

标签: java android sqlite listview


【解决方案1】:

一般步骤是:

  1. 使用DbHelper dbHelper = new DbHelper(this)SQLiteDatabase db = dbHelper.getWritableDatabase()从您的活动中打开您的数据库
  2. Cursor c = db.query(...) 返回游标
  3. 使用ListView lv = (ListView) findViewById(..) 获取您的ListView
  4. 创建一个适配器,将光标中的数据绑定到 ListView。我建议使用SimpleCursorAdapter
  5. 最后拨打lv.setAdapter(adapter)

【讨论】:

  • 您忘记了第 0 步:创建 SQLiteOpenDatabaseHelper 的子类
【解决方案2】:
java.lang.IllegalArgumentException: column '_id' does not exist

CursorAdapter 需要光标中的 _id。将其添加到您的ListViewStudies()

Cursor mCursor = ourDatabase.query(DATABASE_TABLESTUDY,
    new String[] { KEY_ROWSTUDYID + " AS _id", KEY_STUDYCODE, KEY_STUDYNAME },
    null, null, null, null, null);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-02-10
    • 1970-01-01
    • 2012-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-12-15
    • 1970-01-01
    相关资源
    最近更新 更多