SimpleCursorAdapter Problem (nothing on the display)

Basic Tutorials concerning: GUI, Views, Activites, XML, Layouts, Intents, ...

SimpleCursorAdapter Problem (nothing on the display)

Postby MobileBen » Sat Mar 22, 2008 4:29 pm

Hello.
Can anybody help me with the SimpleCursorAdapter. I have a database with two columns "names" and "age".
All names should be displayed like a list. But nothing happens. It is just empty. How can I display the names on the screen?

This is my Java:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  private void fillData() {
  2.  
  3.         // Get all of the rows from the database and create the item list
  4.  
  5.         myTeaCursor = myDBHelper.callUpAllTeas();
  6.  
  7.         startManagingCursor(myTeaCursor);
  8.  
  9.        
  10.  
  11.         // Create an array to specify the fields we want to display in the list (only NAME)
  12.  
  13.         String[] from = new String[]{TeaDB.KEY_NAME};
  14.  
  15.        
  16.  
  17.         // and an array of the fields we want to bind those fields to (in this case just text1)
  18.  
  19.         int[] to = new int[]{R.id.list};
  20.  
  21.        
  22.  
  23.         ListAdapter test = new SimpleCursorAdapter(this, R.layout.main, myTeaCursor, from, to);
  24.  
  25.         setListAdapter(test);
  26.  
  27.     }
  28.  
  29.  
Parsed in 0.031 seconds, using GeSHi 1.0.8.4



This is my XML:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1.  
  2. <?xml version="1.0" encoding="utf-8"?>
  3.  
  4. <LinearLayout
  5.  
  6.                 android:id="@+id/background1"
  7.  
  8.                 android:orientation="vertical"
  9.  
  10.        android:layout_width="fill_parent"
  11.  
  12.        android:layout_height="fill_parent"
  13.  
  14.        xmlns:android="http://schemas.android.com/apk/res/android">
  15.  
  16.  
  17.  
  18.         <LinearLayout
  19.  
  20.                 android:id="@+id/background2"
  21.  
  22.                 android:orientation="vertical"
  23.  
  24.        android:layout_width="fill_parent"
  25.  
  26.        android:layout_height="wrap_content"
  27.  
  28.        xmlns:android="http://schemas.android.com/apk/res/android">
  29.  
  30.          <TextView
  31.  
  32.                 android:id="@+id/list"
  33.  
  34.            android:layout_width="fill_parent"
  35.  
  36.            android:layout_height="wrap_content"
  37.  
  38.            android:layout_weight="1">
  39.  
  40.          </TextView>
  41.  
  42.           <ListView
  43.  
  44.                 android:id="@android:id/list"
  45.  
  46.            android:layout_width="fill_parent"
  47.  
  48.            android:layout_height="wrap_content"
  49.  
  50.            android:layout_weight="1">
  51.  
  52.          </ListView>  
  53.  
  54.         </LinearLayout>
  55.  
  56. </LinearLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4



The method "callUpAllTeas" looks like this:

Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. public Cursor callUpAllTeas() {
  2.  
  3.         return myDb.query(DATABASE_TABLE, new String[] {
  4.  
  5.                 KEY_ROWID, KEY_NAME, KEY_BREWTIME}, null, null, null, null, null);
  6.  
  7.     }
Parsed in 0.031 seconds, using GeSHi 1.0.8.4
MobileBen
Freshman
Freshman
 
Posts: 4
Joined: Thu Mar 20, 2008 4:10 am

Top

Postby Michael T » Thu Sep 25, 2008 2:25 am

I had a problem just like this. Everything seemed to be working except the data was not showing up. I finally had a "slap hand on forehead" moment when I realized I my layout XML file I was using was not main.xml. Make sure the R.layout.the_correct_layout in the SimpleCursorAdapter constructor is correct.
Michael T
Freshman
Freshman
 
Posts: 2
Joined: Thu Sep 25, 2008 2:14 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 2 guests