Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
159 views
in Technique[技术] by (71.8m points)

java - How i use my JsonArray in listview android?

hope you all are fine . I am developing weather app in android where android app get data from server in json format. In log file you can see that i successfully get my json data using volley, now i want to display that data in list view. How i will do that ???

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

Log File : This my json data which i am receiving from my server 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]]


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

move your listview code inside onResponse and below to for loop because as per your code first empty array adapter set to listview and after that response receive from api using requestQueue.add(getRequest).

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...