How to populate spinner from cursorloader?

白昼怎懂夜的黑 提交于 2020-01-11 06:49:33

问题


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. The spinner seem had received the data but I found no data in the dropdown list, although several dropdown items (with no text) had been created.

I believe problem is not from my provider because if I use the same cursor to retrieve the data and put it into array, then bind the array to the spinner, then it shows all items correctly.

Below is my code,

package com.supreme2u.shopper;

import android.app.Activity;
import android.app.LoaderManager;
import android.content.CursorLoader;
import android.content.Loader;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.app.NavUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.SimpleCursorAdapter;
import android.widget.Spinner;

import com.supreme2u.shopper.provider.ShopperProvider;

public class RecordActivity extends Activity  implements LoaderManager.LoaderCallbacks<Cursor>  {
private SimpleCursorAdapter sAdapter;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_record);

    getLoaderManager().initLoader(0, null, this);     
    sAdapter = new SimpleCursorAdapter(
            this,
            android.R.layout.simple_spinner_item,
            null, 
            new String[] {ShopperProvider.TAG_COLUMN_TAG}, 
            new int[] {R.id.spinner1},
            0);
    sAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
    Spinner v = (Spinner) findViewById(R.id.spinner1);
    v.setAdapter(sAdapter);
}

public Loader<Cursor> onCreateLoader(int arg0, Bundle arg1) {
    CursorLoader cursorLoader = new CursorLoader(
            this, 
            ShopperProvider.CONTENT_URI_TAGS, 
            ShopperProvider.TAG_COLUMNS, 
            null, 
            null, 
            null);
    return cursorLoader;
}

public void onLoadFinished(Loader<Cursor> arg0, Cursor arg1) {
    sAdapter.swapCursor(arg1);
}

public void onLoaderReset(Loader<Cursor> arg0) {
    sAdapter.swapCursor(null);
}

}

And my layout xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" />
</LinearLayout>

And from my ShopperProvider class, extracted,

public static final Uri CONTENT_URI_TAGS = Uri.parse("content://com.supreme2u.shopper.provider/tableTag");

public static final String TAG_COLUMN_ID = "_id";
public static final String TAG_COLUMN_TAG = "tagName";
public static final String[] TAG_COLUMNS = {"_id","tagName"};

回答1:


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);


来源:https://stackoverflow.com/questions/12434413/how-to-populate-spinner-from-cursorloader

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!