android Activity间List传值

第一个Activity:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;

public class AndroidActivity1 extends Activity {
    /** Called when the activity is first created. */
 
 private TextView tv_test;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        tv_test=(TextView)findViewById(R.id.tv_01);

        tv_test.setOnClickListener(
          
          new TextView.OnClickListener()
          {
           public void onClick(View v)
           {
             Intent intent=new Intent();
                   intent.setClass(AndroidActivity1.this, AndroidActivity2.class);
                   
                   List<Map<String,String>>list1=new ArrayList<Map<String,String>>();
                   Map<String ,String> map1=new HashMap<String ,String>();
                   map1.put("title", "刘德华");
                   Map<String ,String> map2=new HashMap<String ,String>();
                   map2.put("title", "张学友");
                   Map<String ,String> map3=new HashMap<String ,String>();
                   map3.put("title", "黎明");
                   Map<String ,String> map4=new HashMap<String ,String>();
                   map4.put("title", "郭富城");
                   Map<String ,String> map5=new HashMap<String ,String>();
                   map5.put("title", "李连杰");
                   
                   list1.add(map1);
                   list1.add(map2);
                   list1.add(map3);
                   list1.add(map4);
                   list1.add(map5);
                   
                   Bundle bundle=new Bundle();
                   
                   bundle.putParcelableArrayList("list", (ArrayList)list1);
                   intent.putExtras(bundle);
                   startActivity(intent);
           }
          }
          
          );
    }
}

第二个Activity:

import java.util.ArrayList;
import java.util.Map;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;

public class AndroidActivity2 extends Activity {
    /** Called when the activity is first created. */
 
 private TextView mytest;
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main1);
        
        mytest=(TextView)findViewById(R.id.mytest);
        
        Bundle bundle=this.getIntent().getExtras();
        
        ArrayList list2 = bundle.getParcelableArrayList("list");
        
        StringBuffer sb=new StringBuffer("");
        
        for(int i=0;i<list2.size();i++)
        {
         Map<String ,String> map=(Map<String,String>)list2.get(i);
         sb.append(map.get("title")).append("\n");
        }
        mytest.setText(sb.toString());
    }
}

你可能感兴趣的:(Activity)