Hi,

I'm trying to display items from a database in a Spinner. I've read
the other posts on this but my items still aren't showing up and none
of
the posts show a complete solution.

To help debug the error, I added a ListView and the items from the
same db call display without any problem in the list. Thus, there's
some little detail that I can't find in my code for the Spinner. I'd
certainly appreciate someone pointing out the error.

To try to help me debug the code, after selecting an item from the
"invisible" list, I print out the selected item and it displays:
[EMAIL PROTECTED]

I can see that the spinner has lots of items and I can scroll through
them but they don't display anything.

Code:

public class TestListCursor extends ListActivity {
    private DBInterface mDbHelper;
    private Cursor notesCursor,
                           spinnerCursor;
    Spinner spnItems;
    TextView tvItem;
    SimpleCursorAdapter _itemsAdapter = null;
    Button btnSel;

    public void onCreate(Bundle icicle) {
        super.onCreate(icicle);
        setContentView(R.layout.notes_list);
        this.spnItems = (Spinner) findViewById(R.id.spn_NL_items);
        this.tvItem = (TextView) findViewById(R.id.tvNLSelection);
        this.btnSel = (Button) findViewById(R.id.btnNL);

        mDbHelper = new DBInterface(this);
        fillSpinner();
        fillData();

        this.btnSel.setOnClickListener (new View.OnClickListener() {
            public void onClick(View v) {

TestListCursor.this.tvItem.setText(TestListCursor.this.spnItems.getSelectedItem().toString());
            }
        });

    }

    private void fillData() {
        // Get all of the rows from the database and create the item
list
        notesCursor = null;
        notesCursor = mDbHelper.getItemsCursor();

        // Create an array to specify the fields we want to display in
the list (only TITLE)
        String[] from = new String[]{DbAdapter.ROUTE_NAME};

        // and an array of the fields we want to bind those fields to
(in this case just text1)
        int[] to = new int[]{R.id.text1};
        int i = 0;
                 if (notesCursor != null) {
                    startManagingCursor(notesCursor);
                // Now create a simple cursor adapter and set it to
display
                SimpleCursorAdapter notes =
                            new SimpleCursorAdapter(this,
R.layout.notes_row,
notesCursor, from, to);
                setListAdapter(notes);
                 }
    }

    private void fillSpinner() {
          spinnerCursor = mDbHelper.getItemsCursor();
              if (this.spinnerCursor != null) {
                         MMFLog.log("Workout fillItemsSpinner cursor
items: " +
this.spinnerCursor.count());
                         startManagingCursor(this.spinnerCursor);
                         this.spinnerCursor.next();
                         // Create an array to specify the fields we
want to display
in the spinner (only Name)
                         String[] names = new String[]
{DbAdapter.ROUTE_NAME};
                         int[] to = new int[]
{ android.R.layout.simple_spinner_item };

                         // Now create a simple cursor adapter and set
it to display
                         this._itemsAdapter =
                                    new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, this.spinnerCursor, names, to);

this._itemsAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                         this._itemsAdapter.bindView(this.spnItems,
this,
this.spinnerCursor);
                         this.spnItems.setAdapter(this._itemsAdapter);
                 } else {
                         Toast.makeText(this, "error: items cursor is
null",
Toast.LENGTH_SHORT);
                 }
    }

}

--------------------------
xml file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/
android"
        android:layout_height="275px"
        android:layout_width="wrap_content"
        android:orientation="vertical"
        >
    <Button android:id="@+id/btnNL"
          android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Spinner Selection"/>

    <Spinner android:id="@+id/spn_NL_items"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"/>

    <TextView android:id="@+id/tvNLSelection"
            android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>

    <ListView android:id="@+id/android:list"
          android:layout_width="wrap_content"
                android:layout_height="wrap_content"/>
        <TextView android:id="@+id/android:empty"
          android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="No Notes!"/>

</LinearLayout>

If you see the error or if you have a good working example that you'd
be willing to post, I'd certainly appreciate the feedback.

AN


