iT邦幫忙

0

android 全域變數 + 傳值的問題

想要請問:

  • 使用open data的資料:https://gis.taiwan.net.tw/XMLReleaseALL_public/hotel_C_f.json
  • 因想要用android 的json得到的經緯度傳值到GoogleMap,因我原本想使用arraylist 去存在全域變數,再從google map那邊去抓經緯度,但不知道為什麼在googlemap 那邊只能抓到最後一筆的資料,想要知道是哪邊有錯嗎???還是我的關念有錯? 能否給我一個方向。謝謝

class TransTask extends AsyncTask<String, Void, String> implements OnMapReadyCallback {
StringBuffer json = new StringBuffer();
String Px;
String Py;
String sna;
JSONArray contacts;
private ArrayList movieList = new ArrayList<>();

    @Override
    protected String doInBackground(String... params) {
        try {
            URL url = new URL(params[0]);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream is = connection.getInputStream();
            BufferedReader br = new BufferedReader(new InputStreamReader(is));
            String line = br.readLine();
            while (line != null) {
                json.append(line);
                line = br.readLine();
            }
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return json.toString();
    }
    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        JSONObject jonObject = null;
        ArrayList<String> arrayList = new ArrayList<String>();

        User = (hotelmovie) getApplicationContext();

        try {
            jonObject = new JSONObject(s.toString());
             contacts = jonObject.getJSONObject("XML_Head").getJSONObject("Infos").getJSONArray("Info");
            for (int i = 0; i < contacts.length(); i++) {
                JSONObject c = contacts.getJSONObject(i);
                sna = c.getString("Name");
                Px = c.getString("Px");
                Py = c.getString("Py");
                hotelmovie movie = new hotelmovie(sna,Px,Py);
                movieList.add(movie);
                arrayList.add(sna);

                User.setTitle(sna);

                User.setPx(Px);
                User.setPy(Py);



                  //  User.setTitle(sna);
                   Log.i("Volley", User.getTitle());

            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map);
        mapFragment.getMapAsync(this);

    }
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;


        for (int i = 0; i < contacts.length(); i++) {
            Log.i("Volley 1", User.getTitle());
        }


        hotelmovie gv = (hotelmovie) getApplicationContext();
        LatLng sydney = new LatLng(-34, 151);
        mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
        mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));

      }
  }

}

1 個回答

0
海綿寶寶
iT邦大神 1 級 ‧ 2020-06-14 19:49:34
最佳解答

movieList, arrayList 都是 ArrayList
但是 User 就只是一個 class

for (int i = 0; i < contacts.length(); i++) {
    Log.i("Volley 1", User.getTitle());
}

改成

for (int i = 0; i < contacts.length(); i++) {
    Log.i("Volley 1", arrayList.get(i));
}

看看

好厲害~ 確實這樣子名字欄位全部都有讀到,但是,不好意思,但我想要再問一下,movieList 我原本是想要用全域變數去讀,那為什麼如果我想要改成用 movieList會沒辦法讀呢? 全域變數該設定的我都有做,不明白原因是什麼呢? 謝謝

public class hotelmovie extends Application {

    private String title,px,py;
    public hotelmovie() {
    }

    @Override
    public void onCreate() {
        super.onCreate();
        setTitle(title); //初始化全域性變數
    }


    public hotelmovie(String title, String px, String py) {
        this.title = title;
        this.px = px;
        this.py = py;
    }

    public String getTitle() {
        return title;
    }

    public void setTitle(String title) {
        this.title = title;
    }

    public String getPx() {
        return px;
    }


    public void setPx(String px) {
        this.px = px;
    }


    public String getPy() {
        return py;
    }

    public void setPy(String py) {
        this.py = py;
    }

    private static final String NAME = "hotelmovie";


}

我要發表回答

立即登入回答