【问题标题】:Android RecyclerView not updating itemsAndroid RecyclerView 不更新项目
【发布时间】:2016-03-09 06:02:53
【问题描述】:

我的 tabview 片段 Activity 上有一个 RecyclerView。我正在使用 arrayList 来填充 recyclerview 上的项目。这些 JSON 数据是通过 volley 库从这个 URL 获取的。

这是我的代码

            public class EditorsChoiceFragment extends Fragment {
            private RecyclerView mRecyclerView;
            private RecyclerView.Adapter mAdapter;
            private RecyclerView.LayoutManager mLayoutManager;
            private static String LOG_TAG = "EditorsChoiceFragment";
            private String urlJsonArry = "http://checkthisphone.com/black.apps/get_apps/editor.php";
            private ProgressDialog pDialog;
            private String jsonResponse;
            private static String TAG = EditorsChoiceFragment.class.getSimpleName();

            private String appTitle;
            private String appDescription;

            private JSONArray appDetails=null;
            ArrayList<DataObject> results = new ArrayList<DataObject>();

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {


                View view = inflater.inflate(R.layout.editor_choice_layout, container, false);
                makeJsonArrayRequest();
                pDialog = new ProgressDialog(getContext());
                pDialog.setMessage("Please wait...");
                pDialog.setCancelable(false);
                mRecyclerView = (RecyclerView)view.findViewById(R.id.my_recycler_view);
                mRecyclerView.setHasFixedSize(true);
                mLayoutManager = new LinearLayoutManager(getContext());
                mRecyclerView.setLayoutManager(mLayoutManager);

              //  mAdapter.notifyDataSetChanged();
                makeJsonArrayRequest();
                mAdapter = new MyRecyclerViewAdapter(results);
                mAdapter.notifyDataSetChanged();
                mRecyclerView.setAdapter(mAdapter);
               mAdapter.notifyDataSetChanged();

                return view;

            }

            @Override
            public void onResume() {
                super.onResume();
              /*((MyRecyclerViewAdapter) mAdapter).setOnItemClickListener(new MyRecyclerViewAdapter
                        .MyClickListener() {
                    @Override
                    public void onItemClick(int position, View v) {
                        Log.i(LOG_TAG, " Clicked on Item " + position);
                    }
                });*/
            }



            private void makeJsonArrayRequest() {

        //       showpDialog();
                JsonArrayRequest req = new JsonArrayRequest(urlJsonArry,
                        new Response.Listener<JSONArray>() {
                            @Override
                            public void onResponse(JSONArray response) {
                                Log.d(TAG, response.toString());


                                try {
                                    for (int i = 0; i < response.length(); i++) {

                                        JSONObject person = (JSONObject) response.get(i);

                                        String title = person.getString("title");
                                        String description = person.getString("description");
                                        DataObject obj = new DataObject(title, description);
                                        results.add(i, obj);

                                    }


                                } catch (JSONException e) {
                                    e.printStackTrace();
                                    Toast.makeText(getContext(),
                                            "Error: " + e.getMessage(),
                                            Toast.LENGTH_LONG).show();
                                    Log.d(TAG, e.getMessage());
                                }

                                hidepDialog();
                            }


                        }, new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        Toast.makeText(getContext(),
                                error.getMessage(), Toast.LENGTH_SHORT).show();
                        hidepDialog();
                    }
                });



                // Adding request to request queue
                AppController.getInstance().addToRequestQueue(req);


            }

            private void showpDialog() {
                if (!pDialog.isShowing())
                    pDialog.show();
            }

            private void hidepDialog() {
                if (pDialog.isShowing())
                    pDialog.dismiss();
            }




        }

日志文件显示数据正确,但 recyclerview 没有更新。我使用了.notifyDataSetChanged();,但它仍然显示为空白。请我找出错误..

【问题讨论】:

    标签: android android-recyclerview android-volley


    【解决方案1】:

    我没有逐行查看您的代码,但我可以建议将这些代码重构为像这里这样的方法

    public void setAdapter(ArrayList<DataObject> results){
        mAdapter = new MyRecyclerViewAdapter(results);
        mRecyclerView.setAdapter(mAdapter);
        mAdapter.notifyDataSetChanged();
    }
    

    onCreateView 中调用它,在检索到结果后再次调用它,这意味着在onResponse 方法中,就像这里

      try {
            for (int i = 0; i < response.length(); i++) {
                JSONObject person = (JSONObject) response.get(i);
                String title = person.getString("title");
                String description = person.getString("description");
                DataObject obj = new DataObject(title, description);
                results.add(i, obj);
            }
          }
          catch (JSONException e) {
            e.printStackTrace();
            Toast.makeText(getContext(),
                    "Error: " + e.getMessage(),
                    Toast.LENGTH_LONG).show();
            Log.d(TAG, e.getMessage());
        }
      //Call method here
         setAdapter(results)
    

    【讨论】:

    • 它的工作原理!!谢谢。我认为在 onCreate 上设置适配器是问题所在。再次感谢。
    • @san88 我的荣幸!
    【解决方案2】:

    请从以下位置删除 notifyDataSetChanged。

                makeJsonArrayRequest();
                mAdapter = new MyRecyclerViewAdapter(results);
                mAdapter.notifyDataSetChanged();
                mRecyclerView.setAdapter(mAdapter);
               mAdapter.notifyDataSetChanged();
    

    当您更改适配器中的数据并希望适配器知道数据已更改时,应调用notifyDataSetChanged。因此,在这种情况下,您将把它称为一些添加到结果数组列表中的数据。

    因此,一旦您在 makeJsonArrayRequest() 调用中将数据添加到结果中

              mAdapter.notifyDataSetChanged(); 
    

    在 for 循环之后。

    此外,在您的适配器构造函数中,请确保您没有创建新的数组列表并且正在重用结果,如下所示。

          ArrayList<DataObject> results;
    
          MyRecyclerViewAdapter(results){
                 this.results = results;
          }
    

    让我知道这是否有效。如果没有,请发布您的适配器代码,以便我进一步调试。

    【讨论】:

      【解决方案3】:

      您应该在 volley 的 onRequestFinished 方法中通知 DatasetChanged。

      在 MainActivity.java 中

       RequestListener listener;
       listener = new RequestListener();
       volley_queue.addRequestFinishedListener(listener);
      

      RequestListner 类

       class RequestListener implements RequestQueue.RequestFinishedListener     {
          @Override
          public void onRequestFinished(Request request) {
               mAdapter.notifyDataSetChanged();
         }
      }
      

      【讨论】:

        【解决方案4】:
        try {
             for (int i = 0; i < response.length(); i++) {        
                JSONObject person = (JSONObject) response.get(i);
                String title = person.getString("title");
                String description = person.getString("description");
                DataObject obj = new DataObject(title, description);
                results.add(i, obj);
                mAdapter.notifyItemInserted(i);
            }
        

        【讨论】:

          【解决方案5】:
          try 
          {
              for (int i = 0; i < response.length(); i++) 
              {    
                   JSONObject person = (JSONObject) response.get(i);    
                   String title = person.getString("title");
                   String description = person.getString("description");
                   DataObject obj = new DataObject(title, description);
                   results.add(i, obj);    
              }
          } 
          catch (JSONException e) 
          {
              e.printStackTrace();
              Toast.makeText(getContext(),"Error: " + e.getMessage(),Toast.LENGTH_LONG).show();
              Log.d(TAG, e.getMessage());
          }
          mAdapter.notifyDataSetChanged();
          hidepDialog();
          

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 2020-06-04
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多