简体   繁体   中英

How to use SwipeRefreshLayout to update json data in RecyclerView

I am new in android development. I am learning it. I am able to fetch json data in listview using Recyclerview. Now I want to include swiperRefreshLayout, so any update in server also displayed while refreshing. I search tutorial about swiperRefreshLayout in json, but this doesn't work. I tried using DefaultHttpClient, but I can't catch this idea.

protected void onCreate (Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_health);
        mRecyclerView = findViewById(R.id.recycler_view);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
        mRecyclerView.setHasFixedSize(true);
        mRecyclerView.setLayoutManager(new LinearLayoutManager(this));


        mSwipeRefreshLayout = (SwipeRefreshLayout) findViewById(R.id.swipeRefreshLayout);
        mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {

            }
        });
        mExampleList = new ArrayList<>();

        mRequestQueue = Volley.newRequestQueue(this);
        parseJSON();
    }

    private void parseJSON() {
        String url = "http://fitandfineindustries.com/healthapi.php";

        JsonObjectRequest request = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                    public void onResponse(JSONObject response) {
                        try {
                            JSONArray jsonArray = response.getJSONArray("info");

                            for (int i = 0; i < jsonArray.length(); i++) {
                                JSONObject hit = jsonArray.getJSONObject(i);

                                String creatorName = hit.getString("heading");
                                String img = hit.getString("img");
                                String imageUrl = img.length() == 0 ? "file:///android_asset/fitandfineindustries.jpg" : "http://fitandfineindustries.com/images/plan/"+img;

                                mExampleList.add(new NewsItem(imageUrl, creatorName));
                            }

                            mNewsAdapter = new NewsAdapter(HealthActivity.this, mExampleList);
                            mRecyclerView.setAdapter(mNewsAdapter);
                            mNewsAdapter.setOnItemClickListener(HealthActivity.this);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.printStackTrace();
            }
        });

        mRequestQueue.add(request);
    }

On refresh call your parseJSON() make sure before this you need to clear your array.

SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
         mExampleList.clear()
         parseJSON();
        }
    });
    mExampleList = new ArrayList<>();
    mRequestQueue = Volley.newRequestQueue(this);
    parseJSON();
}

SwipeLayout has nothing to do with the server(Network calls). When the user pulls to refresh the onRefresh() of your SwipeRefreshLayout.OnRefreshListener is called. That's it, that is all the SwipeRefreshLayout does.

This might help you-

    mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
        @Override
        public void onRefresh() {
            //call the parseJSON() function here like this.
            parseJSON();
            // call the setRefreshing(true) function to show
            // the circular rotating refresh icon
            mSwipeRefreshLayout.setRefreshing(true);
        }
    });

    //Don't forget to hide the refreshing icon when your server call ends like this.
    //mSwipeRefreshLayout.setRefreshing(false);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM