How to populate spinner from cursorloader?

后端 未结 1 1203
闹比i
闹比i 2021-01-14 23:24

I have spent days to figure this out but no luck. Wish I could get answer from here. I tried to load data into spinner from my content provider using the cursorLoader method

相关标签:
1条回答
  • 2021-01-15 00:05

    The problem lies in your construction of the SimpleCursorAdapter, specifically the second and fifth parameters (layout and to). From the docs:

    layout: resource identifier of a layout file that defines the views for this list item. The layout file should include at least those named views defined in "to"

    to: The views that should display column in the "from" parameter. These should all be TextViews. The first N views in this list are given the values of the first N columns in the from parameter. Can be null if the cursor is not available yet.

    So you are correct to use android.R.layout.simple_spinner_item for layout. However, the views passed for to need to be TextViews and contained within android.R.layout.simple_spinner_item: i.e. android.R.id.text1 instead of R.id.spinner1.

    In short, use this construction instead:

    sAdapter = new SimpleCursorAdapter(
            this,
            android.R.layout.simple_spinner_item,
            null, 
            new String[] {ShopperProvider.TAG_COLUMN_TAG}, 
            new int[] {android.R.id.text1},
            0);
    
    0 讨论(0)
提交回复
热议问题