Search code examples
androidandroid-listviewandroid-listfragmentandroid-cursoradapter

Android ListFragment with custom CursorAdapter not showing data


So basically I have a ListFragment setup like this

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);
}

Also, I implemented onActivityCreated and onLoadFinished like this

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);
}

My cursor in onLoadFinished has 4 rows of data but non of this rows are shown in my ListView. Can somebody help me with this?? Also bindView or newView in my custom adapter that extends CursorAdapter are newer reached. Below is this Adapter.

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;
}

UPDATE: I tryed with SimpleCursorAdapter and just one TextView like this:

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);

but no luck. Again in, onLoadFinished the cursor.getCount()=4 and this is the right number

UPDATE 2: So, after putting ListView lv = (ListView) getActivity().findViewById(R.id.obList); and lv.setAdapter(mAdapter); instead of setListAdapter everything is ok... But now I have another problem. I am expecting main activity (that includes support ActionBar) to load first and after that a progress bar to show when data in the ListView is loading. This is why I implemented CursorLoader in the first place. I tried to put Thread.sleep(9000); in onCreateLoader for testing, but whole activity screen loades at the same time, so the whole UI is stopped for 9 seconds. What am I doing wrong??


Solution

  • In answer to your second question don't sleep in onCreateLoader, that's supposed to be the fast part. Sleep in onLoadFinished to represent it taking a while to load the data.