Skip to content Skip to sidebar Skip to footer

Debugging Simplecursoradapter

I'm working on my first Android app and can't figure out how to get my SimpleCursorAdpater to populate the view. The cursor that I'm passing in has results in it, so the problem m

Solution 1:

You can step into the code if you have the source code. Luckily, Android is open source. To easily attach source code in Eclipse, see:

http://android.opensourceror.org/2010/01/18/android-source/

As for the problem itself, you said in a comment above that you iterate all of the items before creating the adapter. If you are not creating a new cursor after iteration, you probably need to rewind it or the adapter might think it's empty.

cursor.moveToFirst()

Solution 2:

Please don't worry about any internal binding aspects. I'm sure there is an easy way out. Try the following: First, just to ensure your cursor really has got data where it's needed, put the line

System.out.println("cursor.getCount()="+cursor.getCount());

right before the call of setAdapter(). But surely, you already tested to get a row count ;-) So the following might be more interesting.

To check if your binding fails, please test with:

android:id="@+id/android:list"

instead of :

android:id="@android:id/android:list"

in your main.xml. Same thing with: android:id="@+id/android:empty".

And if you still don't get results, you can also try using a list default xml-layout (like simple_list_item_1) for displaying, which would look like this:

ListAdapter adapter = new SimpleCursorAdapter(this, 
    // Use a template that displays a text view
    android.R.layout.simple_list_item_1, 
    // Give the cursor to the list adapter
    cursor, 
    // Map the NAME column in your database to...newString[] {SearchConstants.NAME_COLUMN} ,
    // ...the "text1" view defined in the R.layout.simple_list_item_1newint[] {android.R.id.text1}
);

Just copy paste it into your activity and see what happens. Hope you're done with that!

Solution 3:

Just got the same problem and found how to allow the Simplecursoradapter creation to not fail.

In your cursor, the query of the database MUST contain the table primary key even if you don't need it ! If not it will fail and crash...

Hope it will help others with the same problem !

Solution 4:

Alright, I noticed you used a column name with a capital letter. Make sure you use the exact identifier in the DB scheme (the sqlite column names are case sensitive). But in the code you provided the column identifiers match.

So, if the cursor you use really has got the data, try out the above code at first (instead of some custom layout) for creating a SimpleCursorAdapter and it should work. Here's another little example for the activity code (as I don't know yours):

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.dbManager = newDatabaseManager(this);

    setContentView(R.layout.main);
    registerForContextMenu(getListView()); // catch clicksif (!showList()) {
        TextViewtv=newTextView(this);
        tv.setText(getString(R.string.txt_list_empty));
        setContentView(tv);
    }
}

privatebooleanshowList() {
    finalCursorc= dbManager.fetchListData();
    startManagingCursor(c); // when the Activity finishes, the cursor is closedif (!c.moveToFirst())
        returnfalse;

    finalSimpleCursorAdaptermyAdapter=newSimpleCursorAdapter(
            this, android.R.layout.simple_list_item_2, c,
            newString[]{"name"} , newint[]{android.R.id.text1} );

    setListAdapter(myAdapter);
    returntrue;
}

Before spending a lot of time where you encounter problems, rather start where things are still working and take small steps for extensions. If you keep them simple, there's no great magic in using adapters.

Solution 5:

You might try inserting a ViewBinder for debugging. You can use it to inspect which values are being passed for which views in the SimpleCursorAdaptor. Or just use it to manually do the binding yourself.

Something like this should work:

adapter.setViewBinder(newSimpleCursorBinder());

and then

publicclassSimpleCursorBinderimplementsSimpleCursorAdpater.ViewBinder {
    publicbooleansetViewValue(View view, Cursor cursor, int columnIndex) {
        /* set breakpoints and examine incoming data. */// returning false, causes SimpleCursorAdapter to handing the bindingbooleanbound=false;

        StringcolumnName= cursor.getColumnName(columnIndex);
        TextViewbindingView=null;
        intviewId= view.getId();

        // could just use this opportunity to manually bindif (columnName.equals(SearchConstants._ID)) {
            bindingView = (TextView)(viewId == R.id._id ? view : null);
        } elseif (columnName.equals(SearchConstants.NAME_COLUMN)) {
            bindingView = (TextView)(viewId == R.id.name ? view : null);
        } elseif (columnName.equals(SearchConstants.SEARCH_COLUMN)) {
            bindingView = (TextView)(viewId == R.id.search ? view : null);
        }

        if (bindingView != null) {
            bindingView.setText(cursor.getString(columnIndex));
            bound = true;
        }

        return bound;
    }
}

Post a Comment for "Debugging Simplecursoradapter"