JSON:在两个activity之间使用intent传递list类型的对象
2015-11-28 13:55
721 查看
博客源址:在两个activity之间使用intent传递list类型的对象
博客时间:2012-11-12 08:49
在两个activity之间传递list类型的对象。
第一个界面
[java] view
plaincopyprint?
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public classQueryActivity extends Activity {
private Buttonbutton;
private List<Map<String,Object>>list;
Stringresult="[{\"city\":\"changsha\",\"street\":\"no2\"},{\"city\":\"beijing\",\"street\":\"no5\"}]";
@SuppressWarnings("unchecked")
public void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_query);
this.button=(Button)findViewById(R.id.button);
button.setOnClickListener(new buttonClick());
String result1="{\"address\":"+result+"}";
Log.d("result1",result1);
try{
JSONObject json=new JSONObject(result1);
JSONArray jsonlist=json.getJSONArray("address");
list = new ArrayList<Map<String,Object>>();
list=JsonHelper.toList(jsonlist);
Log.d("list***",list.toString());
}
catch(Exception e){
}
}
classbuttonClickimplements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intentintent=newIntent(QueryActivity.this,ListViewActivity.class);
intent.putExtra("list", (Serializable)list);
QueryActivity.this.startActivity(intent);
Log.d("list",(Serializable)list+"");
}
}
}
第二个界面
有两个layout文件 创建时候注意listview在创建activity的时候的layout中定义。
例如:ListViewActivity
Java
[java] view
plaincopyprint?
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public classListViewActivity extends Activity {
@SuppressWarnings("unchecked")
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
Intent intent=getIntent();
List<HashMap<String,String>> mylist = (List<HashMap<String, String>>)intent.getSerializableExtra("list");
ListView list = (ListView)findViewById(R.id.MyListView);
SimpleAdapter mSchedule = new SimpleAdapter(
this, //没什么解释
mylist,//数据来源
R.layout.my_listitem,//ListItem的XML实现
//动态数组与ListItem对应的子项
new String[] {"city","street"},
//ListItem的XML文件里面的两个TextView ID
new int[] {R.id.edit01,R.id.edit02});
//添加并且显示
list.setAdapter(mSchedule);
}
}
其中还会用到一个工具类 将一个jsonArray类型的字符串转换成list—hashmap
Java.class
[java] view
plaincopyprint?
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;
@SuppressWarnings({ "rawtypes", "unchecked" })
public classJsonHelper {
public static Object toJSON(Objectobject) throwsJSONException {
if (objectinstanceof Map) {
JSONObject json = new JSONObject();
Map map = (Map) object;
for (Object key : map.keySet()) {
json.put(key.toString(), toJSON(map.get(key)));
}
return json;
} else if (object instanceof Iterable) {
JSONArray json = new JSONArray();
for (Object value : ((Iterable)object)) {
json.put(value);
}
return json;
} else {
return object;
}
}
publicstaticbooleanisEmptyObject(JSONObject object) {
return object.names() ==null;
}
publicstaticMap<String, Object> getMap(JSONObject object, String key)throws JSONException {
returntoMap(object.getJSONObject(key));
}
publicstaticMap<String, Object> toMap(JSONObject object)throws JSONException {
Map<String,Object> map = newHashMap();
Iterator keys = object.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
map.put(key, fromJson(object.get(key)));
}
return map;
}
public static List toList(JSONArrayarray) throwsJSONException {
List list = new ArrayList();
for (int i = 0; i < array.length(); i++) {
list.add(fromJson(array.get(i)));
}
return list;
}
privatestaticObject fromJson(Object json)throws JSONException {
if (json == JSONObject.NULL) {
return null;
} else if (json instanceof JSONObject) {
returntoMap((JSONObject) json);
} else if (json instanceof JSONArray) {
returntoList((JSONArray) json);
} else {
return json;
}
}
}
第一个layout
[html] view
plaincopyprint?
<span style="font-size:18px;">activity_list_view.xml
<?xmlversionxmlversion="1.0"encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListViewandroid:layout_widthListViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/MyListView">
</ListView>
</LinearLayout></span>
第二个layout(自己新建的)
my_listitem.xml
[html] view
plaincopyprint?
<?xmlversionxmlversion="1.0"encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content"
android:id="@+id/MyListItem"
android:paddingBottom="3dip"
android:paddingLeft="10dip">
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/edit01"/>"
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/edit02"/>
</LinearLayout>
博客时间:2012-11-12 08:49
在两个activity之间传递list类型的对象。
第一个界面
[java] view
plaincopyprint?
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import org.json.JSONArray;
import org.json.JSONObject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public classQueryActivity extends Activity {
private Buttonbutton;
private List<Map<String,Object>>list;
Stringresult="[{\"city\":\"changsha\",\"street\":\"no2\"},{\"city\":\"beijing\",\"street\":\"no5\"}]";
@SuppressWarnings("unchecked")
public void onCreate(BundlesavedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_query);
this.button=(Button)findViewById(R.id.button);
button.setOnClickListener(new buttonClick());
String result1="{\"address\":"+result+"}";
Log.d("result1",result1);
try{
JSONObject json=new JSONObject(result1);
JSONArray jsonlist=json.getJSONArray("address");
list = new ArrayList<Map<String,Object>>();
list=JsonHelper.toList(jsonlist);
Log.d("list***",list.toString());
}
catch(Exception e){
}
}
classbuttonClickimplements OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intentintent=newIntent(QueryActivity.this,ListViewActivity.class);
intent.putExtra("list", (Serializable)list);
QueryActivity.this.startActivity(intent);
Log.d("list",(Serializable)list+"");
}
}
}
第二个界面
有两个layout文件 创建时候注意listview在创建activity的时候的layout中定义。
例如:ListViewActivity
Java
[java] view
plaincopyprint?
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.widget.ListView;
import android.widget.SimpleAdapter;
public classListViewActivity extends Activity {
@SuppressWarnings("unchecked")
@Override
publicvoidonCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_view);
Intent intent=getIntent();
List<HashMap<String,String>> mylist = (List<HashMap<String, String>>)intent.getSerializableExtra("list");
ListView list = (ListView)findViewById(R.id.MyListView);
SimpleAdapter mSchedule = new SimpleAdapter(
this, //没什么解释
mylist,//数据来源
R.layout.my_listitem,//ListItem的XML实现
//动态数组与ListItem对应的子项
new String[] {"city","street"},
//ListItem的XML文件里面的两个TextView ID
new int[] {R.id.edit01,R.id.edit02});
//添加并且显示
list.setAdapter(mSchedule);
}
}
其中还会用到一个工具类 将一个jsonArray类型的字符串转换成list—hashmap
Java.class
[java] view
plaincopyprint?
import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;
import java.util.*;
@SuppressWarnings({ "rawtypes", "unchecked" })
public classJsonHelper {
public static Object toJSON(Objectobject) throwsJSONException {
if (objectinstanceof Map) {
JSONObject json = new JSONObject();
Map map = (Map) object;
for (Object key : map.keySet()) {
json.put(key.toString(), toJSON(map.get(key)));
}
return json;
} else if (object instanceof Iterable) {
JSONArray json = new JSONArray();
for (Object value : ((Iterable)object)) {
json.put(value);
}
return json;
} else {
return object;
}
}
publicstaticbooleanisEmptyObject(JSONObject object) {
return object.names() ==null;
}
publicstaticMap<String, Object> getMap(JSONObject object, String key)throws JSONException {
returntoMap(object.getJSONObject(key));
}
publicstaticMap<String, Object> toMap(JSONObject object)throws JSONException {
Map<String,Object> map = newHashMap();
Iterator keys = object.keys();
while (keys.hasNext()) {
String key = (String) keys.next();
map.put(key, fromJson(object.get(key)));
}
return map;
}
public static List toList(JSONArrayarray) throwsJSONException {
List list = new ArrayList();
for (int i = 0; i < array.length(); i++) {
list.add(fromJson(array.get(i)));
}
return list;
}
privatestaticObject fromJson(Object json)throws JSONException {
if (json == JSONObject.NULL) {
return null;
} else if (json instanceof JSONObject) {
returntoMap((JSONObject) json);
} else if (json instanceof JSONArray) {
returntoList((JSONArray) json);
} else {
return json;
}
}
}
第一个layout
[html] view
plaincopyprint?
<span style="font-size:18px;">activity_list_view.xml
<?xmlversionxmlversion="1.0"encoding="utf-8"?>
<LinearLayout
android:id="@+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ListViewandroid:layout_widthListViewandroid:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/MyListView">
</ListView>
</LinearLayout></span>
第二个layout(自己新建的)
my_listitem.xml
[html] view
plaincopyprint?
<?xmlversionxmlversion="1.0"encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content"
android:id="@+id/MyListItem"
android:paddingBottom="3dip"
android:paddingLeft="10dip">
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/edit01"/>"
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:id="@+id/edit02"/>
</LinearLayout>
相关文章推荐
- javascript加密之md5加密
- jsp页面中的代码执行加载顺序介绍
- js选项卡
- JavaScript数据类型和数据类型之间的转换关系
- JSONP跨域的原理解析及其实现介绍
- JSP
- JavaScript实现页面跳转的几种常用方式
- 【Ajax 3】JavaScript封装Ajax
- 【Ajax 3】JavaScript封装Ajax
- JS基础——选项卡套选项卡(函数传参)
- Javascript页面跳转常见实现方式汇总
- Extjs GridPanel用法详解
- <17> go json
- JS基础—选项卡套选项卡(函数传参)
- jsp和js的区别
- Javascript编程之继承实例汇总
- 使用JavaScript实现一个“字节码解释器”,并用它重新实现JS科学计算器的后端(待续)
- Javascript编程中几种继承方式比较分析
- JSP详细解析
- JavaScript 对象 实例