Search code examples
androidlistviewsimplecursoradapter

Android: SimpleCursorAdapter & ListView


Hi I started programming a ToDo-List on android (in eclipse) which synchronises with Google Tasks and add tasks from your Google Account. I implemented a really simple database (according to this tutorial: SQLITE Tutorial). The project contains one activity, which only implements a listView and a button which allows it to add tasks.(parent activity) By clicking the button a new acitvity is started via "startActivityForResult" and you can enter a name and a category.(child activity) Then this two strings are passed to the parent activity and added to the database, which is initialized in the parent activity Now, the listview should be created and a simpleCursorAdapter should be set; thats the point where the app crashes, on the emulator and on my android handy. Heres a code snippet of the start activity:

 @Override
public void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    initDB();
    initUI();
}

public void initUI()
{
    Log.w(TAG, "initUI()");
    populateToDoList();

    listView = (ListView) findViewById(R.id.tasklist);
    addButton = (Button) findViewById(R.id.addTask);

    addButton.setOnClickListener(new OnClickListener()
    {

        @Override
        public void onClick(View arg0) 
        {
            // TODO Auto-generated method stub
            Intent i = new Intent(RememberMe.this, AddTask.class);
            startActivityForResult(i, REQUEST_CODE);
    }   });
}

public void initDB()
{
    toDoDBAdapter = new ToDoDBAdapter(RememberMe.this);
    toDoDBAdapter.open();
}

private void populateToDoList() 
{

    Log.w(TAG, "populateToDoList()");

    dbCursor = toDoDBAdapter.getAllToDoItemsCursor();
    Log.w(TAG, "getAllToDoItemsCursor()");

    startManagingCursor(dbCursor);
    Log.w(TAG, "startManagingCursor()");

    String [] columns = { ToDoDBAdapter.KEY_NAME};
    int [] to = { android.R.id.text1 };
    Log.w(TAG, "Sring from int to");

    simpleCursor = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, dbCursor, columns, to);
    Log.w(TAG, "new SimpleCursorAdapter()");

    listView.setAdapter(simpleCursor);
    Log.w(TAG, "setAdapter()");

    simpleCursor.notifyDataSetChanged();    

}

private void updateList() 
{

    dbCursor.requery();
    simpleCursor.notifyDataSetChanged();

}


@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if(resultCode == REQUEST_CODE)
    {
        String taskName = data.getStringExtra("taskName");
        String taskCategory = data.getStringExtra("taskCategory");

        toDoDBAdapter.createTask(taskName, taskCategory);

        updateList();

        Toast.makeText(this, "Aufgabe hinzugefügt", Toast.LENGTH_LONG).show();
    }
}

@Override
public void onDestroy() 
{
    super.onDestroy();
    toDoDBAdapter.close();
}
}

It stops at

listView.setAdapter(simpleCursor);
Log.w(TAG, "setAdapter()");

I've no idea why or what's the problem; I read that there are problems with the emulator on a 64-bit system but the code doens't work either on my handy and implementing listviews and setting a simpleCursor should not be so tricky O.o Heres the logcat: LogCat


Solution

  • Have you checked to make sure that listView is in fact not null? Debug your code, step all the way up to listView.setAdapter(simpleCursor); and hover over listView to see if it is null.

    EDIT: You did find that it was in fact null, here is the most common reason(s) that I have found:

    listView = (ListView) findViewById(R.id.tasklist);
    

    R.id.tasklist is CASE sensitive, make sure in your XML it is not @+id/taskList or something similar or this will return null

    Also make sure it is spelled exactly right, as it could just be a case of misspelling.