Android 带有自定义 CursorAdapter 的 ListFragment 不显示数据
Android ListFragment with custom CursorAdapter not showing data
基本上我有一个像这样的 ListFragment 设置
public class ObavijestiFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
private View mApiError;
private ObavijestiAdapter mAdapter;
private static String TAG="ObavijestiFragment";
public static ObavijestiFragment newInstance() {
return new ObavijestiFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_obavijesti, container, false);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
mApiError = view.findViewById(R.id.api_error);
}
此外,我实现了 onActivityCreated
和 onLoadFinished
这样的
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Activity activity = getActivity();
if (!activity.isFinishing()) {
Loader loader = getActivity().getSupportLoaderManager().getLoader(Config.OBAVIJESTI_CURSOR_ID);
mAdapter = new ObavijestiAdapter(activity, null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(mAdapter);
if (loader != null && !loader.isReset()) {
getActivity().getSupportLoaderManager().restartLoader(Config.OBAVIJESTI_CURSOR_ID, null, this);
} else {
getActivity().getSupportLoaderManager().initLoader(Config.OBAVIJESTI_CURSOR_ID, null, this);
}
}
}
public void onLoadFinished(Loader<Cursor> loader,Cursor cursor) {
if(mAdapter!=null && cursor!=null)
mAdapter.swapCursor(cursor);
}
我在 onLoadFinished 中的光标有 4 行数据,但我的 ListView 中没有显示这些行。有人可以帮我解决这个问题吗??我的自定义适配器中扩展 CursorAdapter
的 bindView
或 newView
也更新了。下面是这个适配器。
public class ObavijestiAdapter extends CursorAdapter {
private Cursor mCursor;
private Context mContext;
private final LayoutInflater mInflater;
static class ViewHolder {
public TextView hTextNaslov;
public TextView hTextOpis;
public ImageView hImage;
}
public ObavijestiAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
mInflater = LayoutInflater.from(context);
mContext = context;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final ViewHolder holder = (ViewHolder)view.getTag();
//TODO: REMOVE AFTER IMPLEMENTING REAL IMAGE
int w = 24, h = 24;
Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
//END REMOVE
holder.hImage.setImageBitmap(bmp);
holder.hTextNaslov.setText(cursor.getString(cursor.getColumnIndex(DataContract.Obavijesti.OBAVIJESTI_NASLOV)));
holder.hTextOpis.setText(cursor.getString(cursor.getColumnIndex(DataContract.Obavijesti.OBAVIJESTI_OPIS)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view=mInflater.inflate(R.layout.fragment_obavijesti_row,parent,false);
final ViewHolder holder = new ViewHolder();
holder.hTextOpis = (TextView) view.findViewById(R.id.obOpis);
holder.hTextNaslov = (TextView) view.findViewById(R.id.obNaslov);
holder.hImage = (ImageView) view.findViewById(R.id.oblist_image);
view.setTag(holder);
return view;
}
更新:
我尝试使用 SimpleCursorAdapter 和一个像这样的 TextView:
String[] from = new String[] {DataContract.Obavijesti.OBAVIJESTI_NASLOV};
int[] to = new int[] { R.id.obNaslov };
sAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.fragment_obavijesti_row, null, from,to , 0);
setListAdapter(sAdapter);
getActivity().getSupportLoaderManager().initLoader(0, null, this);
但运气不好。再次输入,onLoadFinished
cursor.getCount()=4 这是正确的数字
更新 2:
因此,在将 ListView lv = (ListView) getActivity().findViewById(R.id.obList);
和 lv.setAdapter(mAdapter);
替换为 setListAdapter
之后,一切正常......但现在我遇到了另一个问题。我期待 main activity(包括支持 ActionBar)首先加载,然后加载进度条以显示 ListView 中的数据何时加载。这就是我首先实现 CursorLoader 的原因。我试图将 Thread.sleep(9000);
放在 onCreateLoader
中进行测试,但是整个 activity 屏幕同时加载,所以整个 UI 停止了 9 秒。我做错了什么??
在回答你的第二个问题时不要睡在onCreateLoader
,这应该是快速的部分。休眠 onLoadFinished
表示加载数据需要一段时间。
基本上我有一个像这样的 ListFragment 设置
public class ObavijestiFragment extends ListFragment implements LoaderManager.LoaderCallbacks<Cursor> {
private View mApiError;
private ObavijestiAdapter mAdapter;
private static String TAG="ObavijestiFragment";
public static ObavijestiFragment newInstance() {
return new ObavijestiFragment();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_obavijesti, container, false);
return v;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
mApiError = view.findViewById(R.id.api_error);
}
此外,我实现了 onActivityCreated
和 onLoadFinished
这样的
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Activity activity = getActivity();
if (!activity.isFinishing()) {
Loader loader = getActivity().getSupportLoaderManager().getLoader(Config.OBAVIJESTI_CURSOR_ID);
mAdapter = new ObavijestiAdapter(activity, null, CursorAdapter.FLAG_REGISTER_CONTENT_OBSERVER);
setListAdapter(mAdapter);
if (loader != null && !loader.isReset()) {
getActivity().getSupportLoaderManager().restartLoader(Config.OBAVIJESTI_CURSOR_ID, null, this);
} else {
getActivity().getSupportLoaderManager().initLoader(Config.OBAVIJESTI_CURSOR_ID, null, this);
}
}
}
public void onLoadFinished(Loader<Cursor> loader,Cursor cursor) {
if(mAdapter!=null && cursor!=null)
mAdapter.swapCursor(cursor);
}
我在 onLoadFinished 中的光标有 4 行数据,但我的 ListView 中没有显示这些行。有人可以帮我解决这个问题吗??我的自定义适配器中扩展 CursorAdapter
的 bindView
或 newView
也更新了。下面是这个适配器。
public class ObavijestiAdapter extends CursorAdapter {
private Cursor mCursor;
private Context mContext;
private final LayoutInflater mInflater;
static class ViewHolder {
public TextView hTextNaslov;
public TextView hTextOpis;
public ImageView hImage;
}
public ObavijestiAdapter(Context context, Cursor c, int flags) {
super(context, c, flags);
mInflater = LayoutInflater.from(context);
mContext = context;
}
@Override
public void bindView(View view, Context context, Cursor cursor) {
final ViewHolder holder = (ViewHolder)view.getTag();
//TODO: REMOVE AFTER IMPLEMENTING REAL IMAGE
int w = 24, h = 24;
Bitmap.Config conf = Bitmap.Config.ARGB_8888; // see other conf types
Bitmap bmp = Bitmap.createBitmap(w, h, conf); // this creates a MUTABLE bitmap
//END REMOVE
holder.hImage.setImageBitmap(bmp);
holder.hTextNaslov.setText(cursor.getString(cursor.getColumnIndex(DataContract.Obavijesti.OBAVIJESTI_NASLOV)));
holder.hTextOpis.setText(cursor.getString(cursor.getColumnIndex(DataContract.Obavijesti.OBAVIJESTI_OPIS)));
}
@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
View view=mInflater.inflate(R.layout.fragment_obavijesti_row,parent,false);
final ViewHolder holder = new ViewHolder();
holder.hTextOpis = (TextView) view.findViewById(R.id.obOpis);
holder.hTextNaslov = (TextView) view.findViewById(R.id.obNaslov);
holder.hImage = (ImageView) view.findViewById(R.id.oblist_image);
view.setTag(holder);
return view;
}
更新: 我尝试使用 SimpleCursorAdapter 和一个像这样的 TextView:
String[] from = new String[] {DataContract.Obavijesti.OBAVIJESTI_NASLOV};
int[] to = new int[] { R.id.obNaslov };
sAdapter = new SimpleCursorAdapter(getActivity(),
R.layout.fragment_obavijesti_row, null, from,to , 0);
setListAdapter(sAdapter);
getActivity().getSupportLoaderManager().initLoader(0, null, this);
但运气不好。再次输入,onLoadFinished
cursor.getCount()=4 这是正确的数字
更新 2:
因此,在将 ListView lv = (ListView) getActivity().findViewById(R.id.obList);
和 lv.setAdapter(mAdapter);
替换为 setListAdapter
之后,一切正常......但现在我遇到了另一个问题。我期待 main activity(包括支持 ActionBar)首先加载,然后加载进度条以显示 ListView 中的数据何时加载。这就是我首先实现 CursorLoader 的原因。我试图将 Thread.sleep(9000);
放在 onCreateLoader
中进行测试,但是整个 activity 屏幕同时加载,所以整个 UI 停止了 9 秒。我做错了什么??
在回答你的第二个问题时不要睡在onCreateLoader
,这应该是快速的部分。休眠 onLoadFinished
表示加载数据需要一段时间。