如何从游标加载程序填充微调器



我花了几天时间来解决这个问题,但运气不佳。希望我能从这里得到答案。我尝试使用cursorLoader方法将数据从内容提供商加载到微调器中。微调器似乎已经收到了数据,但我在下拉列表中没有发现任何数据,尽管已经创建了几个下拉项(没有文本)。

我相信问题不是来自我的提供者,因为如果我使用同一个游标来检索数据并将其放入数组,然后将数组绑定到微调器,那么它就会正确地显示所有项目。

下面是我的代码,

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

我的布局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>

从我的ShopperProvider类中提取

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"};

问题在于SimpleCursorAdapter的构造,特别是第二个和第五个参数(layoutto)。来自文档:

layout:布局文件的资源标识符,用于定义此列表项的视图。布局文件应该至少包括在"视图"中定义的那些命名视图;至";

:应该在";从";参数这些都应该是TextViews。此列表中的前N个视图被赋予from参数中前N列的值。如果光标还不可用,则可以为null。

因此,对于布局,使用android.R.layout.simple_spinner_item是正确的。但是,为传递给的视图需要是TextViews,并包含在android.R.layout.simple_spinner_item中:即android.R.id.text1而不是R.id.spinner1

简而言之,使用这种结构:

sAdapter = new SimpleCursorAdapter(
        this,
        android.R.layout.simple_spinner_item,
        null, 
        new String[] {ShopperProvider.TAG_COLUMN_TAG}, 
        new int[] {android.R.id.text1},
        0);

最新更新