tags:

views:

28

answers:

1

Hi!

I am just starting out with Android and I am attempting my first test app, but I am a bit stuck.

I have an SQLite database and a user is selecting a picture and a reference to this a long with a short description is getting stored in the database. This is all working fine, now I want to bind the images from to database to a gallery, but I am a bit confused how I do this.

My layout xml is as follows:

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout
 xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent" 
 android:layout_height="fill_parent">

  <Gallery
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/Weeksgallery"    
      android:layout_width="fill_parent"   
      android:layout_height="wrap_content"/>

 <Button android:id="@+id/ok"     
       android:layout_width="wrap_content"   
       android:layout_height="wrap_content"
       android:layout_below="@+id/text1"    
       android:layout_marginLeft="10dip"    
       android:text="Take a Picture"   
       android:onClick="TakePicture" />
 </LinearLayout>

I want the gallery with a button below

My on create function I have the following:

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

  mDbHelper = new DbAdapter(this);
  mDbHelper.open();
  Cursor cur = mDbHelper.fetchDaysImages();

  //array of fields to display
  String[] from = new String[] { mDbHelper.KEY_ROWID,mDbHelper.KEY_PICREF};

  int[] to = new int[] { R.id.Weeksgallery };

  // Now create a simple cursor adapter and set it to display
  SimpleCursorAdapter images = new SimpleCursorAdapter(this,
    R.layout.main, cur, from, to);
  setListAdapter(images);

  mDbHelper.close();
 }

I am using the notepad tutorial as an example and my class extends ListActivity.

When I run it I get the error

Your content must have a ListView whose id attribute is 'android.R.id.list'

I'm assuming I am using the wrong type of binding, but I can't seem to find anywhere that tells me how to bind from an sqlite DB to a gallery. I also want to add a reference to the rowId in the gallery so when the image is clicked it will open the relevant page (so it can show the message)

Can anyone help?

Thanks

Bex

A: 

Your Activity inherits ListActivity, right? When you use ListActivity, you must make sure there is a ListView whose id is "android.R.id.list" in the layout of your Activity, that is what the log told you.

If there is no ListView in your Activity, then don't use ListActivity. If you want to set adapter to Gallery, use Gallery`s setAdapter method.

Tony
I have updated the class now, and am getting a different error "android.widget.Gallery is not a view that can be bounds by this SimpleCursorAdapter" does the error mean I can't user the simplecursoradapter to bind it, or am I doing something wrong in the simplecursoradapter? Also do I need to somehow use the imageadapter class as used in the hello gallery tutorial?
Bex
I don't think you can use SimpleCursorAdapter here. Because SimpleCursorAdapter is an adapter that maps columns from a cursor to TextViews or ImageViews. May you can implement your own Adapter to archive this. Try it, won't be too hard.
Tony
um... I'll give it ago, do you have any pointers as I am very new to Android (and Java) so am still struggling with the most basic stuff! I would have thought there would be a tutorial somewhere about as I'd have thought that what I am trying to to is quite common.
Bex