Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何使用SwipeRefreshLayout在RecyclerView中更新json数据_Java_Android_Json_Android Recyclerview_Swiperefreshlayout - Fatal编程技术网

Java 如何使用SwipeRefreshLayout在RecyclerView中更新json数据

Java 如何使用SwipeRefreshLayout在RecyclerView中更新json数据,java,android,json,android-recyclerview,swiperefreshlayout,Java,Android,Json,Android Recyclerview,Swiperefreshlayout,我是android开发的新手。我正在学习。我能够使用Recyclerview在listview中获取json数据。现在我想包括swiperRefreshLayout,以便在刷新时也显示服务器中的任何更新。我在json中搜索关于swiperRefreshLayout的教程,但这不起作用。我试着使用DefaultHttpClient,但我没能理解这个想法 protected void onCreate (Bundle savedInstanceState){ super.onCrea

我是android开发的新手。我正在学习。我能够使用Recyclerview在listview中获取json数据。现在我想包括swiperRefreshLayout,以便在刷新时也显示服务器中的任何更新。我在json中搜索关于swiperRefreshLayout的教程,但这不起作用。我试着使用DefaultHttpClient,但我没能理解这个想法

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);
    }
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_health);
mRecyclerView=findviewbyd(R.id.recycler\u视图);
LinearLayoutManager LinearLayoutManager=新的LinearLayoutManager(此);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(新的LinearLayoutManager(this));
mSwipeRefreshLayout=(SwipeRefreshLayout)findViewById(R.id.SwipeRefreshLayout);
mSwipeRefreshLayout.setOnRefreshListener(新的SwipeRefreshLayout.OnRefreshListener()){
@凌驾
公共void onRefresh(){
}
});
mExampleList=newArrayList();
mRequestQueue=Volley.newRequestQueue(this);
parseJSON();
}
私有void parseJSON(){
字符串url=”http://fitandfineindustries.com/healthapi.php";
JsonObjectRequest=新的JsonObjectRequest(request.Method.GET,url,null,
新的Response.Listener(){
@凌驾
公共void onResponse(JSONObject响应){
试一试{
JSONArray JSONArray=response.getJSONArray(“info”);
for(int i=0;i
SwipeLayout与服务器无关(网络呼叫)。当用户拉动刷新SwiperFreshLayout.OnRefreshListener的onRefresh()时,将调用。就这样,这就是SwipeRefreshLayout的全部功能

这可能对你有帮助-

    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);

在刷新调用parseJSON()时,确保在此之前需要清除数组

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

onRefresh()
为空。调用
pareJSON()
内的
onRefresh()
方法,如果
RecyclerView
适配器初始化一次,然后只更新
parseJSON()内的列表就更好了,谢谢,但它不起作用,每个刷新事件上的每个列表项都是重复的。