我有一个ListFragment,它通过加载器加载它的数据。 到目前为止,我使用内置的R.layout.list_item和SimpleCursorAdapter。 但是现在,我必须在每个列表项上做一些自定义格式化,并且需要使用自定义列表项布局,所以我必须将我的ListFragment转换为使用我的自定义Cursor Adapter。 我不明白的是,如何绑定数据,这些数据是异步加载到我的列表视图中的。到目前为止,我的ListFragment和GewichtListViewAdapter。
这是我的listfragment:
public class GewichtListViewAdapter extends CursorAdapter {
public GewichtListViewAdapter(Context context, Cursor c, boolean autoRequery) {
super(context, c, autoRequery);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
return LayoutInflater.from(context).inflate(R.layout.listview_item_gewicht, parent, false);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
// Find fields to populate in inflated template
TextView gewichtTextView = (TextView)view.findViewById(R.id.gewichtTextView);
TextView gemessenAmTextView = (TextView)view.findViewById(R.id.gemessenAmTextView);
// Extract properties from cursor
String body = cursor.getString(cursor.getColumnIndexOrThrow("body"));
int priority = cursor.getInt(cursor.getColumnIndexOrThrow("priority"));
// Populate fields with extracted properties
long geburtsdatumLong = cursor.getLong(cursor.getColumnIndex(GewichtContract.Columns.GEMESSEN_AM));
LocalDate geburtsDatumLocalDate = new LocalDate(geburtsdatumLong);
DateTimeFormatter fmt = DateTimeFormat.mediumDate();
String str = fmt.print(geburtsDatumLocalDate);
gemessenAmTextView.setText(str);
double gewichtDouble = cursor.getDouble(cursor.getColumnIndex(GewichtContract.Columns.GEWICHT));
BigDecimal gewichtBigDecimal = new BigDecimal(gewichtDouble, MathContext.DECIMAL32);
gewichtTextView.setText(String.format("%.1f", gewichtBigDecimal));
}
}
这是带有Loader的ListFragment的摘录:
public class GewichtListFragment extends ListFragment implements
LoaderManager.LoaderCallbacks<Cursor>{
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = { GewichtContract.Columns._ID, GewichtContract.Columns.GEMESSEN_AM, GewichtContract.Columns.GEWICHT };
CursorLoader cursorLoader = new CursorLoader(getActivity(),
GewichtContract.CONTENT_URI, projection, null, null, null);
return cursorLoader;
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor cursor) {
adapter.swapCursor(cursor);
}
@Override
public void onLoaderReset(Loader<Cursor> loader) {
adapter.swapCursor(null);
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
// TODO: Change Adapter to display your content
//setListAdapter(new ArrayAdapter<DummyContent.DummyItem>(getActivity(),
// android.R.layout.simple_list_item_1, android.R.id.text1, DummyContent.ITEMS));
String[] uiBindFrom = { GewichtContract.Columns.GEWICHT, GewichtContract.Columns.GEMESSEN_AM };
int[] uiBindTo = { R.id.gewichtTextView, R.id.gemessenAmTextView };
getLoaderManager().initLoader(GEWICHT_LIST_LOADER, null, this);
adapter = new SimpleCursorAdapter(
getActivity(), R.layout.listview_item_gewicht, null, uiBindFrom, uiBindTo,
CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(adapter);
}
}
如果使用简单游标适配器,它将附加在onCreate()中。但是,我需要一个光标的引用,而我在onCreate()中没有。此外,此时此日期(据我所知)尚未加载,所以我不知道如何绑定/附加此处。 任何人都可以告诉我ListFragments,自定义光标适配器和加载器如何协同工作?
答案 0 :(得分:0)
我的解决方案:
在onCreate()中创建一个空的MatrixCursor并用它构造我的GewichtListViewAdapter:
String[] columnNames = {GewichtContract.Columns._ID,
GewichtContract.Columns.GEMESSEN_AM,
GewichtContract.Columns.GEWICHT,
GewichtContract.Columns.HUND_ID};
MatrixCursor matrixCursor = new MatrixCursor(columnNames);
gewichtListViewAdapter = new GewichtListViewAdapter(getActivity(), matrixCursor, true);
setListAdapter(gewichtListViewAdapter);
在onCreateLoader()中:
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
String[] projection = { GewichtContract.Columns._ID,
GewichtContract.Columns.GEMESSEN_AM,
GewichtContract.Columns.GEWICHT,
GewichtContract.Columns.HUND_ID
};
CursorLoader cursorLoader = new CursorLoader(getActivity(),
GewichtContract.CONTENT_URI, projection, null, null, GewichtContract.Columns.GEMESSEN_AM);
return cursorLoader;
}