Hi,

I'm trying to display items from a database in a Spinner. I've read
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


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Beginners" 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-beginners?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to