使用gson,volley获取和发布项目并使用recyclerview显示它后,我将从recyclerviewadapter中删除项目。项目在网址上被删除,但它没有更新recyclerview。删除的项目仍然存在。首先,我必须再次返回重新启动应用程序,它正在更新UI。我希望它能够同时更新。
public class RecyclerViewAdapter extends android.support.v7.widget.RecyclerView.Adapter<RecyclerViewHolders> {
private List<FriendsData> friendList;
private Context context;
//making class recyclerviewadapter for fetching friendlist
public RecyclerViewAdapter(Context context, List<FriendsData> friendList) {
this.friendList = friendList;
this.context = context;
}
@Override
//view holder is holding the data
public RecyclerViewHolders onCreateViewHolder(ViewGroup parent, int viewType) {
View layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_friend, null);
RecyclerViewHolders recyclerViewHolders = new RecyclerViewHolders(layoutView);
return recyclerViewHolders;
}
//method for deleting friend
public void delete(int position) { //removes the row
friendList.remove(position);
notifyItemRemoved(position);
notifyItemRangeChanged(position,friendList.size());
//notifyDataSetChanged();
}
@Override
//onBindViewHolder is binding the data to recyclerviewholder
public void onBindViewHolder(RecyclerViewHolders holder, final int position) {
//current = friendList.get(position);
//setting id,firstname,lastname,contact to holder
holder.id.setText(String.valueOf("Id: " + friendList.get(position).getId()));
holder.firstName.setText("First Name: " + friendList.get(position).getFirstname());
holder.lastName.setText("Last Name: " + friendList.get(position).getLastname());
holder.contact.setText("Contact: " + friendList.get(position).getContact());
//delete button is clicked
holder.del.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
//making requestqueue
RequestQueue queue = Volley.newRequestQueue(context);
String url="http://192.168.1.50:8080/*********/"+friendList.get(position).getId();
//making json object request
JsonObjectRequest jsonObjReq= new JsonObjectRequest(Request.Method.DELETE,url,null, new Response.Listener<JSONObject>() {
@Override
//getting response
public void onResponse(JSONObject response){
Log.d("Tag", response.toString());
//deleting that particular friend data
delete(position);
//adapter(position);
//notifyDataSetChanged();
}
}
,new Response.ErrorListener() {
@Override
//getting error response
public void onErrorResponse(VolleyError error) {
//getting error message
VolleyLog.d("TAG", "Error:" + error.getMessage());
}
});
//adding request to queue
queue.add(jsonObjReq);
}
});
};
@Override
//counting total friends for displaying
public int getItemCount () {
//returning size of list
return friendList.size();
}
}
答案 0 :(得分:0)
删除一个项目后调用您的列表网址功能。它会自动重新绑定RecyclerView的适配器。