如何在Android上通过ListView显示数据库中的数据

时间:2016-02-28 20:28:24

标签: android

我想从数据库中显示listview光标,我尝试运行我的项目但总是出错并且我的应用总是需要关闭。请帮忙。

我的loadListview函数(我在onCreate上调用它)

  private void loadListviewData() {
    NoteAlDbAdapter db = new NoteAlDbAdapter(getApplicationContext());

    List<String> lables = db.getAllLabels();

    ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_item, lables);

     dataAdapter.setDropDownViewResource(android.R.layout.simple_list_item_1);

    ListView listview = (ListView) findViewById(R.id.listView);
    listview.setAdapter(dataAdapter);
}

这是我的DbAdapter.java

public List<String> getAllCategory() {
    List<String> List = new ArrayList<String>();
    String selectQuery = "SELECT  * FROM " + TABLO_ISMI;
    Cursor cursor = db.rawQuery(selectQuery, null);

    if (cursor.moveToFirst()) {
        do {
            List.add(cursor.getString(0));
        } while (cursor.moveToNext());
    }
    return List;
}

logcat的

02-28 22:38:01.831 12605-12605/reminderplus.reminder2 E/AndroidRuntime: FATAL EXCEPTION: main
                                                                    Process: reminderplus.reminder2, PID: 12605
                                                                    java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String java.lang.Object.toString()' on a null object reference
                                                                        at android.widget.ArrayAdapter.createViewFromResource(ArrayAdapter.java:401)
                                                                        at android.widget.ArrayAdapter.getView(ArrayAdapter.java:369)
                                                                        at android.widget.AbsListView.obtainView(AbsListView.java:2346)
                                                                        at android.widget.ListView.makeAndAddView(ListView.java:1875)
                                                                        at android.widget.ListView.fillDown(ListView.java:702)
                                                                        at android.widget.ListView.fillFromTop(ListView.java:763)
                                                                        at android.widget.ListView.layoutChildren(ListView.java:1684)
                                                                        at android.widget.AbsListView.onLayout(AbsListView.java:2148)
                                                                        at android.view.View.layout(View.java:16630)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                        at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                        at android.view.View.layout(View.java:16630)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1079)
                                                                        at android.view.View.layout(View.java:16630)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                        at android.view.View.layout(View.java:16630)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                        at android.support.v7.widget.ActionBarOverlayLayout.onLayout(ActionBarOverlayLayout.java:435)
                                                                        at android.view.View.layout(View.java:16630)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                        at android.view.View.layout(View.java:16630)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1743)
                                                                        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1586)
                                                                        at android.widget.LinearLayout.onLayout(LinearLayout.java:1495)
                                                                        at android.view.View.layout(View.java:16630)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:336)
                                                                        at android.widget.FrameLayout.onLayout(FrameLayout.java:273)
                                                                        at com.android.internal.policy.PhoneWindow$DecorView.onLayout(PhoneWindow.java:2678)
                                                                        at android.view.View.layout(View.java:16630)
                                                                        at android.view.ViewGroup.layout(ViewGroup.java:5437)
                                                                        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:2171)
                                                                        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1931)
                                                                        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1107)
                                                                        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:6013)
                                                                        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:858)
                                                                        at android.view.Choreographer.doCallbacks(Choreographer.java:670)
                                                                        at android.view.Choreographer.doFrame(Choreographer.java:606)
                                                                        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:844)
                                                                        at android.os.Handler.handleCallback(Handler.java:739)
                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                        at android.os.Looper.loop(Looper.java:148)
                                                                        at android.app.ActivityThread.main(ActivityThread.java:5417)
                                                                        at java.lang.reflect.Method.invoke(Native Method)
                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
                                                                        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

1 个答案:

答案 0 :(得分:0)

功能上的名称错误?

您在方法中调用了db.getAllLabels(),但您已链接了一个名为db.getAllCategory()的函数。

在这种情况下,它会关闭应用程序,因为您发送了一个空对象列表,当它尝试将该字符串绑定到简单列表项时,它为空。