【问题标题】:How i use my JsonArray in listview android?我如何在 listview android 中使用我的 JsonArray?
【发布时间】:2021-04-16 10:31:26
【问题描述】:

希望你们一切都好。我正在 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/响应:[[10,10,10,10,15,17,13,11],[12,8,8,8,9,9,14,10],[0,0,0,0, 0,0,0,0]]

【问题讨论】:

    标签: java android arrays json android-volley


    【解决方案1】:

    将您的列表视图代码移动到 onResponse 和下面的 for 循环中,因为根据您的代码,第一个空数组适配器设置为 listview,然后使用 requestQueue.add(getRequest) 从 api 接收响应。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-19
      • 1970-01-01
      • 2021-06-20
      • 1970-01-01
      相关资源
      最近更新 更多