android - ListView 的 Intent (JSON 数据)

标签 android android-intent android-listview

我将 JSON 数据放入 ListView。我希望列表中的数据(“ItemTitle”、“ItemText”、“latit”、“longit”、“date”)可以在单击项目时传输到另一个 Activity (result.java)。这是代码:

Activity :

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;
     }
  }

新闻.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;
}
}

谢谢!!

最佳答案

我建议你重写你的 SimpleAdapter扩展 ArrayAdapter<News> .创建另一个 ListHasMap s 非常无用并且会消耗额外的内存。比让你的 News类(class)工具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);
    }
}

关于android - ListView 的 Intent (JSON 数据),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11685611/

相关文章:

Android Studio Win 8.1 apk 删除访问被拒绝

android - Android 上的祝贺屏幕

android - 为什么我的 intent-filter 会匹配它不应该匹配的 URI?

android - 为什么 OnNewIntent() 中的代码会执行两次?

android - 调整 TextView 大小以适合文本(带有 9-patch 背景图像)?

android - Android图像的Intent.ACTION_VIEW

android - 从 API 22 升级到 23 : FileNotFoundException: **file full path** open failed: EACCES (Permission denied) 时出错

android - 以编程方式从应用程序启动 Skype 并传递号码 - Android

java - Android:如何将 HTML Table 解析为 ListView?

java - ImageView的onClick方法