Java / Android scoop

时间:2016-04-14 14:00:12

标签: java android json

为什么Log.d("Test", "" + ListOfAttractions3.size() + "");Log.d("Test2", "" + ListOfAttractions3.size() + "");返回2时返回0,即使先调用DatabseRequest();?还有一些Log 2也是最后打印的,但我看不出原因?

代码:

public class Testlist extends Activity {
    List<Attractions> ListOfAttractions3 = new ArrayList<>();


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_testlist);
        DatabseRequest();
        Log.d("Test", "" + ListOfAttractions3.size() + "");
    }


    /*private void reFreshDisplay(){
        ListView listView2 = (ListView) findViewById(R.id.listView2);
        ArrayAdapter<Attractions> adapter = new ArrayAdapter<Attractions>(this, android.R.layout.simple_list_item_1, ListOfAttractions3);
        listView2.setAdapter(adapter);
        adapter.notifyDataSetChanged();
    }*/



    private void DatabseRequest(){

        Response.Listener<String> responseListener = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                try {
                    JSONArray jsonArray = new JSONArray(response);
                    int i = 0;

                    while(i < jsonArray.length()) {
                        JSONObject jsonObject = jsonArray.getJSONObject(i);

                        boolean success;
                        if (jsonObject.getBoolean("success")) success = true;
                        else success = false;


                        if (success) {
                            String attractionname = jsonObject.getString("attractionname");
                            double lng = jsonObject.getDouble("longitude");
                            double lat = jsonObject.getDouble("latitude");
                            int Rating = jsonObject.getInt("rating");

                            Attractions attraction = new Attractions(attractionname, lng, lat, Rating);

                            ListOfAttractions3.add(attraction);
                            Log.d("Test2", "" + ListOfAttractions3.size() + "");

                        } else {
                            AlertDialog.Builder builder = new AlertDialog.Builder(Testlist.this);
                            builder.setMessage("Connection to server Failed")
                                    .setNegativeButton("Retry", null)
                                    .create()
                                    .show();
                        }
                        i++;
                    }
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }
        };

        AttractionRequest attractionRequest = new AttractionRequest(responseListener);
        RequestQueue queue2 = Volley.newRequestQueue(Testlist.this);
        queue2.add(attractionRequest);
    }
}

0 个答案:

没有答案