【问题标题】:ListView not updating after web service SyncWeb 服务同步后 ListView 未更新
【发布时间】:2015-04-30 18:32:54
【问题描述】:

我已经实现了SyncAdapter,它调用了 web 服务和插入/更新数据库。我想在ListView 中显示其中的一些数据,所以我实现了ListFragment,数据通过CursorAdapter 显示。一切正常,除了当我从网络获取新数据并将其插入数据库时​​,CursorAdapter 没有更新,ListView 中也没有显示新数据

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

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

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

谁能帮帮我?

【问题讨论】:

    标签: android android-listview android-listfragment android-syncadapter android-webservice


    【解决方案1】:

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

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

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

    希望对大家有所帮助

    【讨论】:

      【解决方案2】:

      在更新信息后尝试使您正在使用的视图无效。

      listview.invalidate();
      

      【讨论】:

      • 好的...但是,我的 SyncAdapter 正在后台同步,我不知道我需要把这个 listview.invalidate(); 放在哪里。因为,SyncAdapter 不知道我现在在做什么活动(它是否有一个列表视图,如果我的 ListFragment 被打开,我不知道 SyncAdapter 中的数据发生了变化......这让我感到困惑...... .
      猜你喜欢
      • 1970-01-01
      • 2012-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-25
      • 1970-01-01
      相关资源
      最近更新 更多