[android]无法获取 JSON 响应上 RecyclerView

标签: json Android
发布时间: 2017/3/4 1:55:49
注意事项: 本文中文内容可能为机器翻译,如要查看英文原文请点击上面连接.

我想要在 RecyclerView 上的 JSON 响应。虽然我正在通过 REST API 的正确响应我所用,但解析后在 RecyclerView 上不来响应。下面是代码。请帮助!!

RepoActivity.java

        public class RepoActivity extends AppCompatActivity {

            private static final String TAG = MainActivity.class.getSimpleName();
            private ProgressDialog pDialog;
            private List<RepoList> repoList = new ArrayList<>();
            String data = "";
            private RecyclerView recyclerView;
            private static String url;
            private RepoAdapter adapter;
            RequestQueue requestQueue;


            @Override
            protected void onCreate(Bundle savedInstanceState) {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.activity_repo);

                recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
                Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
                setSupportActionBar(toolbar);

                Bundle bundle = getIntent().getExtras();
                String message = bundle.getString("message");
                adapter = new RepoAdapter(repoList);
                RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
                recyclerView.setLayoutManager(mLayoutManager);
                recyclerView.setItemAnimator(new DefaultItemAnimator());



                url = "https://api.github.com/users/" + message + "/repos";
                parsingMethod();
            }

            private void parsingMethod() {

                Log.d("hello1", url);
                pDialog = new ProgressDialog(this);
                // Showing progress dialog
                pDialog.setMessage("Loading...");
                pDialog.show();

                // Creating volley request obj
                JsonArrayRequest cityReq = new JsonArrayRequest(url,
                        new Response.Listener<JSONArray>() {

                            @Override
                            public void onResponse(JSONArray jsonArray) {
                                hidePDialog();

                                // Parsing json
                                for (int i = 0; i < jsonArray.length(); i++) {

                                    JSONObject obj = null;
                                    try {
                                        obj = jsonArray.getJSONObject(i);
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }

                                    RepoList repoList = new RepoList();
                                    try {

                                        repoList.setRepoName(obj.getString("name"));
                                        Log.d("zxcv",obj.getString("name") );
                                        repoList.setRepoDesc(obj.getString("description"));
                                        Log.d("zxcv",obj.getString("description") );
                                    } catch (JSONException e) {
                                        e.printStackTrace();
                                    }

                                }
                                recyclerView.setAdapter(adapter);

                            }

                        },new  Response.ErrorListener()

                {
                    @Override
                    public void onErrorResponse (VolleyError error){
                        VolleyLog.d(TAG, "Error: " + error.getMessage());
                        hidePDialog();

                    }

                });


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

            @Override
            public void onDestroy() {
                super.onDestroy();
                hidePDialog();
            }

            private void hidePDialog() {
                if (pDialog != null) {
                    pDialog.dismiss();
                    pDialog = null;
                }
            }
        }

RepoAdapter.java

        public class RepoAdapter extends RecyclerView.Adapter<RepoAdapter.MyViewHolder> {

            private List<RepoList> repoLists;


            @Override
            public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
                View itemView = LayoutInflater.from(parent.getContext())
                        .inflate(R.layout.repo_list_row, parent, false);

                return new MyViewHolder(itemView);
            }

            @Override
            public void onBindViewHolder(MyViewHolder holder, int position) {
                RepoList repoList = repoLists.get(position);
                holder.repoName.setText(repoList.getRepoName());
                holder.repoDesc.setText(repoList.getRepoDesc());
            }

            @Override
            public int getItemCount() {
                return repoLists.size();
            }

            public class MyViewHolder extends RecyclerView.ViewHolder {
                public TextView repoName, repoDesc;

                public MyViewHolder(View view) {
                    super(view);
                    repoName = (TextView) view.findViewById(R.id.repoName);
                    repoDesc = (TextView) view.findViewById(R.id.repoDesc);
                }
            }

            public RepoAdapter( List<RepoList> repoList) {
                this.repoLists = repoList;
            }

        }

RepoList.java

            package com.example.lenovo.boxme;

        /**
         * Created by lenovo on 16-12-2016.
         */

        public class RepoList {

            private String repoName,repoDesc;

            public RepoList(String repoDesc, String repoName) {
                this.repoDesc = repoDesc;
                this.repoName = repoName;
            }

            public RepoList(){};

            public String getRepoDesc() {
                return repoDesc;
            }

            public void setRepoDesc(String repoDesc) {
                this.repoDesc = repoDesc;
            }

            public String getRepoName() {
                return repoName;
            }

            public void setRepoName(String repoName) {
                this.repoName = repoName;
            }
        }

解决方法 1:

  adapter = new RepoAdapter(repoList);
  RecyclerView.LayoutManager mLayoutManager = new LinearLayoutManager(getApplicationContext());
  recyclerView.setLayoutManager(mLayoutManager);
  recyclerView.setItemAnimator(new DefaultItemAnimator());
  recyclerView.setAdapter(adapter);

通知列表项更改后的数据集

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

                                JSONObject obj = null;
                                try {
                                    obj = jsonArray.getJSONObject(i);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                                RepoList repo= new RepoList();
                                try {

                                    repo.setRepoName(obj.getString("name"));
                                    Log.d("zxcv",obj.getString("name") );
                                    repo.setRepoDesc(obj.getString("description"));
                                    Log.d("zxcv",obj.getString("description") );
                                    repoList.add(repo);//you missed
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }

                            }
            adapter.notifyDataSetChange();
官方微信
官方QQ群
31647020