有意向的ListView(JSON数据) [英] Intent with ListView (JSON data)

查看:134
本文介绍了有意向的ListView(JSON数据)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我把JSON数据到ListView。我想在列表中的数据(ItemTitle,ItemText,latit,longit,日期)可以被点击项目时转移到另一个活动(result.java)。下面是code:

I put JSON data to the ListView. I want data in the list("ItemTitle", "ItemText", "latit", "longit", "date") can transfer to another activity(result.java)when item is clicked. Here are the code:

活动:

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:

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:

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

谢谢!

推荐答案

始终使用非UI线程从服务器获取数据。看着你code,它看起来像您正在使用的UI线程获取数据。您可以使用的AsyncTask 粘贴写在的getJSON 类code中的 doInBackground 的AsyncTask 对象的方法。

Always use a non-UI thread to fetch data from servers. Looking at your code it looks like you are using the UI thread to fetch data. You may use AsyncTask and paste the code written in the GetJson class in the doInBackground method of an AsyncTask object.

现在你的问题的点击列表项数据传递到下一个活动。你将不得不要么使类新闻实施 Parcelable 序列化接口。实现这些类允许您自定义对象数据发送到另一个活动。最有效的方式是贯彻落实 Parcelable

Now about your problem to pass the clicked list item data to the next activity. You will have to either make the class News implement Parcelable or Serializable interface. implementing these classes allows you to send the custom object data to another activity. The most efficient way is to implement Parcelable.

详细信息请参考以下链接:
http://developer.android.com/reference/android/os/Parcelable.html

Check the following links for more details: http://developer.android.com/reference/android/os/Parcelable.html

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

希望这有助于解释。

这篇关于有意向的ListView(JSON数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