Blank display of items-Spinner that's filled from DB cursor

Put your problem here if it does not fit any of the other categories.

Blank display of items-Spinner that's filled from DB cursor

Postby jdl » Wed Apr 02, 2008 2:21 am

Thank you plusminus for all of your wonderful tutorials!

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:
android.database.sqlite.SQLiteCursor@400b0138

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>
----------------
The db call is very simple:

public Cursor getItemsCursor() {
return mDb.query(ITEMS_TABLE, ITEM_NAME_FIELDS, ITEM_NAME + " != '' ", null, null, null, ITEM_NAME);
}

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.

jdl
jdl
Freshman
Freshman
 
Posts: 4
Joined: Thu Jan 03, 2008 12:58 am
Location: USA

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 20 guests