SwipeRefreshLayout重复项目

时间:2017-11-09 22:09:39

标签: java android swiperefreshlayout

当我使用SwipeRefreshLayout更新RecyclerView时,为什么我会在items中获得重复的RecyclerView?这是我的代码:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipe_container);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                new GetContacts().execute();
            }
        });

        videoList = new ArrayList<>();
        videoList2 = new ArrayList<>();

        mAdapter = new VideoAdapter(videoList,videoList2, this,this);


        videoRecyclerView = (RecyclerView) findViewById(R.id.recycler_view);
        videoRecyclerView.setLayoutManager(new LinearLayoutManager(this));
        videoRecyclerView.setHasFixedSize(true);

        videoRecyclerView.setAdapter(mAdapter);
        new GetContacts().execute();


    }




    /**
     * Async task class to get json by making HTTP call
     */

                // new GetContacts().execute();
    private class GetContacts extends AsyncTask<Void, Void, Void> {

        @Override
        protected void onPreExecute() {
            super.onPreExecute();
            // Showing progress dialog
            pDialog = new ProgressDialog(MainActivity.this);
            pDialog.setMessage("Caricamento...");
            pDialog.setCancelable(false);
            pDialog.show();
            //mSwipeRefreshLayout.setRefreshing(true);


        }

        @Override
        protected Void doInBackground(Void... arg0) {

            HttpHandler sh = new HttpHandler();

            // Making a request to url and getting response
            String jsonStr = sh.makeServiceCall(url);

            Log.e(TAG, "Response from url: " + jsonStr);

            if (jsonStr != null) {
                try {
                    JSONObject jsonObj = null;
                    try {
                        jsonObj = new JSONObject(jsonStr);
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }

                    // Getting JSON Array node
                    JSONArray contacts = jsonObj.getJSONObject("data").getJSONArray("output");

                    // looping through All Contacts
                    for (int i = 0; i < contacts.length(); i++) {
                        JSONObject c = contacts.getJSONObject(i);

                        Video video=new Video();
                        String titolo = c.getString("titolo");
                        String sottotitolo = c.getString("sottotitolo");
                        String data = c.getString("date");
                        video.setTitolo(titolo);
                        video.setSottotitolo(sottotitolo);
                        video.setData(data);



                        videoList.add(video);
                    }


                    JSONArray img_url = jsonObj.getJSONObject("data").getJSONArray("output2");

                    // looping through All Contacts
/*
                    for(int i=0;i<=img_url.length();i++)
*/
                    for(int i=0;i<img_url.length();i++)

                    {

                        String c = img_url.getString(i);

                        // Log.i("Value is:::",""+c);
                        System.out.println("C:"+c);

                        Video video=new Video();
                        video.setPic(c);
                        videoList2.add(video);

                    }


                } catch (final JSONException e) {
                    Log.e(TAG, "Json parsing error: " + e.getMessage());
                    runOnUiThread(new Runnable() {
                        @Override
                        public void run() {
                            Toast.makeText(getApplicationContext(),
                                    "Json parsing error: " + e.getMessage(),
                                    Toast.LENGTH_LONG)
                                    .show();

                        }
                    });

                }
            }    else {
                Log.e(TAG, "Couldn't get json from server.");
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(),
                                "Couldn't get json from server. Check LogCat for possible errors!",
                                Toast.LENGTH_LONG)
                                .show();

                    }
                });

            }

            return null;
        }

        @Override
        protected void onPostExecute(Void result) {
            super.onPostExecute(result);
            mAdapter.notifyDataSetChanged();

            // Dismiss the progress dialog
           if (pDialog.isShowing())
                pDialog.dismiss();
            mSwipeRefreshLayout.setRefreshing(false);

            /**
             * Updating parsed JSON data into ListView
             * */
          /*  ListAdapter adapter = new SimpleAdapter(
                    MainActivity.this, contactList,
                    R.layout.list_item, new String[]{"name", "email",
                    "mobile"}, new int[]{R.id.name,
                    R.id.email, R.id.mobile});

            lv.setAdapter(adapter);*/
            //mSwipeRefreshLayout.setRefreshing(false);

        }

    }

有人可以帮助我吗?

0 个答案:

没有答案