我如何在列表视图 android 中使用我的 JsonArray?

How i use my JsonArray in listview android?

希望你们一切都好。我正在 android 中开发天气应用程序,其中 android 应用程序以 json 格式从服务器获取数据。在日志文件中,您可以看到我使用 volley 成功获取了 json 数据,现在我想在列表视图中显示该数据。我会怎么做???

    package com.example.the_weather_forecast;
    
    import androidx.appcompat.app.AppCompatActivity;
    
    import android.os.Bundle;
    import android.util.Log;
    import android.widget.ArrayAdapter;
    import android.widget.ListView;
    
    import com.android.volley.Request;
    import com.android.volley.RequestQueue;
    import com.android.volley.Response;
    import com.android.volley.RetryPolicy;
    import com.android.volley.VolleyError;
    import com.android.volley.toolbox.JsonArrayRequest;
    import com.android.volley.toolbox.Volley;
    
    import org.json.JSONArray;
    import org.json.JSONException;
    
    import java.util.ArrayList;
    
    public class MainActivity extends AppCompatActivity {
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            ArrayList<String> tubeLines = new ArrayList<String>();
    
            String URL = "xyz";
    
            RequestQueue requestQueue = Volley.newRequestQueue(this);
    
            JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, URL, null,
                    new Response.Listener<JSONArray>()
                    {
                        @Override
                        public void onResponse(JSONArray response) {
                            // display response
                            Log.d("Response", response.toString());
                            for(int i=0;i<response.length();i++){
                                try {
                                    JSONArray jsonArray = response.getJSONArray(i);
                                    String x = jsonArray.toString();
                                    tubeLines.add(x);
                                    System.out.println("reached" + x);
                                } catch (JSONException e) {
                                    e.printStackTrace();
                                }
                            }
                        }
                    },
                    new Response.ErrorListener()
                    {
                        @Override
                        public void onErrorResponse(VolleyError error) {
                            Log.d("Error.Response", error.toString());
                        }
                    }
            );
            getRequest.setRetryPolicy(new RetryPolicy() {
                @Override
                public int getCurrentTimeout() {
                    return 50000;
                }
    
                @Override
                public int getCurrentRetryCount() {
                    return 50000;
                }
    
                @Override
                public void retry(VolleyError error) throws VolleyError {
    
                }
            });
            ListView myListView = (ListView)findViewById(R.id.myListview);
            ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, 
            android.R.layout.simple_list_item_1, tubeLines);
            myListView.setAdapter(arrayAdapter);
            // add it to the RequestQueue
            requestQueue.add(getRequest);
        }
    }

日志文件: 这是我从服务器接收到的 json 数据 D/Response: [[10,10,10,10,15,17,13,11],[12,8,8,8,9,9,14,10],[0,0,0, 0,0,0,0,0]]

将您的列表视图代码移到 onResponse 和下面的 for 循环中,因为根据您的代码,首先将空数组适配器设置为 listview,然后使用 [= 从 api 接收响应13=].

JsonArrayRequest getRequest = new JsonArrayRequest(Request.Method.GET, URL, null,
                new Response.Listener<JSONArray>()
                {
                    @Override
                    public void onResponse(JSONArray response) {
                        // display response
                        Log.d("Response", response.toString());
                        for(int i=0;i<response.length();i++){
                            try {
                                JSONArray jsonArray = response.getJSONArray(i);
                                String x = jsonArray.toString();
                                tubeLines.add(x);
                                System.out.println("reached" + x);
                            } catch (JSONException e) {
                                e.printStackTrace();
                            }

                        }
         ListView myListView = (ListView)findViewById(R.id.myListview);
         ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, 
         android.R.layout.simple_list_item_1, tubeLines);
         myListView.setAdapter(arrayAdapter);
                    }
                },
                new Response.ErrorListener()
                {
                    @Override
                    public void onErrorResponse(VolleyError error) {
                        Log.d("Error.Response", error.toString());
                    }
                }
        );
        getRequest.setRetryPolicy(new RetryPolicy() {
            @Override
            public int getCurrentTimeout() {
                return 50000;
            }

            @Override
            public int getCurrentRetryCount() {
                return 50000;
            }

            @Override
            public void retry(VolleyError error) throws VolleyError {

            }
        });
        
        // add it to the RequestQueue
        requestQueue.add(getRequest);
    }