On Mar 10, 2:23 pm, Charlie Collins <[EMAIL PROTECTED]> wrote:
> ASpinnerhas two sets of data, and two Views, what's shown in 
> theSpinneritself, and what's in the drop down. SimpleCursorAdapter is
> just one set of data.  It might work if you do what they have done in
> the samples, where they used ArrayAdapter with aSpinnerby *also*
> setting "dropDownViewResource".
>
> Have a look at samples\ApiDemos\src\com\google\android\samples\view
> \Spinner1.java for details.
>
> On Mar 10, 8:31 am, kaiping <[EMAIL PROTECTED]> wrote:
>
> > Hi, I want to load the values for aspinnerfrom a database.  But, no
> > matter what I do I cannot get thespinnerto bind to acursor
> > adapter.  Here is the Code I am using and the exact error:
>
> > Activity Code:
> > package com.jingsys.money;
>
> > import android.app.Activity;
> > import android.database.Cursor;
> > import android.os.Bundle;
> > import android.widget.ArrayAdapter;
> > import android.widget.SimpleCursorAdapter;
> > import android.widget.Spinner;
> > import android.widget.SpinnerAdapter;
>
> > import com.jingsys.money.data.AccountsDbAdapter;
> > import com.jingsys.money.data.MoneyDbAdapterFactory;
> > import com.jingsys.money.data.UtilDbAdapter;
>
> > public class AccountEdit extends Activity {
>
> >         //private AccountsDbAdapter mDb;
>
> >         @Override
> >         protected void onCreate(Bundle icicle) {
> >                 super.onCreate(icicle);
> >                 setContentView(R.layout.account_edit);
> >                 MoneyDbAdapterFactory factory = new 
> > MoneyDbAdapterFactory(this);
> >                 factory.open();
> >                 AccountsDbAdapter mDb = factory.getAccountNameDbAdapter();
>
> >                Spinnerspr = (Spinner) findViewById(R.id.account_type);
>
> >                Cursorc = UtilDbAdapter.getAccountTypes(factory);
> >                 SpinnerAdapter sp = new SimpleCursorAdapter(this,
> > R.layout.account_edit, c, new String[]
> > {MoneyDbAdapterFactory.ACCOUNT_TYPE}, new int[] {R.id.account_type});
>
> >                 spr.setAdapter(sp); //This line of code causes the error
>
> >         }
>
> > }
>
> > XML view code:
> > <?xml version="1.0" encoding="UTF-8"?>
> > <TableLayout xmlns:android="http://schemas.android.com/apk/res/
> > android"
> >         android:layout_width="fill_parent"
> >         android:layout_height="fill_parent"
> >         android:stretchColumns="1">
> >         <TableRow>
> >                 <TextView android:layout_width="wrap_content"
> >                         android:layout_height="wrap_content"
> >                         android:text="@string/account_name" />
> >                 <EditText android:id="@+id/account_name"
> >                   android:layout_width="wrap_content"
> >                         android:layout_height="wrap_content" />
> >         </TableRow>
>
> >         <TableRow>
> >                 <TextView android:layout_width="wrap_content"
> >                         android:layout_height="wrap_content"
> >                         android:text="@string/account_type" />
> >                 <Spinnerandroid:id="@+id/account_type"
> >                         android:layout_width="wrap_content"
> >                         android:layout_height="wrap_content"/>
> >         </TableRow>
>
> >         <TableRow>
> >                 <TextView android:layout_width="wrap_content"
> >                         android:layout_height="wrap_content"
> >                         android:text="@string/account_start_balance"/>
> >                 <EditText android:id="@+id/start_balance"
> >                         android:layout_width="wrap_content"
> >                         android:layout_height="wrap_content"/>
> >         </TableRow>
>
> >         <TableRow>
> >                 <TextView android:layout_width="wrap_content"
> >                         android:layout_height="wrap_content"
> >                         android:text="@string/account_currency"/>
> >         </TableRow>
>
> > </TableLayout>
>
> > Error Message:
> > Application Error:
> > com.jingsys.money
>
> > An error has occurred in com.jingsys.money. android.widget.Spinneris
> > not a view that can be bounds by this SimpleCursorAdapter.
>
> > I have tried everything I can.  Any help would be great, Thanks!
>
> > Kaiping
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[EMAIL PROTECTED]
Announcing the new M5 SDK!
http://android-developers.blogspot.com/2008/02/android-sdk-m5-rc14-now-available.html
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to