从回收器中删除项目数据更新后查看



我正在从适配器内的回收器视图更新数据:

private void desmarcar_anuncio_favorito(final String anuncioF, final String usuarioF, final Integer position) {
// Tag used to cancel the request
String tag_string_req = "req_login";

StringRequest strReq = new StringRequest(Request.Method.POST,
URL_MEDIA_ANUNCIO, new Response.Listener<String>() {
@Override
public void onResponse(String response) {

String ultimo = response;
String ya = "borrado";
if (ultimo.equals(ya)){
Toast.makeText(context,"Anuncio eliminado de favoritos",Toast.LENGTH_SHORT).show();
updateData(anuncios);
}
else {
Toast.makeText(context,"Error al eliminar anuncio de favoritos",Toast.LENGTH_SHORT).show();
}

}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
}) {
@Override
protected Map<String, String> getParams() {
// Posting parameters to login url
Map<String, String> params = new HashMap<String, String>();
params.put("anuncio", anuncioF);
params.put("usuario", usuarioF);


return params;
}
};
// Adding request to request queue
AppController.getInstance().addToRequestQueue(strReq, tag_string_req);
}

public void updateData(List data) {
this.anuncios = data;
notifyDataSetChanged();
}

使用此代码,更新在远程数据库中完成,但不更新回收器视图项,在这种情况下,应从回收器视图中删除该项。

我做错了什么?

updateData(anuncios);

您正在使用数据anuncios的当前值更新回收器视图,您必须从列表中删除该项目 anunciosanuncios.remove(indexOfItemRemoving)然后调用notifyDataSetChanged()notifyItemRemoved(indexOfItemRemoving)

您需要做的是更新indexOfItemRemoving

您的方法 updateData 将替换您情况下的列表,与以前相同的列表,您需要更改列表并仅通知适配器。

像这样:

if (ultimo.equals(ya)){
Toast.makeText(context,"Anuncio eliminado de favoritos",Toast.LENGTH_SHORT).show();
int indexOfItemRemoving = anuncios.size(); // get the correct item here instead
anuncios.remove(indexOfItemRemoving);
notifyItemRemoved(indexOfItemRemoving);
}

使用通知项目插入/删除/范围更改的原因有一个优势 回收商视图 使用漂亮的事件动画反馈处理这些事件

最新更新