本文介绍了Web服务同步后,ListView不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已启用 SyncAdapter 来调用Web服务并插入/更新数据库。我想在 ListView 中显示一些数据,所以我在 ListFragment 中实现了通过<$显示数据c $ c> CursorAdapter 。一切正常,除非当我从Web获取新数据并将其插入数据库时​​, CursorAdapter 不会更新,并且新数据也不会显示在中ListView

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

在我的 ContentProvider 中,每次插入后我都调用 context.getContentResolver()。notifyChange(uri,null);

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

这是 ListFragment 用于实现适配器的部分

This is ListFragment part for implementing adapter

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

这是我的自定义适配器

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

有人可以帮助我吗?

推荐答案

我终于可以使用它了。因此,对于任何尝试做类似事情的人,您必须在 .query之后包含 cursor.setNotificationUri(getContext()。getContentResolver(),uri); ContentProvider 中。就是这样...

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...

但是,如果您像我一样,并且正在从Web服务接收数据,请记住不要放在 .notifyDataSetChanged( )在ContentProvider .update / .insert / .delete 上,但是在 SyncAdapter 中调用此方法的适配器上code>。在我的示例中

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

我希望对某些人有帮助

这篇关于Web服务同步后,ListView不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 18:57