如何使用CursorLoader填充微调器?

时间:2015-05-30 00:25:21

标签: android android-spinner simplecursoradapter android-cursorloader

我正在尝试从cursorloader填充一个微调器。

我有一个用于微调器的IndicatorFragment,我将把它包含在其他一些片段中(通过<include>标签)。

首先,我检查了数据是否在数据库中(ContentProvider),一切正常。

第二步是编写一个xml文件:

<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="3"
            android:text="@string/con_lbl_input_indicator"
            android:textSize="22sp"
            android:textStyle="bold"
            android:layout_marginTop="8dp"
            android:layout_marginLeft="5dp"/>

        <Spinner
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="2"
            android:id="@+id/indicators_spinner"
            android:layout_gravity="bottom"
            android:spinnerMode="dropdown" />
    </LinearLayout>
</merge>

然后我编写了IndicatorFragment.class,我正在使用SimpleCursorAdapterLoader来填充数据库中的数据:

public class IndicatorsFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor>{

    private static final String LOG_TAG = IndicatorsFragment.class.getSimpleName();

    private static final int INDICATOR_LOADER_ID = 0;

    private Spinner spIndicator;
    private SimpleCursorAdapter spinAdapter;

    private static final String[] INDICATOR_PROJECTION = {
            ChemToolsContract.IndicatorsEntry._ID,
            ChemToolsContract.IndicatorsEntry.COLUMN_NAME
    };

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View rootView = inflater.inflate(R.layout.fragment_indicators, container, false);

        spIndicator = (Spinner) rootView.findViewById(R.id.indicators_spinner);

        spinAdapter = new SimpleCursorAdapter(getActivity(),
                android.R.layout.simple_spinner_item, null,
                new String[]{ChemToolsContract.IndicatorsEntry.COLUMN_NAME},
                new int[] {android.R.id.text1}, 0);

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

        spIndicator.setAdapter(spinAdapter);

        spIndicator.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

            }
        });

        return rootView;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        getLoaderManager().initLoader(INDICATOR_LOADER_ID, null, this);
        spIndicator.setOnItemClickListener(null);
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onPause() {
        getLoaderManager().destroyLoader(INDICATOR_LOADER_ID);
        super.onPause();
    }

    @Override
    public Loader<Cursor> onCreateLoader(int id, Bundle args) {
        Uri baseUri = ChemToolsContract.IndicatorsEntry.CONTENT_URI;

        return new CursorLoader(getActivity(), baseUri, INDICATOR_PROJECTION, null, null, null);
    }

    @Override
    public void onLoadFinished(Loader<Cursor> loader, Cursor data) {
        spinAdapter.swapCursor(data);
    }

    @Override
    public void onLoaderReset(Loader<Cursor> loader) {
        spinAdapter.swapCursor(null);
    }
}

最后一件事是xml文件,我使用<include>属性在spinner中添加了我的片段:

<include layout="@layout/fragment_indicators"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="10dp"/>

但是在应用程序启动时我没有在微调器中看到任何数据:

http://i.stack.imgur.com/bgi9R.png

所以我不知道我做错了什么:从数据库加载数据或将带有微调器的片段包含到另一个?

2 个答案:

答案 0 :(得分:1)

您必须初始化 Loader ,此行已丢失。我建议包括这一行:

getLoaderManager().initLoader(INDICATOR_LOADER_ID, null, this);

onCreateView 方法内部,位于return rootView;行的正上方。在这种情况下,该方法应该是这样的:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_indicators, container, false);

    spIndicator = (Spinner) rootView.findViewById(R.id.indicators_spinner);

    spinAdapter = new SimpleCursorAdapter(getActivity(),
            android.R.layout.simple_spinner_item, null,
            new String[]{ChemToolsContract.IndicatorsEntry.COLUMN_NAME},
            new int[] {android.R.id.text1}, 0);

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

    spIndicator.setAdapter(spinAdapter);

    spIndicator.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        }
    });
    getLoaderManager().initLoader(INDICATOR_LOADER_ID, null, this);
    return rootView;
}

快乐的编码!

答案 1 :(得分:0)

很奇怪你没有遇到以下错误(因为我做了)。

java.lang.RuntimeException: setOnItemClickListener cannot be used with a spinner. 

尽管如此,@ Rynel的答案很棒。我只是想做一个小编辑。而不是

spIndicator.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    }
});
你写了

spIndicator.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        // you code here
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {
        // you code here
    }
});

取自post