ListView not updating after web service Sync

2019-09-06 15:20发布

问题:

I have impelmented SyncAdapter that calls webservice and insert/update database. I would like to show some of this data in ListView so i implemented ListFragment in witch the data is displayed via CursorAdapter. And everything works ok, except when I get new data from web and insert it into database, the CursorAdapter is not updated and new data is not shown in ListView

In my ContentProvider, after every insert I call context.getContentResolver().notifyChange(uri, null);

This is ListFragment part for implementing adapter

mAdapter = new ObavijestiAdapter(getActivity(), null, 0);
setListAdapter(mAdapter);

and this is my custom 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;
}

Can anybody help me?

回答1:

I finally got this to work. So for anybody trying to do similar thing, you must include cursor.setNotificationUri(getContext().getContentResolver(), uri); after your .query in the ContentProvider. And that is it...

But if you are like me and are receiving data from a web service, remember NOT to put .notifyDataSetChanged() on ContentProvider .update/.insert/.delete but on a adapter that is calling this methods in SyncAdapter. In my example

mContentResolver.applyBatch(ScheduleContract.CONTENT_AUTHORITY, batch);
mContentResolver.notifyChange(DataContract.Obavijesti.CONTENT_URI, null);

I hope, for somebody, this will be helpful



回答2:

Try invalidating the view you're using after updating the information.

listview.invalidate();