Android ListView using SQLite

∥☆過路亽.° 提交于 2019-12-08 09:03:54

问题


I am trying to populate listview within a fragment class. I am trying to query that for every one student he has many toDo's s. 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.

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. Why is it when I try to get the data it is a null pointer, here is my populateList method.

    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

  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)

回答1:


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.




回答2:


You have an error in your reference to the listNotes function. execute doesn't have a function called listNotes, so that is what is returning as null

You need to reference the actual class in which listNotes appears, e.g. Controls.listNotes();



来源:https://stackoverflow.com/questions/28903352/android-listview-using-sqlite

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!