在两个activity之间使用intent传递list类型的对象

在两个activity之间传递list类型的对象。

 

第一个界面

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>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>();
           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

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> mylist = (List>)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

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 getMap(JSONObject object, String key)throws JSONException {
        returntoMap(object.getJSONObject(key));
   }
 
   
   publicstaticMap toMap(JSONObject object)throws JSONException {
       
       Map 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

activity_list_view.xml
 
 
         
         
         


 

第二个layout(自己新建的)

my_listitem.xml

 
 
 
        "
    
        
 


 


你可能感兴趣的:(android学习笔记)