将数据库读入app /在listview中显示它

时间:2012-04-24 08:34:32

标签: java android database sqlite

我正在尝试使用我已保存在资产中的数据库填充列表视图。在运行应用程序时,我已经检查了数据库是否正在创建(它是)。我目前的问题是试图弄清楚如何获取列并在列表视图中显示它们。我的列是名称,位置和部门,以防您看到一段您不理解的代码。我一直在尝试使用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();
            }}

        }
    }

logcat的:

     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)

抱歉

1 个答案:

答案 0 :(得分:0)

试试这个PLZ .. 在下面的语句中,请将''before'放在'keyword'。

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

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