Android ListFragment,自定义CursorAdapter不显示数据

时间:2015-02-26 17:49:12

标签: android android-listview android-listfragment android-cursoradapter

所以基本上我有像这样的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);
}

另外,我实施了onActivityCreatedonLoadFinished这样的

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中没有显示这些行。有人可以帮我吗?我的自定义适配器中bindViewnewView也延伸到CursorAdapter。下面是这个适配器。

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之后一切都还可以......但现在我还有另外一个问题。我期望首先加载主要活动(包括支持ActionBar),然后加载进度条以显示ListView中的数据何时加载。这就是我首先实现CursorLoader的原因。我试图将Thread.sleep(9000);放在onCreateLoader进行测试,但整个活动屏幕同时出现,因此整个用户界面停止了9秒。我做错了什么?

1 个答案:

答案 0 :(得分:0)

在回答你的第二个问题时,不要在onCreateLoader中睡觉,这应该是快速的部分。在onLoadFinished中休眠以表示需要一段时间来加载数据。