简体   繁体   中英

Can someone tell me why my listView isn't populating with the JSON data

I have figured out that my main issue right now is my intent code. I have no idea how to build my intent to make the data come back from the website so if anyone could help me with that, it would be awesome! I am trying to get JSON data from the openweather api and my app will load up the web page instead of bringing it into the list view. Right now with the intent I have, a web page opens up with the data I need. Any help would be great!

      public class MainActivity extends AppCompatActivity {

      private String url = "http://api.openweathermap.org/data/2.5/weather?q=London,us&APPID=805bd9f830268d5274c60e41613c28a5";
      private List<String> items = new ArrayList<String>();
      private ArrayAdapter<String> mArrayAdapter;

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

        final Button btnRequest = (Button) findViewById(R.id.button); //your button id must be button
        final ListView lv = (ListView) findViewById(R.id.listView); //your listview ide must be listview


        final RequestQueue requestQueue = Volley.newRequestQueue(this);




        final JsonObjectRequest jor = new JsonObjectRequest(Request.Method.GET, url, null,
                new Response.Listener<JSONObject>() {
                    @Override
                 public void onResponse(JSONObject response) {

                    try {

                        JSONObject jcity = response.getJSONObject("city");

                        String name = jcity.getString("name");
                        JSONArray wlist = response.getJSONArray("list");


                          for (int i = 0; i < 10; i++) {

                            JSONObject jsonObject = wlist.getJSONObject(i);


                            String dt = jsonObject.getString("dt");
                            JSONArray warray = jsonObject.getJSONArray("weather");

                                JSONObject wobj = warray.getJSONObject(0);
                                String desc = wobj.getString("description");


                                items.add(dt + " " + desc);



                                Log.d("date", dt);  //Log.d prints to the terminal
                                Log.d("desc", desc);//good for debugging


                            }

                            mArrayAdapter = new ArrayAdapter<String>
                                    (getApplicationContext(),
                                    android.R.layout.simple_list_item_1, items);

                            //lv.setAdapter(mArrayAdapter);

                        } catch (JSONException e) {
                            e.printStackTrace();
                        }
                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.e("Volley", "Error");

                    }
                }
        );


        btnRequest.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                requestQueue.add(jor);
                Intent i = new Intent(Intent.ACTION_VIEW);
                i.setData(Uri.parse(url));
                startActivity(i);

            }
            });
    }
}

I think that's because you missed the setting adapter to the ListView . And also make sure your items have data inside it .

mArrayAdapter = new ArrayAdapter<String> (getApplicationContext(), android.R.layout.simple_list_item_1, items); 
lv.setAdapter(mArrayAdapter); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM