Android - Text dropdown/selection of Spinner does

2019-02-14 07:16发布

问题:

I'm using this sample code to populate the Spinner. Data is read from database. The selection displays correctly - in this case, it shows "Green" and "Red".

    Spinner spinnerColor = (Spinner) findViewById(R.id.spinnertProfile);

    mProfileDbHelper = new ProfileDbAdapter(this);
    mProfileDbHelper.open();

    Cursor profilesCursor = mProfileDbHelper.fetchAllProfiles();
    startManagingCursor(profilesCursor);

    // Create an array to specify the fields we want to display in the list
    String[] from = new String[] { ProfileDbAdapter.COL_PROFILE_TITLE };

    // and an array of the fields we want to bind those fields to
    int[] to = new int[] { R.id.textviewColors };

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter profilesAdapter = new SimpleCursorAdapter(this,
            R.layout.profile_color, profilesCursor, from,
            to);

    spinnerColor.setAdapter(profilesAdapter);

}

However, when I changed to use a different layout android.R.layout.simple_spinner_dropdown_item. The Spinner Text disappeared.

    // Now create a simple cursor adapter and set it to display
    SimpleCursorAdapter profilesAdapter = new SimpleCursorAdapter(this,
            R.layout.profile_color, profilesCursor, from,
            to);

    profilesAdapter
            .setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

    spinnerColor.setAdapter(profilesAdapter);

See snapshots of without and with simple_spinner_dropdown_item below:

Anything I may miss?

回答1:

Ok, what's happening is that when you're calling setDropDownViewResource you're replacing the layout you previously specified in the constructor. In your case R.layout.profile_color. SimpleCursorAdapter extends ResourceCursorAdapter and in the constructor sets the two layouts equal to each other.

public ResourceCursorAdapter(Context context, int layout, 
    Cursor c, boolean autoRequery) {

    super(context, c, autoRequery);
    mLayout = mDropDownLayout = layout;
    mInflater = (LayoutInflater)
        context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

The issue arises when you call setDropDownViewResource and change the drop down layout. The SimpleCursorAdapter will continue to use the same resource id bindings that you specified in the constructor.

What you should do is only specify the layout in the SimpleCursorAdapter's constructor. I suggest changing your code to as follows:

String[] from = new String[] { ProfileDbAdapter.COL_PROFILE_TITLE };
int[] to = new int[] { android.R.id.text1 }; // from simple_spinner_dropdown_item

SimpleCursorAdapter profilesAdapter = new SimpleCursorAdapter(this,
        android.R.layout.simple_spinner_dropdown_item, profilesCursor, from, to);

spinnerColor.setAdapter(profilesAdapter);

To get the results you want.

Basically don't use the setDropDownViewResource method. Or, if you do, and you change the resource id bindings, you'll have to call SimpleCursorAdapter#changeCursorAndColumns; however, that is probably overkill for the simple result you're trying to achieve.



回答2:

VERY IMPORTANT! I almost tore my hair out trying to figure out why my code wouldn't work. For those of you who may be reading this and your code still isn't working, make sure that int[] to = new int[] {android.R.id.text1} if you're using the other android layouts (such as android.R.layout.simple_spinner_dropdown_item). The code won't work if the integer array textview is not contained in the specified layout that you're using. While debugging my code I changed a lot of things around and forgot to make sure that these matched. SO, if you define your own layout, make sure that you use a textview from that layout.



回答3:

Qberticus,

Yes, you're right about the resource ID binding!!

However, if I started with android.R.layout.simple_spinner_dropdown_item, obviously the dropdown layout will show, but it is not pretty :-)

String[] from = new String[] { ProfileDbAdapter.COL_PROFILE_TITLE };
int[] to = new int[] { android.R.id.text1 }; // from simple_spinner_dropdown_item

SimpleCursorAdapter profilesAdapter = new SimpleCursorAdapter(this,
     android.R.layout.simple_spinner_dropdown_item, profilesCursor, from, to);

spinnerColor.setAdapter(profilesAdapter);

alt text http://www.freeimagehosting.net/uploads/c2647bd57d.gif

But now If I started with simple_spinner_item first, then setDropDownViewResource to simple_spinner_dropdown item, it now displays exactly what I'm looking for.

String[] from = new String[] { ProfileDbAdapter.COL_PROFILE_TITLE };
int[] to = new int[] { android.R.id.text1 }; // from simple_spinner_dropdown_item

SimpleCursorAdapter profilesAdapter = new SimpleCursorAdapter(this,
     android.R.layout.simple_spinner_item, profilesCursor, from, to);

profilesAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

spinnerColor.setAdapter(profilesAdapter);

Thank you so much for your help.