joh*_*p34 9 java arrays android google-maps
我正在寻找类似于Array的东西,但它需要存储多种数据类型.Oracle Java教程说:"数组是一个容器对象,它包含固定数量的单一类型的值." 因此,如果我不能将数组用于多种类型,我该使用什么?
我有这个代码,一次只向地图添加一个标记,因为它在每个循环上写入我的lat和long值,并且只将最后一个传递给onPostExecute.所以我需要像数组一样传递多种形式的联系信息.即我从每个JSON字符串中提取位置,但我需要从该后台线程中拉出并将名称和电话号码传递给UI.
try {
String apples = endpoint.listContactInfo().execute().toString();
JSONObject jObject = new JSONObject(apples);
JSONArray jsonArr = jObject.getJSONArray("items");
for(int i =0 ; i<jsonArr.length() ;i++ ){
JSONObject jsonObj1 = jsonArr.getJSONObject(i);
// Storing each json item in variable
String id = jsonObj1.getString(TAG_ID);
String nameFirst1 = jsonObj1.getString(TAG_FIRSTNAME);
String nameLast1 = jsonObj1.getString(TAG_LASTNAME);
String emailAddress1 = jsonObj1.getString(TAG_EMAIL);
String streetAddress1 = jsonObj1.getString(TAG_ADDRESS);
String phone1 = jsonObj1.getString(TAG_PHONE);
//test to see if made it to string
Log.d("YOUR_TAG", "First Name: " + nameFirst1 + " Last Name: " + nameLast1);
address = coder.getFromLocationName(streetAddress1,5);
Address location1 = address.get(0);
// SET LAT LNG VALUES FOR MARKER POINT
lati = location1.getLatitude();
longi = location1.getLongitude();
Log.d("Location", "Location:" + lati + " " + longi);
}
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return (long) 0;
}
// ADD MARKER TO MAP UI
protected void onPostExecute(Long result) {
mMap.addMarker(new MarkerOptions()
.position(new LatLng(lati, longi))
.title("Hello world"));
}
Run Code Online (Sandbox Code Playgroud)
use*_*689 10
You can use an ArrayList.
ArrayList<Object> listOfObjects = new ArrayList<Object>();
Run Code Online (Sandbox Code Playgroud)
And then add items to it.
listOfObjects.add("1");
listOfObjects.add(someObject);
Run Code Online (Sandbox Code Playgroud)
Or create your own object that encapsulates all the field that you require like
public class LocationData {
private double lat;
private double longitude;
public LocationData(double lat, double longitude) {
this.lat = lat;
this.longitude = longitude;
}
//getters
//setters
}
Run Code Online (Sandbox Code Playgroud)
and then add your lat/long pairs to an ArrayList of type LocationData
ArrayList<LocationData> listOfObjects = new ArrayList<LocationData>();
listOfObjects.add(new LocationData(lat, longitude));
Run Code Online (Sandbox Code Playgroud)
您可以创建自定义类的数组.
public class YourCustomClass {
String id;
String name;
double longitude;
// and many more fields ...
public YourCustomClass() { // constructor
}
public void setID(String id) {
this.id = id;
}
public String getID() {
return id;
}
// and many more getter and setter methods ...
}
Run Code Online (Sandbox Code Playgroud)
在您的自定义类中,您可以拥有任意数量的字段,您可以在其中存储数据,然后使用它:
// with array
YourCustomClass [] array = new YourCustomClass[10];
array[0] = new YourCustomClass();
array[0].setID("yourid");
String id = array[0].getID();
// with arraylist
ArrayList<YourCustomClass> arraylist = new ArrayList<YourCustomClass>();
arraylist.add(new YourCustomObject());
arraylist.get(0).setID("yourid");
String id = arraylist.get(0).getID();
Run Code Online (Sandbox Code Playgroud)
您还可以让AsyncTasks doInBackground(...)方法返回您的Custom-class:
protected void onPostExecute(YourCustomClass result) {
// do stuff...
}
Run Code Online (Sandbox Code Playgroud)
或者一个数组:
protected void onPostExecute(YourCustomClass [] result) {
// do stuff...
}
Run Code Online (Sandbox Code Playgroud)
或者一个ArrayList:
protected void onPostExecute(ArrayList<YourCustomClass> result) {
// do stuff...
}
Run Code Online (Sandbox Code Playgroud)
编辑:当然,您也可以创建自定义对象的ArrayList.
您应该考虑使用类型安全的异构容器模式。
数据存储在 a 中Map<Key<?>, Object>,对地图的访问隐藏在通用方法后面,这些方法会自动转换返回值。
public <T> T getObjectByKey(Key<T> key)
return (T) map.get(key);
Run Code Online (Sandbox Code Playgroud)
对于put.
| 归档时间: |
|
| 查看次数: |
45807 次 |
| 最近记录: |