Recycler视图刷新无法正常工作

时间:2017-11-30 11:31:05

标签: android android-recyclerview

我通过volley字符串请求从mysqli数据库中检索数据到android recycleler视图。我希望在添加项目时刷新回收器视图,而无需进行手动刷新。我将notifyDataSetChanged添加到create方法的主要活动上,但是当添加了一个项目时,Recycler视图不会刷新。以下是我的代码:

    public class MainActivity extends AppCompactActivity {
        RecyclerView recyclerView;
        RecyclerLayout adapter;
        List <Recycler> arrayList;
        private static final String url = "http://example.com";

    @Override 
     //overriding the onCreate method from its super class
    {
        recyclerView = findViewById (R.id.recycler);
        arrayList = new ArrayList <>();
        retrieveData ();

        recyclerView.setHasFixedSize (true);
        recyclerView.setLayoutManager (new LinearLayoutManager (this));
        adapter = new adapter(getApplicationContext());
        recyclerView.setAdapter (adapter);
        adapter.notifyDataSetChanged ();
        }

        private void retrieveData (){
            StringRequest stringRequest = new StingRequest(Request.Method.POST, url, 
            new Response.Listener <String>();
            @Override 
            public void onResponseListener (response){

            //surround jsonArray with try and catch
                JsonArray array = new JsonArray  (response);

                for (int i = o;  i  < array.length (); i++){
                    JsonObject object = array.getJsonObject(i) ;
                     arrayList.add (new Recycler (
                      object.getString ("text")))
                }
            },
            new Response.ErrorListener <>()
            /**overriding the error listener method**/);
            //adding the volley request on a queue 
        }
    }


    //Recycler adapter class

    public class RecyclerLayout extends RecyclerView.adapter <RecyclerLayout.RecyclerHolder>{
            Context ctx;
            List <Recycler> arrayList;
        RecyclerLayout(Context ctx, List <Recycler> arrayList){
                this.ctx=ctx;
                this.arrayList = arrayList;
            }
            @Override 
            //creating the view

            @Override 
             //implementing the onBind method

            @Override 
             //implementing the getItemCount method
        RecyclerHolder extends RecyclerView.ViewHolder {
        }
    }


    //Recycler j son store
    public class Recycler {
            private String text;
        Recycler (String text){
            this.text = text;
        }
        public String getText (){
            return text;
        }
    }

3 个答案:

答案 0 :(得分:0)

在检索项目并将其添加到数组后,应调用notifyDataSetChanged。在setAdapter之后调用notifyDataSetChanged是没有意义的,因为此时适配器不需要更新。

答案 1 :(得分:0)

更改代码
 adapter = new adapter(getApplicationContext())  
 recyclerView.setAdapter (adapter);
 adapter.notifyDataSetChanged (); 

adapter = new RecyclerLayout(getApplicationContext(),arrayList);
recyclerView.setAdapter (adapter);

答案 2 :(得分:0)

您当前的代码应该给您错误。您应该修改您的代码,如下所示。在onCreate()

 recyclerView = findViewById (R.id.recycler);
 arrayList = new ArrayList <>();
 recyclerView.setHasFixedSize (true);
 recyclerView.setLayoutManager (new LinearLayoutManager (this));
 adapter = new adapter(getApplicationContext(), arrayList);
 recyclerView.setAdapter (adapter);

retriveData()方法中:

 public void onResponseListener (response){

        //surround jsonArray with try and catch
            JsonArray array = new JsonArray  (response);

        for (int i = o;  i  < array.length (); i++){
                JsonObject object = array.getJsonObject(i) ;
                 arrayList.add (new Recycler (
                  object.getString ("text")))
        }

        adapter.notifyDataSetChanged();
 }