Populate Spinner from database

by Brad Stintson » Sat, 12 Mar 2011 23:10:19 GMT


Sponsored Links
 How to populate spinner from database?

Following code is not working in my case.

This method shows database entries in spinner


private void fillData() {
     spin_pro= (Spinner)findViewById(R.id.spin_profile);
        Cursor profilesCursor = mDbHelper.retrieveAllProfiles();
        startManagingCursor(profilesCursor);


        String[] from = new String[]{DBAdapter.KEY_PRONAME};

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

        SimpleCursorAdapter profile =
            new SimpleCursorAdapter(this, R.layout.profile_list,
profilesCursor, from, to);

        spin_pro.setAdapter(profile);
    }


And this is database class to retrieve data

    public Cursor retrieveAllProfiles()
    {

        return mDb.query(DATABASE_TABLE1, new String[] {KEY_PROID,
                    KEY_PRONAME, KEY_PASSWORD}, null, null, null, null,
null);
    }

-- 



Re: Populate Spinner from database

by Mark Murphy » Sat, 12 Mar 2011 23:22:55 GMT


 


Please consider defining "not working".

-- 
Mark Murphy (a Commons Guy)
 http://commonsware.com  |  http://github.com/commonsguy 
 http://commonsware.com/blog  |  http://twitter.com/commonsguy 

_The Busy Coder's Guide to *Advanced* Android Development_ Version 1.9
Available!

-- 


Sponsored Links


Re: Populate Spinner from database

by davemac » Sun, 13 Mar 2011 04:46:42 GMT


 As you appear to be using a single text view in your spinner, consider
using some standard layouts and views. Instead of
R.layout.profile_list, try using android.R.layout.simple_list_item_1,
and instead of your R.id.tvDBViewRow, replace that with
android.R.id.text1. My guess is that you're getting hung up on where
these resources get specified and how. Android provides a bunch of
standard ones that you can easily use. To see the actual specification
of the layout, go to the Android SDK directory, look under the
appropriate platform directory for your target build, then under data/
res/layout. There you will find simple_list_item_1.xml which is a
layout with a single TextView in it, with an ID of text1. Anyway, try
that and see if you get better results.

- dave






-- 



Other Threads

1. Calling close() on a Cursor that's backing a ListView



Bear in mind that Cursors hold a copy of the query result set. The more
Cursors you hold onto, the more memory you consume.

Going back to your original problem, I don't see how you're getting into
trouble. You have N Cursors. One of these Cursors is in a ListView. You
know which one of these Cursors is in a ListView (your statement "so do
not know how a given cursor is being used" is false). The other Cursors
are, hopefully, not in a ListView. So you have:

-- 1 ListView
-- 1 CursorAdapter
-- N Cursors

I assume you are not calling close() on the Cursor that is in the
CursorAdapter, as the user is kinda looking at it. None of the other
Cursors should be connected to the UI, and so you should not get that
exception.

If you are getting the exception on a Cursor that is not held by any
CursorAdapter/ListView, that might be a bug in Android, or perhaps some
other step you need to do to completely detach the Cursor from the
CursorAdapter when you swap in another Cursor.

If you actually have N ListViews and N CursorAdapters, please
reconsider. That's gobs more memory you're taking up and, among other
things, leaves you open for this error.

-- 
Mark Murphy (a Commons Guy)
http://commonsware.com | http://twitter.com/commonsguy

_Android Programming Tutorials_ Version 2.0 Available!

-- 

2. NPE in onItemClickListener

I have a (Multi)AutoCompleteTextView which uses an item click
listener. I received a crash report which shows a NPE in
onItemClick(). In onItemClick, I use the selected view (param #2 of
the listener) to do manipulation to the text, but looking at the
source code for AutoCompleteTextView and it has the following block:

// Note that we don't have a View here, so we will need to
                // supply null.  Hopefully no existing apps crash...
                mItemClickListener.onItemClick(list, null,
completion.getPosition(),
                        completion.getId());

Shouldn't the onItemClick listener always receive a non-null value for
view? The docs (http://min.ie/3mr) say:
view - The view within the AdapterView that was clicked (this will be
a view provided by the adapter)

Am I correct in assuming this is a bug? The stack trace can be seen
here: http://paste2.org/p/765292

-- 

3. How do you change values in another activity from a activity?

4. disabling GMail access ... changing GMail password doesn't

5. "unable to open database file" after OS upgrade

6. Calling close() on a Cursor that's backing a ListView

7. Calling close() on a Cursor that's backing a ListView