在我的onCreate:
ListView定义:
listView = (ListView) findViewById(R.id.listView1);
装载程序经理:
getLoaderManager().initLoader(0, null, new LoaderManager.LoaderCallbacks<Cursor>() {
@Override
public Loader<Cursor> onCreateLoader(int id, Bundle args) {
return new CursorLoader(ListDetailActivity.this,
ReceiptProvider.URI_RECEIPT, Receipt.FIELDS, null, null,
null);
}
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
System.out.println("Cursor: " + c);
}
@Override
public void onLoaderReset(Loader<Cursor> arg0) {
}
});
我的自定义光标适配器:
private class CurAdapter extends CursorAdapter {
public CurAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
TextView tv = (TextView) view.findViewById(R.id.textView2);
String name = (cursor.getString(cursor.getColumnIndexOrThrow("receipt_name")));
tv.setText(name);
//setImage(image, iv);
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view = LayoutInflater.from(context).inflate(R.layout.list_layout, null);
return view;
}
}
如何将光标设置为现在列出?我知道如何为SimpleCursorAdapter做到这一点,但我对如何为光标适配器,任何提示做这些无能为力?
现在我这样做:
@Override
public void onLoadFinished(Loader<Cursor> loader, Cursor c) {
CurAdapter Cur = new CurAdapter(ListDetailActivity.this, c, 0);
listView.setAdapter(Cur);
}
它工作正常,但我不确定这是否是正确的方法?
答案 0 :(得分:1)
也许如此:
listView.setAdapter(new CurAdapter(this, cursor));
答案 1 :(得分:1)
您需要在init方法中将适配器设置为listview,并将对适配器的引用保存为字段。可以将null作为游标参数传递。 CursorAdapter类有changeCursor和swapCursor两种方法,使用你需要在onLoadFinished中设置光标的方法。
更新:https://developer.android.com/training/load-data-background/handle-results.html