0

I am fetching data from a website using jsoup into recyclerview but I can't quite figure out exactly what I should do to refresh and update the recyclerview using the swiperefreshlayout. Can someone please help explain what code do I put in onRefresh in the code below to be able to refresh on pull down?

Content content = new Content();
    content.execute();

    swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            swipeRefreshLayout.setRefreshing(false);
            //What do I put in here??
        }
    });

    return root;
}

private class Content extends AsyncTask<Void,Void,Void> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        progressBar.setVisibility(View.VISIBLE);
        //progressBar.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_in));
    }

    @Override
    protected void onPostExecute(Void aVoid) {
        super.onPostExecute(aVoid);
        progressBar.setVisibility(View.GONE);
        //progressBar.startAnimation(AnimationUtils.loadAnimation(getActivity(), android.R.anim.fade_out));
        adapter.notifyDataSetChanged();
    }

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

        try {
            String url = "https://www.mywebsite.com";

            Document doc = Jsoup.connect(url).get();

            Elements data = doc.select(".thisurl");
            int size = data.size();
            Log.d("doc", "doc: " + doc);
            Log.d("data", "data: " + data);
            Log.d("size", "" + size);
            for (int i = 0; i < size; i++) {
                String date = doc.select(".b-time").eq(i).text();

                String league = doc.select(".card-competition-title").eq(i).text();

                String homeLogo = data.select(".card-vs-left")
                        .select("img")
                        .eq(i)
                        .attr("src");

                String homeTeam = doc.select(".card-vs-left").eq(i).text();

                String awayLogo = data.select(".card-vs-right")
                        .select("img")
                        .eq(i)
                        .attr("src");

                String awayTeam = doc.select(".card-vs-right").eq(i).text();

                String pick = doc.select("span.card-our-prono").eq(i).text();

                sportyParseItems.add(new SportyParseItem(date, league, homeLogo, homeTeam, awayLogo, awayTeam, pick));
                Log.d("items", "img: " + homeLogo + "img:" + awayLogo + " . title: " + league);
            }

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;
    }
}
crixus
  • 33
  • 4
  • [take a look here](https://stackoverflow.com/questions/26858692/swiperefreshlayout-setrefreshing-not-showing-indicator-initially) – androidLearner May 27 '21 at 22:30
  • That doesn't answer the question I asked because the concerned person in the link given only had an issue of displaying the refresh icon. My problem is refreshing the recylerview so that new data can be added / updated. – crixus May 28 '21 at 06:22

1 Answers1

0
swipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
    @Override
    public void onRefresh() {
        swipeRefreshLayout.setRefreshing(true);
         content.execute();
    }
});
androidLearner
  • 1,654
  • 1
  • 7
  • 13