Android RecyclerView不更新项目

时间:2016-03-09 06:02:54

标签: android android-recyclerview android-volley

我的tabview片段活动中有一个RecyclerView。我使用arrayList来填充recyclerview上的项目。这些JSON数据来自此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();,但仍然显示为空白。请我找到错误..

5 个答案:

答案 0 :(得分:2)

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

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)

答案 1 :(得分:0)

请从以下地点删除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;
      }

如果有效,请告诉我。如果没有,请发布您的适配器代码,以便我可以进一步调试。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

您应该在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 :(得分:0)

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