简体   繁体   English

使用SQLite的Android ListView

[英]Android ListView using SQLite

I am trying to populate listview within a fragment class. 我试图在片段类中填充listview。 I am trying to query that for every one student he has many toDo's s. 我试图查询每个学生,他有很多toDo的。 When the user is logged on I want to retrieve all his ToDo's he uniquely added and only data that belongs to him will be shown. 当用户登录时,我想要检索他唯一添加的所有ToDo,并且只显示属于他的数据。

Here is my Controls for notes to retrieve all notes 这是我的控件,用于检索所有笔记的注释

private DatabaseHelper help;
private SQLiteDatabase db;

    public Cursor listNotes() {
        Cursor c = db.query(help.NOTE_TABLE, new String[]{help.COLUMN_TITLE,help.COLUMN_BODY, help.COLUMN_DATE}, help.COLUMN_ID + " = ?", new String[]{String.valueOf(help.NOTES_ID)} , null, null, null);
        if(c != null) {
            c.moveToFirst();
        }

        return c;
     }

My databasehelper is the class that only creates tables. 我的databasehelper是只创建表的类。 Why is it when I try to get the data it is a null pointer, here is my populateList method. 为什么当我尝试获取数据时它是一个空指针,这是我的populateList方法。

    public void populateList(){
    Cursor cursor = execute.listNotes();

    //Mapping the fields cursor to text views
    String[] fields = new String[]{help.COLUMN_TITLE,help.COLUMN_BODY, help.COLUMN_DATE};
    int [] text = new int[] {R.id.item_title,R.id.item_body, R.id.item_date};
    adapter = new SimpleCursorAdapter(getActivity(),R.layout.list_layout,cursor, fields, text,0);

    //Calling list object instance
    listView = (ListView) getView().findViewById(android.R.id.list);
    listView.setAdapter(adapter);
}

LogCat logcat的

  java.lang.NullPointerException
        at com.example.app.Notes.populateList(Notes.java:49)

        at android.support.v4.app.Fragment.performCreateView(Fragment.java:1786)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:947)
        at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1126)
        at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:739)
        at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1489)
        at android.support.v4.app.FragmentManagerImpl.executePendingTransactions(FragmentManager.java:486)
        at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:141)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:1073)
        at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
        at android.support.v4.view.ViewPager$3.run(ViewPager.java:249)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
        at android.view.Choreographer.doCallbacks(Choreographer.java:555)
        at android.view.Choreographer.doFrame(Choreographer.java:524)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
        at android.os.Handler.handleCallback(Handler.java:615)
        at android.os.Handler.dispatchMessage(Handler.java:92)
        at android.os.Looper.loop(Looper.java:137)
        at android.app.ActivityThread.main(ActivityThread.java:4745)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:511)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
        at dalvik.system.NativeStart.main(Native Method)

You're closing the cursor before accessing any data. 您在访问任何数据之前关闭了光标。 You'll need to put the data you're looking for into an object and then return that object. 您需要将要查找的数据放入对象中,然后返回该对象。 Don't return the cursor. 不要返回光标。

public MyObject listNotes() {
    Cursor c = db.query("YOUR_QUERY");
    MyObject obj = new MyObject();
    if (c != null && c.moveToFirst()) {
        obj.setParameter(c.getInt(c.getColumnIndex("column_name")));
    }
    c.close();
    return obj;
 }

This is assuming of course that the first element of your cursor is the item your looking for. 当然,假设光标的第一个元素是您要查找的项目。

You have an error in your reference to the listNotes function. 您对listNotes函数的引用中存在错误。 execute doesn't have a function called listNotes, so that is what is returning as null execute没有名为listNotes的函数,因此返回null

You need to reference the actual class in which listNotes appears, eg Controls.listNotes(); 您需要引用listNotes出现的实际类,例如Controls.listNotes();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM