OnRefresh期间的RecyclerView添加了另一个recyclerView List?

时间:2016-01-21 08:04:41

标签: android android-recyclerview

当我进行onRefresh时,我的recyclerView将另一个副本添加到我当前的recyclerView而不是刷新它,通过查看下面的代码,有人知道问题是什么吗?我已经尝试了所有但似乎没有任何工作?

  @Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    if (getArguments() != null) {
        mParam1 = getArguments().getString(ARG_PARAM1);
        mParam2 = getArguments().getString(ARG_PARAM2);
    }
    //send our volley JSON Request
    //Initialize VolleySingleton
    mVolleySingleton = VolleySingleton.getInstance();
    //intitalize Volley Singleton request key
    mRequestQueue = mVolleySingleton.getRequestQueue();
    //2 types of requests an Array request and an Object Request
    JSONArrayRequest();


}
private void JSONArrayRequest() {
    JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET, URL_API, (String) null, new Response.Listener<JSONArray>() {
        @Override
        public void onResponse(JSONArray response) {
            listblogs=parseJSONResponse(response);
            mAdapterDashBoard.setBloglist(listblogs);

            System.out.println("it worked!!!");
        }

    }, new Response.ErrorListener() {
        @Override
        public void onErrorResponse(VolleyError error) {
            ToastTest.t(getActivity(), error.toString());
        }
    });
    mRequestQueue.add(request);
}

private ArrayList<Blogs> parseJSONResponse(JSONArray response) {
    if (!response.equals("")) {
        ArrayList<Blogs> blogsArrayList = new ArrayList<>();
        try {
            StringBuilder data = new StringBuilder();
            for (int i = 0; i < response.length(); i++) {
                JSONObject currentQuestions = response.getJSONObject(i);
                String text = currentQuestions.getString("text");
                String points = currentQuestions.getString("points");
                String ID=currentQuestions.getString("id");
                String courseId = currentQuestions.getString("courseId");
                String studentId = currentQuestions.getString("studentId");


                data.append(text + "\n" + points + "\n" + courseId + "\n");
                System.out.println(data);

                Blogs blogs = new Blogs();
                blogs.setId(ID);
                blogs.setMstudentId(studentId);
                blogs.setMtext(text);
                blogs.setPoints(points);
                listblogs.add(blogs);
            }

        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
    return listblogs;
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment

    View view=inflater.inflate(R.layout.fragment_dashboard,container,false);
    mRecyclerView=(RecyclerView)view.findViewById(R.id.fragment_dashboard);
    mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
    mAdapterDashBoard=new AdapterDashBoard(getActivity());
    mRecyclerView.setAdapter(mAdapterDashBoard);
    mPullToRefreshView = (PullToRefreshView)view.findViewById(R.id.pull_to_refresh);
    mPullToRefreshView.setOnRefreshListener(new PullToRefreshView.OnRefreshListener() {
        @Override
        public void onRefresh() {
            mPullToRefreshView.postDelayed(new Runnable() {
                @Override
                public void run() {
                    mPullToRefreshView.setRefreshing(false);
                    JSONArrayRequest();

                }
            }, REFRESH_DELAY);
        }
    });

    return view;
}

}

public class AdapterDashBoard extends RecyclerView.Adapter<AdapterDashBoard.ViewDashboard>{
private LayoutInflater mLayoutInflater;
private ArrayList<Blogs> listblogs=new ArrayList<>();
public AdapterDashBoard(Context context){

    mLayoutInflater=LayoutInflater.from(context);

}
public void setBloglist(ArrayList<Blogs> listBlogs){
    this.listblogs=listBlogs;
    notifyItemRangeChanged(0,listBlogs.size());
}

@Override
public ViewDashboard onCreateViewHolder(ViewGroup parent, int viewType) {
   View view= mLayoutInflater.inflate(R.layout.customizejson,parent,false);
    ViewDashboard viewholder=new ViewDashboard(view);
    return viewholder;
}

@Override
public void onBindViewHolder(ViewDashboard holder, int position) {
    Blogs currentBlog=listblogs.get(position);
    holder.questionText.setText(currentBlog.getMtext().toString());
    holder.points.setText(currentBlog.getPoints().toString());
    holder.id.setText(currentBlog.getId().toString());
}

@Override
public int getItemCount() {
    return listblogs.size();
}

static class ViewDashboard extends RecyclerView.ViewHolder{
    private ImageView thumbnail;
    private TextView questionText;
    private TextView points;
    private TextView id;


    public ViewDashboard (View itemView){
        super(itemView);
        //thumbnail=(ImageView)itemView.findViewById(R.id.thumbnail);
        questionText=(TextView)itemView.findViewById(R.id.questionText);
        points=(TextView)itemView.findViewById(R.id.points);
        id=(TextView)itemView.findViewById(R.id.ID);




    }
}
}

1 个答案:

答案 0 :(得分:0)

在添加新项目之前,您需要清除listblogs列表。这就是您获得重复值的原因。

此外,您已经在listblogs方法中向parseJSONResponse()添加了新项目,因此listblogs=parseJSONResponse(response);方法中不需要onResponse(),而是同一个列表。

onResponse()方法更改为:

@Override
public void onResponse(JSONArray response) {
    listblogs.clear(); // here you clear the old data
    parseJSONResponse(response); 
    mAdapterDashBoard.setBloglist(listblogs);

    System.out.println("it worked!!!");
}