How to set a default item in a spinner drop down list

by MMC2 » Tue, 26 Jan 2010 21:10:21 GMT


Sponsored Links
 How can I use code to set a default item in a spinner drop down list

--



Other Threads

1. cannot get a spinner to bind to a cursor

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






--~--~---------~--~----~------------~-------~--~----~

2. java.text.SimpleDateFormat appears to be broken

Hello all. On the latest M5 I've tried creating a DateFormat object as
follows:

    DateFormat df = new SimpleDateFormat("YYYY-MM-DD HH:MM:SS");
    df.setTimeZone( TimeZone.getTimeZone( "UTC" ) );

The SimpleDateFormat reference fails every time; the TimeZone
reference does not cause a problem. I have very similar code that
worked fine on the initial versions of the SDK. Just for grins, I
changed "UTC" to "GMT" with the same result. The idea is to have a
simple class that encapsulates a DateFormat object that will convert
an ANSI/ISO date as understood by SQLite to/from a Date/String in the
UTC timezone. So for a class ConvertDate:

   public Date parse(String dateTime);

and

   public String format(Date date);

The logcat snippet from the emulator for the failure on the
SimpleDateFormat reference follows. Thanks for any help.

...
D/dalvikvm(  656): Exception Ljava/lang/ClassNotFoundException; from
PathClassLoader.java:205 not caught locally
D/dalvikvm(  656): NOTE: loadClass
'org.apache.harmony.luni.internal.locale.ISO4Currencies_en_US'
0x40018950 threw an exception
D/dalvikvm(  656): Exception Ljava/lang/ClassNotFoundException; from
PathClassLoader.java:205 not caught locally
D/dalvikvm(  656): NOTE: loadClass
'org.apache.harmony.luni.internal.locale.ISO4Currencies_en' 0x40018950
threw an exception
D/dalvikvm(  656): Exception Ljava/lang/ClassNotFoundException; from
PathClassLoader.java:205 not caught locally
D/dalvikvm(  656): NOTE: loadClass
'org.apache.harmony.luni.internal.locale.ISO4CurrenciesToDigits_en_US'
0x40018950 threw an exception
D/dalvikvm(  656): Exception Ljava/lang/ClassNotFoundException; from
PathClassLoader.java:205 not caught locally
D/dalvikvm(  656): NOTE: loadClass
'org.apache.harmony.luni.internal.locale.ISO4CurrenciesToDigits_en'
0x40018950 threw an exception
D/dalvikvm(  656): Exception Ljava/lang/ClassNotFoundException; from
PathClassLoader.java:205 not caught locally
D/dalvikvm(  656): NOTE: loadClass
'org.apache.harmony.text.internal.nls.messages_en_US' 0x40018950 threw
an exception
D/dalvikvm(  656): Exception Ljava/lang/ClassNotFoundException; from
PathClassLoader.java:205 not caught locally
D/dalvikvm(  656): NOTE: loadClass
'org.apache.harmony.text.internal.nls.messages_en' 0x40018950 threw an
exception
D/dalvikvm(  656): Exception Ljava/lang/ClassNotFoundException; from
PathClassLoader.java:205 not caught locally
D/dalvikvm(  656): NOTE: loadClass
'org.apache.harmony.text.internal.nls.messages' 0x40018950 threw an
exception
...
--~--~---------~--~----~------------~-------~--~----~

3. Is it possible to use built-in contact list application to pick contacts?

4. View Parents

5. Aver the air download of application

6. Null Pointer Exception !

7. Android clip