SimpleCursorAdapter Problem (nothing on the display)

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

SimpleCursorAdapter Problem (nothing on the display)

Postby EricJason » Sat Jan 08, 2011 6:17 am

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.         // Get all of the rows from the database and create the item list
  3.         myTeaCursor = myDBHelper.callUpAllTeas();
  4.         startManagingCursor(myTeaCursor);
  5.        
  6.         // Create an array to specify the fields we want to display in the list (only NAME)
  7.         String[] from = new String[]{TeaDB.KEY_NAME};
  8.        
  9.         // and an array of the fields we want to bind those fields to (in this case just text1)
  10.         int[] to = new int[]{R.id.list};
  11.        
  12.         ListAdapter test = new SimpleCursorAdapter(this, R.layout.main, myTeaCursor, from, to);
  13.         setListAdapter(test);
  14.     }
  15.  
Parsed in 0.032 seconds, using GeSHi 1.0.8.4



This is my XML:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout
  3.                 android:id="@+id/background1"
  4.                 android:orientation="vertical"
  5.        android:layout_width="fill_parent"
  6.        android:layout_height="fill_parent"
  7.        xmlns:android="http://schemas.android.com/apk/res/android">
  8.  
  9.         <LinearLayout
  10.                 android:id="@+id/background2"
  11.                 android:orientation="vertical"
  12.        android:layout_width="fill_parent"
  13.        android:layout_height="wrap_content"
  14.        xmlns:android="http://schemas.android.com/apk/res/android">
  15.          <TextView
  16.                 android:id="@+id/list"
  17.            android:layout_width="fill_parent"
  18.            android:layout_height="wrap_content"
  19.            android:layout_weight="1">
  20.          </TextView>
  21.           <ListView
  22.                 android:id="@android:id/list"
  23.            android:layout_width="fill_parent"
  24.            android:layout_height="wrap_content"
  25.            android:layout_weight="1">
  26.          </ListView>  
  27.         </LinearLayout>
  28. </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.         return myDb.query(DATABASE_TABLE, new String[] {
  3.                 KEY_ROWID, KEY_NAME, KEY_BREWTIME}, null, null, null, null, null);
  4.     }
Parsed in 0.032 seconds, using GeSHi 1.0.8.4
EricJason
Junior Developer
Junior Developer
 
Posts: 23
Joined: Mon Apr 05, 2010 7:46 am

Top

Re: SimpleCursorAdapter Problem (nothing on the display)

Postby jiqqaman » Tue Jan 11, 2011 9:36 am

Hi, Im new to android development, try changing the following:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. ListAdapter test = new SimpleCursorAdapter(this, R.layout.main, myTeaCursor, from, to);
  2.         setListAdapter(test);
  3.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


to:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. SimpleCursorAdapter test = new SimpleCursorAdapter(this, R.layout.main, myTeaCursor, from, to);
  2.         setListAdapter(test);
  3.  
Parsed in 0.036 seconds, using GeSHi 1.0.8.4


the main change here is using SimpleCursorAdater while you were using ListAdapter. Hope it works!
jiqqaman
Freshman
Freshman
 
Posts: 6
Joined: Sat Sep 25, 2010 12:23 am

Top

Return to Novice Tutorials

Who is online

Users browsing this forum: No registered users and 6 guests