androidlistviewandroid-fragmentscursorsimplecursoradapter

Refresh Listview simpleCursorAdapter always null


I have a program with fills a listview from a SQLLiteDB. The initialload works, but when I edit a entry the program crashes and the simplecursorAdapter = null.

The Fragment:

public class TaskListFragment extends ListFragment
{
DBHelper dbHelper;
SimpleCursorAdapter simpleCursorAdapter;
Cursor cursor;
ListView listView;
static final int UPDATE_TASK = 1;
String[] from = {dbHelper.COL_2,dbHelper.COL_3};
int[] to = new int[]{R.id.taskName , R.id.taskDescription};


public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.tasklistlayout, container, false);

    listView = (ListView)rootView.findViewById(android.R.id.list);

    setRetainInstance(true);
    return rootView;
}

@Override
public void onActivityCreated(Bundle savedInstanceState)
{
    super.onActivityCreated(savedInstanceState);
    dbHelper = new DBHelper(getActivity());
    prepareCursor();
}

public void prepareCursor()
{
    new Handler().post(new Runnable()
    {
        @Override
        public void run()
        {
            cursor = dbHelper.getTasksForListView();
            simpleCursorAdapter = new SimpleCursorAdapter (getActivity(),R.layout.taskrowlayout,cursor,from,to,0);
            fillList();
        }
    });
}

public void updateList()
{
    simpleCursorAdapter.notifyDataSetChanged();
    fillList();
}

public void fillList()
{
    setListAdapter(simpleCursorAdapter);
}

@Override
public void onListItemClick(ListView l, View v, int position, long id)
{
    Intent intent = new Intent(getActivity().getApplicationContext(), TaskInformation.class);
    intent.putExtra("taskId", cursor.getInt(0));
    intent.putExtra("taskName", cursor.getString(1));
    intent.putExtra("taskDescription", cursor.getString(2));
    intent.putExtra("taskStartDate", cursor.getString(3));
    intent.putExtra("taskEndDate", cursor.getString(4));
    startActivityForResult(intent, UPDATE_TASK);
}
}

The piece of code in MainActivity which calls it:

   @Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
        if (resultCode == RESULT_OK)
        {
            taskListFragment.updateList();
        }
}

The code in the TaskInformation class which runs when I click update

    public void updateTask(View view)
{
    task = new ArrayList<>();
    task.add(0,String.valueOf(currentTaskId));
    task.add(1,String.valueOf(taskName.getText()));
    task.add(2,String.valueOf(taskDescription.getText()));
    task.add(3,String.valueOf(startDate.getText()));
    task.add(4,String.valueOf(endDate.getText()));
    dbHelper.updateTask(task);
    setResult(RESULT_OK, intent);
    finish();
}

Why is the simpleCursorAdapter always NULL when I update it? I want to refresh/reload the listview when I updated it.

EDIT------------------------------

I tried running the prepareCursor again but its still null, I can't seem to find out why

EDIT 2 --------------------------------

I'm a bit further, it seems the context my adapter needed was gone. Fixed it by updating the method to this:

    public void updateList(Context context)
{

    simpleCursorAdapter = new SimpleCursorAdapter (context,R.layout.taskrowlayout,cursor,from,to,0);
    simpleCursorAdapter.notifyDataSetChanged();
    fillList();
}

Only this time my cursor is empty. When I try to refill it using this:

cursor = dbHelper.getTasksForListView();

I get this error:

05-18 07:30:59.478 7323-7323/com.owlfinity.zeepblok.taskjournal E/AndroidRuntime: FATAL EXCEPTION: main
                                                                              Process: com.owlfinity.zeepblok.taskjournal, PID: 7323
                                                                              java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=65537, result=-1, data=Intent { cmp=com.owlfinity.zeepblok.taskjournal/.TaskInformation (has extras) }} to activity {com.owlfinity.zeepblok.taskjournal/com.owlfinity.zeepblok.taskjournal.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.owlfinity.zeepblok.taskjournal.DBHelper.getTasksForListView()' on a null object reference
                                                                                  at android.app.ActivityThread.deliverResults(ActivityThread.java:3699)
                                                                                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742)
                                                                                  at android.app.ActivityThread.-wrap16(ActivityThread.java)
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393)
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102)
                                                                                  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)
                                                                               Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.Cursor com.owlfinity.zeepblok.taskjournal.DBHelper.getTasksForListView()' on a null object reference
                                                                                  at com.owlfinity.zeepblok.taskjournal.TaskListFragment.updateList(TaskListFragment.java:61)
                                                                                  at com.owlfinity.zeepblok.taskjournal.MainActivity.onActivityResult(MainActivity.java:37)
                                                                                  at android.app.Activity.dispatchActivityResult(Activity.java:6428)
                                                                                  at android.app.ActivityThread.deliverResults(ActivityThread.java:3695)
                                                                                  at android.app.ActivityThread.handleSendResult(ActivityThread.java:3742) 
                                                                                  at android.app.ActivityThread.-wrap16(ActivityThread.java) 
                                                                                  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1393) 
                                                                                  at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                                                  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) 

And here is the getTaskForListView method:

    public Cursor getTasksForListView()
{
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor result = db.rawQuery("SELECT * FROM " + TABLE_NAME,null);
    return result;
}

EDIT 3----------------------

Errors are gone but still no refresh of my ListView :( this is the code

    public void updateList(Context context)
{
    dbHelper = new DBHelper(context);
    cursor = dbHelper.getTasksForListView();
    simpleCursorAdapter = new SimpleCursorAdapter (context,R.layout.taskrowlayout,cursor,from,to,0);
    fillList();

}

public void fillList()
{
    dbHelper = new DBHelper(getActivity());
    setListAdapter(simpleCursorAdapter);
}

----EDIT 4

My ListView is empty in the update method. Can't seem to find a way to recreate it.

I think the problem lies in this piece of code:

  public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
    View rootView = inflater.inflate(R.layout.tasklistlayout, container, false);
    listView = (ListView)rootView.findViewById(android.R.id.list);
    setRetainInstance(false);
    return rootView;
}

How can I rerun this piece of code in the update method?


Solution

  • Found it, I had multiple Activities that uses the onActivityResult and in there rebuild the cursor and list. fused them into one and it works :D guess my app was confused how to handle it. Found t because the request code, when it returned, was a number I never set.