我已经解析了一个 JSON 文件,然后将一个数组从 JSON 保存到一个字符串,并将其传递给下一个要解析的活动,但我有一个问题,即列表视图只显示第一组位置而不显示任何其他位置。
JSON 片段:
{
title: World Rover Scout Moot
locations: [
{
address: Kandersteg, Switzerland
latitude: 46.483742
longitude: 7.663157
name: 01st World Rover Moot (1921)
}
{
address: Ingarö, Sweden
latitude: 59.25235
longitude: 18.465536
name: 02nd World Rover Moot (1925)
}
{
address: Monzie, Scotland
latitude: 56.404182
longitude: -3.818855
name: 03rd World Rover Moot (1939)
}
{
address: Skjak, Norway
latitude: 61.898525
longitude: 8.369957
name: 04th World Rover Moot (1949)
}
{
address: Kandersteg, Switzerland
latitude: 46.483742
longitude: 7.663157
name: 05th World Rover Moot (1953)
}
{
address: Sutton Coldfield, UK
latitude: 52.558769
longitude: -1.802851
name: 06th World Rover Moot (1957)
}
{
address: Melbourne, Australia
latitude: -37.814251
longitude: 144.963165
name: 07th World Rover Moot (1961)
}
{
address: Melbourne, Australia
latitude: -37.814251
longitude: 144.963165
name: 08th World Rover Moot (1990-91)
}
{
address: Kandersteg, Switzerland
latitude: 46.483669
longitude: 7.663095
name: 09th World Rover Moot (1953)
}
{
address: Ransberg, Sweden
latitude: 58.454601
longitude: 14.226012
name: 10th World Moot (1996)
}
{
address: Mexico
latitude: 19.691061
longitude: -98.805557
name: 11th World Scout Moot (2000)
}
{
address: Hualien, Taiwan
latitude: 23.987925
longitude: 121.612175
name: 12th World Scout Moot (2004)
}
{
address: Nairobi, Kenya
latitude: -1.312558
longitude: 36.769932
name: 13th World Scout Moot
}
]
},
{
title: World Scout Interreligious Symposium
locations: [
{
address: Kampala, Uganda
latitude: 0.239059
longitude: 32.623615
name: 3rd World Scout Interreligious Symposium
}
]
}
源代码:
public class SingleMapSet extends Activity {
final String KEY_MAPS = "maps";
static final String KEY_MAPSETMAPSET = "";
static final String KEY_MAPNAME = "name";
static final String KEY_MAPLOCATION = "address";
static final String KEY_MAPLONG = "longitude";
static final String KEY_MAPLAT = "latitude";
ListView list;
org.scouts.android.maps.MapsetAdapter adapter;
/** Called when the activity is first created. */
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.playlists);
TextView lblTitle = (TextView) findViewById(R.id.actionbar);
lblTitle.setText(R.string.maps);
Intent in = getIntent();
String mapset = in.getStringExtra(KEY_MAPSETMAPSET);
mapset = mapset.substring(1);
mapset = mapset.substring(0, mapset.length() - 1);
final ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String jsonString = null;
try {
jsonString = mapset;
} catch (IllegalStateException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
JSONObject json = null;
try {
json = new JSONObject(jsonString);
} catch (JSONException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
try{
JSONArray jsonArray = json.getJSONArray("locations");
for(int i=0;i < jsonArray.length();i++){
HashMap<String, String> map = new HashMap<String, String>();
JSONObject maps = jsonArray.getJSONObject(i);
map.put("id", String.valueOf(i));
map.put(KEY_MAPNAME, maps.getString(KEY_MAPNAME));
map.put(KEY_MAPLOCATION, maps.getString(KEY_MAPLOCATION));
map.put(KEY_MAPLONG, maps.getString("longitude"));
map.put(KEY_MAPLAT, maps.getString("latitude"));
mylist.add(map);
}
}catch(JSONException e) {
Log.e("log_tag", "Error parsing data "+e.toString());
}
list=(ListView)findViewById(R.id.playlistlist);
adapter=new org.scouts.android.maps.MapsetAdapter (this, mylist);
list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
HashMap<String, String> map = mylist.get(position);
Intent in = new Intent(SingleMapSet.this, org.scouts.android.maps.SingleMapSet.class);
in.putExtra(KEY_MAPNAME, map.get(KEY_MAPNAME));
in.putExtra(KEY_MAPLOCATION, map.get(KEY_MAPLOCATION));
in.putExtra(KEY_MAPLONG, map.get(KEY_MAPLONG));
in.putExtra(KEY_MAPLAT, map.get(KEY_MAPLAT));
startActivity(in);
}
});
}
}; ;