2012-07-27 24 views
1

我把JSON數據放到ListView中。當單擊項目時,我希望列表中的數據(「ItemTitle」,「ItemText」,「latit」,「longit」,「date」)可以轉移到另一個活動(result.java)。下面是代碼:意圖與ListView(JSON數據)

活動:

public class Earthquake extends Activity { 
public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.earthquake); 
     getData(); 
    } 

private void getData() { 
    // TODO Auto-generated method stub 
    ListView list = (ListView)findViewById(R.id.earthquake); 
    try { 
     List<News> newes = GetJson.update();   

    List<HashMap<String, Object>> data = new ArrayList<HashMap<String, Object>>(); 
    for(News news : newes){ 
     HashMap<String, Object> item = new HashMap<String, Object>(); 
     item.put("ItemTitle", news.getPlace()); 
     item.put("ItemText", "Magnitude: "+news.getMag()); 
     item.put("latit", news.getLatit()); 
     item.put("longit", news.getLongit()); 
     item.put("date", news.getTime()); 
     data.add(item); 
    } 

    SimpleAdapter adapter = new SimpleAdapter(this, data, R.layout.list_earthquake, 
      new String[]{"ItemTitle", "ItemText", "latit", "longit", "date"}, 
      new int[]{R.id.ItemTitle, R.id.ItemText, R.id.latit, R.id.longit, R.id.date}); 
    list.setAdapter(adapter); 
    list.setOnItemClickListener(new OnItemClickListener() { 

     public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, 
       long arg3) { 
      //intent 
     } 
    }); 

    } catch (Exception e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
    } 

} 

GetJson.java:

public class GetJson { 

public static List<News> update() throws Exception, IOException { 
    // TODO Auto-generated method stub 
    String path = "http://earthquake.usgs.gov/earthquakes/feed/geojson/all/hour"; 
    HttpURLConnection conn = (HttpURLConnection)new URL(path).openConnection(); 
    conn.setConnectTimeout(5000); 
    conn.setRequestMethod("GET"); 
    if(conn.getResponseCode() == 200){ 
     InputStream json = conn.getInputStream(); 
     return parseJSON(json); 
    } 
    return null; 
} 

private static List<News> parseJSON(InputStream jsonStream) throws Exception { 
    // TODO Auto-generated method stub 
    List<News> list = new ArrayList<News>(); 
    byte[] data = StreamTool.read(jsonStream); 
    String json = new String(data); 

    //start decoad JSON 
    JSONObject jsonObject1 = new JSONObject(json); 
    String object1 = jsonObject1.getString("features"); 
    JSONArray features = new JSONArray(object1); 
    for(int i = 0; i < features.length(); i++){ 
     JSONObject object2 = features.getJSONObject(i); 
     JSONObject properties = object2.getJSONObject("properties"); 
      String place = properties.getString("place"); 
      int mag = properties.getInt("mag"); 
      String time = properties.getString("time"); 
     JSONObject geometry = object2.getJSONObject("geometry"); 
      JSONArray coordinates = geometry.getJSONArray("coordinates"); 
       String longit = coordinates.getString(0); 
       String latit = coordinates.getString(1); 
      list.add(new News(mag, longit, latit, place, time)); 
    } 
    return list; 
    } 
    } 

News.java:

public class News { 
private Integer mag; 
private String longit; 
private String latit; 
private String place, time; 
public News(){} 
public News(Integer mag, String longit, String latit, String place, String time){ 
    this.mag = mag; 
    this.longit = longit; 
    this.latit = latit; 
    this.place = place; 
    this.time = time; 
} 

public Integer getMag(){ 
    return mag; 
} 
public String getLongit(){ 
    return longit; 
} 
public String getLatit(){ 
    return latit; 
} 
public String getPlace(){ 
    return place; 
} 
public String getTime(){ 
    return time; 
} 
} 

謝謝!

+0

請查看下面的鏈接http://www.androidhive.info/2012/01/android-json-parsing-tutorial/ – 2012-07-27 10:10:05

回答

1

我建議你改寫你的SimpleAdapter以延伸ArrayAdapter<News>。創建另一個ListHasMap s是相當無用的,並消耗額外的內存。比讓你的News類實現Parcelable。並且在onClick()中,您可以這樣稱呼:

public void onItemClick(AdapterView<?> parent, View view, int position, long id) { 
    News news = parent.getItem(position); 
    if (news != null) { 
     Intent intent = new Intent(...); 
     intent.put("news", news); 
     startActivity(intent); 
    } 
} 
0

始終使用非UI線程從服務器獲取數據。看看你的代碼,看起來你正在使用UI線程來獲取數據。您可以使用AsyncTask並將GetJson類中寫入的代碼粘貼到AsyncTask對象的doInBackground方法中。

現在您可以將點擊列表項數據傳遞給下一個活動。您將必須使類News實現ParcelableSerializable接口。實現這些類允許您將自定義對象數據發送到其他活動。最有效的方法是執行Parcelable

詳細信息請參考以下鏈接: http://developer.android.com/reference/android/os/Parcelable.html

http://developer.android.com/reference/java/io/Serializable.html

希望這有助於解釋。

+0

感謝您關於使用AsyncTask的建議! – Hinata 2012-07-30 14:08:02