0
使用gson,凌空和使用recyclerview顯示它後獲取和發佈項目後,我從recyclerviewadapter中刪除項目。項目在網址上被刪除,但不會更新recyclerview。刪除的項目仍然存在。首先,我必須再次回到重新啓動應用程序時更新UI。我希望它在同一時間更新。從url中刪除項目但不更新recyclerview
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();
}
}