TabHost, ListActivity and SimpleCursorAdapter

Put problem concerning Views, Layouts and other XML-Resources (like AndroidManifest) here.

TabHost, ListActivity and SimpleCursorAdapter

Postby bones237 » Fri Jan 01, 2010 1:56 am

Hey guys,
new to android development......
I'm having trouble with displaying my list (which will be populated by a db query)...
I'm creating a TabHost as such (taken from the ApiDemos sample app)....

right now there's just one tab but i will be adding more as time goes on.
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class iWatchr extends TabActivity {
  3.  
  4.  
  5.  
  6.     @Override
  7.  
  8.     protected void onCreate(Bundle savedInstanceState) {
  9.  
  10.         super.onCreate(savedInstanceState);
  11.  
  12.         final TabHost tabHost = getTabHost();
  13.  
  14.  
  15.  
  16.         tabHost.addTab(tabHost.newTabSpec("tab1")
  17.  
  18.                 .setIndicator("History", getResources().getDrawable(R.drawable.history))
  19.  
  20.                 .setContent(new Intent(this, Dates.class)));
  21.  
  22.          }
  23.  
  24. }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4



As you can see i'm using an Intent to provide the content for tab1 via the Dates class.
In the Dates class, the relevant code is.....
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1.  
  2. public class Dates extends ListActivity {
  3.  
  4.  
  5.  
  6.     private DbHelper mDbHelper;
  7.  
  8.    
  9.  
  10.         @Override
  11.  
  12.         public void onCreate(Bundle savedInstanceState) {
  13.  
  14.                 super.onCreate(savedInstanceState);
  15.  
  16.                 mDbHelper = new DbHelper(this);
  17.  
  18.                 mDbHelper.open();
  19.  
  20.                 fillData();
  21.  
  22.         }
  23.  
  24.        
  25.  
  26.         private void fillData() {
  27.  
  28.                 Cursor datesCursor = mDbHelper.fetchAllDates();
  29.  
  30.                 startManagingCursor(datesCursor);
  31.  
  32.                
  33.  
  34.  
  35.  
  36.                 String[] from = new String[]{DbHelper.DATE_KEY};
  37.  
  38.                 int[] to = new int[]{android.R.id.text1};
  39.  
  40.                
  41.  
  42.                 SimpleCursorAdapter dates = new SimpleCursorAdapter(this, android.R.layout.simple_list_item_1, datesCursor, from, to);
  43.  
  44.                 setListAdapter(dates);
  45.  
  46.                
  47.  
  48.         //      setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, from));
  49.  
  50.         //      getListView().setTextFilterEnabled(true);
  51.  
  52.                
  53.  
  54.         }
  55.  
  56.  
Parsed in 0.034 seconds, using GeSHi 1.0.8.4


When this is run app crashes on startup ("...stopped unexpectedly").
as for the database query returning the cursor in "fetchAllDates"....log statements tell me that is all fine. The cursor is empty but it should be because i haven't inserted any data into database yet.
I believe the problem is with the line related to displaying the data via a SimpleCursorAdapter. If i comment that line out (where it is created and displayed) and instead uncomment the line where setListAdapter is done with an ArrayAdapter, app runs fine...value of DbHelper.DATE_KEY is displayed since we are just passing in the 'from' array.

Anyone have any suggestions or see what i am doing wrong?

I have been developing in ObjC for the past year and a half on the iphone so it's taken me some time to freshen up my java skills but it's going ok. I have been working on this problem for about 2-3 hours and i think getting used to the android frameworks is what i think will take me a bit.

Thanks!!!
bones237
Freshman
Freshman
 
Posts: 3
Joined: Fri Jan 01, 2010 12:54 am

Top

Postby bones237 » Fri Jan 01, 2010 6:19 am

That sucked!!!

Bad habits, i guess....
Thanks to LogCat.....my primary key column wasn't called "_id".
I didn't realize it MUST be "_id".
Works now
bones237
Freshman
Freshman
 
Posts: 3
Joined: Fri Jan 01, 2010 12:54 am

Top

Return to View, Layout & Resource Problems

Who is online

Users browsing this forum: No registered users and 7 guests