gpt4 book ai didi

android - 如何使用 CursorLoader 填充微调器?

转载 作者:行者123 更新时间:2023-11-30 02:06:51 25 4
gpt4 key购买 nike

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

我有一个微调器的 IndicatorFragment,我将把它包含到其他一些 fragment 中(通过 <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>

然后我在使用 SimpleCursorAdapter 的地方编写了 IndicatorFragment.class和 Loader用于从数据库填充数据:

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> 添加了带有微调器的 fragment 。属性:

<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


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

最佳答案

您必须初始化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;
}

编码愉快!

关于android - 如何使用 CursorLoader 填充微调器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30540939/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com