无法在RecyclerView上获取JSON响应

时间:2016-12-16 19:02:28

标签: android json android-recyclerview github-api

我想将JSON响应放到RecyclerView上。虽然我通过我使用的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;
            }
        }

3 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

您在创建适配器时传递adapter = new RepoAdapter(repoList);

setAdapter

然后在调用RepoList repoList = new RepoList();

之前创建/填充新列表

recyclerView.setAdapter(adapter);

...

let model = { clientConfig: { x: 123 } }; res.render('index.html', model);

答案 2 :(得分:0)

删除此行:

RepoList repoList = new RepoList();

将此行放入onCreate

recyclerView.setAdapter(adapter);

在获取数据电话后:

adapter.notifyDataSetChange()