2

我正在尝试使用我保存在资产中的数据库填充列表视图。我已经检查了当我运行应用程序时是否正在创建数据库(它是)。我当前的问题是试图弄清楚如何获取列并将它们显示在列表视图中。我的列是姓名,职位和部门,以防你看到一段你不明白的代码。我一直在尝试使用来自http://saigeethamn.blogspot.com/2011/02/listview-of-data-from-sqlitedatabase.html的代码, 如果有人能够帮助我,我将不胜感激,谢谢在高级。

编辑:当代码运行时,它给了我一个空指针异常

    public class Favorites extends ListActivity {
        public static String tableName = "fac";
        private ListView lv;
        SQLiteDatabase newDB;
        private ArrayList<String> results = new ArrayList<String>();

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

            openAndQueryDatabase();
            displayResultList();
        }

        private void displayResultList() {
            TextView tView = new TextView(this);
            tView.setText("This data is retrieved from the database and only 4 "
                    + "of the results are displayed");
            getListView().addHeaderView(tView);

            setListAdapter(new ArrayAdapter<String>(this,
                    android.R.layout.simple_list_item_1, results));
            getListView().setTextFilterEnabled(true);

        }

        private void openAndQueryDatabase() {
            try {
                DataBaseHelper dbHelper = new DataBaseHelper(
                        this.getApplicationContext());
                newDB = dbHelper.getWritableDatabase();
                Cursor c = newDB.rawQuery("SELECT name, position FROM " + tableName
                        + "where ", null);

                if (c != null) {
                    if (c.moveToFirst()) {
                        do {
                            String name = c.getString(c.getColumnIndex("name"));
                            String position = c.getString(c
                                    .getColumnIndex("position"));
                            results.add("name: " + name + ",Position " + position);
                        } while (c.moveToNext());
                    }
                }
            } catch (SQLiteException se) {
            } finally {
                if (newDB != null)
{
                    newDB.execSQL("DELETE FROM " + tableName);
                newDB.close();
            }}

        }
    }

日志猫:

     04-24 04:43:19.064: E/AndroidRuntime(1644): FATAL EXCEPTION: main
        04-24 04:43:19.064: E/AndroidRuntime(1644): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cerealBarApps/com.cerealBarApps.Faculty_tab}: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cerealBarApps/com.cerealBarApps.Favorites}: java.lang.NullPointerException
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1837)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread.access$1500(ActivityThread.java:132)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1033)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.os.Handler.dispatchMessage(Handler.java:99)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.os.Looper.loop(Looper.java:143)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread.main(ActivityThread.java:4196)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at java.lang.reflect.Method.invokeNative(Native Method)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at java.lang.reflect.Method.invoke(Method.java:507)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at dalvik.system.NativeStart.main(Native Method)
        04-24 04:43:19.064: E/AndroidRuntime(1644): Caused by: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.cerealBarApps/com.cerealBarApps.Favorites}: java.lang.NullPointerException
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1816)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread.startActivityNow(ActivityThread.java:1651)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.LocalActivityManager.moveToState(LocalActivityManager.java:127)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.LocalActivityManager.startActivity(LocalActivityManager.java:339)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.widget.TabHost$IntentContentStrategy.getContentView(TabHost.java:656)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.widget.TabHost.setCurrentTab(TabHost.java:326)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.widget.TabHost.addTab(TabHost.java:216)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at com.cerealBarApps.Faculty_tab.onCreate(Faculty_tab.java:55)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)
        04-24 04:43:19.064: E/AndroidRuntime(1644):     ... 11 more
        04-24 04:43:19.064: E/AndroidRuntime(1644): Caused by: java.lang.NullPointerException
04-24 04:43:19.064: E/AndroidRuntime(1644):     at com.cerealBarApps.Favorites.openAndQueryDatabase(Favorites.java:63)
04-24 04:43:19.064: E/AndroidRuntime(1644):     at com.cerealBarApps.Favorites.onCreate(Favorites.java:25)
04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1093)
04-24 04:43:19.064: E/AndroidRuntime(1644):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1780)

对不起

4

1 回答 1

0

试试这个 plz.. 在下面的语句中,请在 'where' 关键字之前加上 ' '。

老的

Cursor c = newDB.rawQuery("SELECT name, position FROM " + tableName + "where ", null);

新的

Cursor c = newDB.rawQuery("SELECT name, position FROM " + tableName + " where ", null);
于 2012-04-24T09:10:46.540 回答