簡體   English   中英

有人可以告訴我為什么我的listView不填充JSON數據嗎

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

我發現我現在的主要問題是我的意圖代碼。 我不知道該如何建立使數據從網站返回的意圖,因此,如果有人可以幫助我,那就太好了! 我正在嘗試從openweather api獲取JSON數據,我的應用程序將加載網頁,而不是將其帶入列表視圖。 現在,出於我的意圖,將打開一個包含我所需數據的網頁。 任何幫助將是巨大的!

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

            }
            });
    }
}

我認為這是因為您錯過了ListView的設置適配器。 並確保您的商品中包含數據。

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM