Intent传各种值

Intent传各种值,第1张

概述Android五种数据传递方法汇总Android开发中,在不同模块(如Activity)间经常会有各种各样的数据需要相互传递,我把常用的几种 方法都收集到了一起。它们各有利弊,有各自的应用场景。 我现在把它们集中到一个例子中展示,在例子中每一个按纽代表了一种实现方法。1. 利用Intent对象携带简单数据利用Intent的Extra部分来存储我们想要传递的数据,可以传送int, long, ch 1.Array

private ArrayList checkList=new ArrayList();
Intent intent=new Intent(mytext.this,show.class);
intent.putStringArrayListExtra(“List”,checkList);
startActivity(intent);
调用
Intent intent=this.getIntent();
ArrayList<String> List=intent.getStringArrayListExtra(“List”);
ArrayList<HashMap<String,String>> myList=new ArrayList<HashMap< String,String>>();
///ListvIEw使用
ListVIEw ListvIEw=(ListVIEw)findVIEwByID(R.ID.MyListVIEw);
for(int i=0;i<List.size();i++)
{
HashMap<String,String> map=new HashMap<String,String>();
map.put(“ItemTitle”,List.get(i).toString());
myList.add(map);
}
SimpleAdapter mSchedule=new SimpleAdapter(this,myList,R.layout.show,new String[]{“ItemTitle”,”ItemTitle”},new int[]{R.ID.ItemTitle,R.ID.ItemTitle});
ListvIEw.setAdapter(mSchedule);


2.对象、列表(object,List)

前面都一样,上遍文章已经讲解,这边补充一下,只讲一个Serializable的。。另一个Parcelable都差不多,各自实现各自的接口,调用的时候可以不用那么麻烦,真心不喜欢用Bundle,挺多此一举的
改写
如果是List
List<Parking_Info> List=new ArrayList<Parking_Info>();//搜索结果集合
Intent intent=new Intent(linkMap.this,MapList.class);
intent.putExtra(“List”,(Serializable)List);
startActivity(intent);
这边直接强制转化就行了,再接受的时候用(List<Parking_Info>) getIntent().getSerializable(“List”)就可以接受到List<Parking_Info>数据了
ListList=(List)getIntent().getSerializableExtra(“List”);
调用起来倒也方便的
具体调用,遍历代码
@OverrIDe
public voID onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentVIEw(R.layout.Listmain);
// List<Parking_Info> List=(List<Parking_Info>)getIntent().getSerializableExtra(“List”);
//创建适配器
SimpleAdapter adapter = new SimpleAdapter(this,getData(),R.layout.mapList,
new String[]{“name”,”kongwei”,”code”,”addr”},
new int[]{R.ID.name,R.ID.kongwei,R.ID.code,R.ID.addr});
setlistadapter(adapter);
// ListVIEw ListvIEw=(ListVIEw)findVIEwByID(R.ID.SearchList);
// ListvIEw.setAdapter(adapter);
};
private List<Map<String,Object>> getData() {
List<Map<String,Object>> List = new ArrayList<Map<String,Object>>();
//获取参数
List<Parking_Info> myList=(List<Parking_Info>)getIntent().getSerializableExtra(“List”);
for(int i=0;i<myList.size();i++)
{
Map<String,Object> map = new HashMap<String,Object>();
map.put(“name”,myList.get(i).getPrk_name());
map.put(“kongwei”,”空位:”+myList.get(i).getKongwei());
map.put(“code”,”收费:”+ myList.get(i).getCode());
map.put(“addr”,”地址:”+myList.get(i).getPrk_addr());
List.add(map);
// Toast.makeText(this,myList.get(5).getPrk_name(),Toast.LENGTH_LONG).show();
}
return List;
}


AndroID五种数据传递方法汇总

AndroID开发中,在不同模块(如Activity)间经常会有各种各样的数据需要相互传递,我把常用的几种
方法都收集到了一起。它们各有利弊,有各自的应用场景。
我现在把它们集中到一个例子中展示,在例子中每一个按纽代表了一种实现方法。


1. 利用Intent对象携带简单数据

利用Intent的Extra部分来存储我们想要传递的数据,可以传送int,long,char等一些基础类型,对复杂的对象就无能为力了。
1.1 设置参数
//传递些简单的参数
Intent intentSimple = new Intent();
intentSimple.setClass(MainActivity.this,SimpleActivity.class);

Bundle bundleSimple = new Bundle();
bundleSimple.putString(“usr”,“xcl”);
bundleSimple.putString(“pwd”,“zj”);
intentSimple.putExtras(bundleSimple);

startActivity(intentSimple);

1.2 接收参数
this.setTitle(“简单的参数传递例子”);

//接收参数
Bundle bunde = this.getIntent().getExtras();
String eml = bunde.getString(“usr”);
String pwd = bunde.getString(“pwd”);


2. 利用Intent对象携带如ArrayList之类复杂些的数据

这种原理是和上面一种是一样的,只是要注意下。 在传参数前,要用新增加一个List将对象包起来。
2.1 设置参数
//传递复杂些的参数
Map<String,Object> map1 = new HashMap<String,Object>();
map1.put(“key1”,“value1”);
map1.put(“key2”,“value2”);
List<Map<String,Object>>();
List.add(map1);

Intent intent = new Intent();
intent.setClass(MainActivity.this,ComplexActivity.class);
Bundle bundle = new Bundle();
//须定义一个List用于在budnle中传递需要传递的ArrayList<Object>,这个是必须要的
ArrayList bundleList = new ArrayList();
bundleList.add(List);
bundle.putParcelableArrayList(“List”,bundleList);
intent.putExtras(bundle);
startActivity(intent);

2.1 接收参数
//接收参数
Bundle bundle = getIntent().getExtras();
ArrayList List = bundle.getParcelableArrayList(“List”);
//从List中将参数转回 List<Map<String,Object>>
List<Map<String,Object>> Lists= (List<Map<String,Object>>)List.get(0);

String sResult = “”;
for (Map<String,Object> m : Lists)
{
for (String k : m.keySet())
{
sResult += “\r\n”+k + ” : ” + m.get(k);
}
}


3. 通过实现Serializable接口

3.1 设置参数

利用Java语言本身的特性,通过将数据序列化后,再将其传递出去。
//通过Serializable接口传参数的例子
HashMap<String,String> map2 = new HashMap<String,String>();
map2.put(“key1”,“value1”);
map2.put(“key2”,“value2”);

Bundle bundleSerializable = new Bundle();
bundleSerializable.putSerializable(“serializable”,map2);
Intent intentSerializable = new Intent();
intentSerializable.putExtras(bundleSerializable);
intentSerializable.setClass(MainActivity.this,
SerializableActivity.class);
startActivity(intentSerializable);

3.2 接收参数

this.setTitle(“Serializable例子”);

//接收参数
Bundle bundle = this.getIntent().getExtras();
//如果传 linkedHashMap,则bundle.getSerializable转换时会报ClassCastException,不知道什么原因
//传HashMap倒没有问题。
HashMap<String,String> map = (HashMap<String,String>)bundle.getSerializable(“serializable”);

String sResult = “map.size() =”+map.size();

I@R_403_6704@tor iter = map.entrySet().i@R_403_6704@tor();
while(iter.hasNext())
{
Map.Entry entry = (Map.Entry)iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
sResult +=”\r\n key—-> “+(String)key;
sResult +=”\r\n value—-> “+(String)value;
}


4. 通过实现Parcelable接口

这个是通过实现Parcelable接口,把要传的数据打包在里面,然后在接收端自己分解出来。这个是AndroID独有的,在其本身的源码中也用得很多,
效率要比Serializable相对要好。

4.1 首先要定义一个类,用于 实现Parcelable接口

因为其本质也是序列化数据,所以这里要注意定义顺序要与解析顺序要一致噢。
public class XclParcelable implements Parcelable {
//定义要被传输的数据
public int mInt;
public String mStr;
public HashMap<String,String> mMap = new HashMap<String,String>();
//Describe the kinds of special objects contained in this Parcelable’s marshalled representation.
public int describeContents() {
return 0;
}
//Flatten this object in to a Parcel.
public voID writetoParcel(Parcel out,int flags) {
//等于将数据映射到Parcel中去
out.writeInt(mInt);
out.writeString(mStr);
out.writeMap(mMap);
}
//Interface that must be implemented and provIDed as a public CREATOR fIEld
//that generates instances of your Parcelable class from a Parcel.
public static final Parcelable.Creator<XclParcelable> CREATOR
= new Parcelable.Creator<XclParcelable>() {
public XclParcelable createFromParcel(Parcel in) {
return new XclParcelable(in);
}
public XclParcelable[] newArray(int size) {
return new XclParcelable[size];
}
};
private XclParcelable(Parcel in) {
//将映射在Parcel对象中的数据还原回来
//警告,这里顺序一定要和writetoParcel中定义的顺序一致才行!!!
mInt = in.readInt();
mStr = in.readString();
mMap = in.readHashMap(HashMap.class.getClassLoader());
}
public XclParcelable() {
// Todo auto-generated constructor stub
}
}

4.2 设置参数

//通过实现Parcelable接口传参的例子
Intent intentParcelable = new Intent();
XclParcelable xp = new XclParcelable();
xp.mInt = 1;
xp.mStr = “字符串”;
xp.mMap = new HashMap<String,String>();
xp.mMap.put(“key”,“value”);
intentParcelable.putExtra(“Parcelable”,xp);
intentParcelable.setClass(MainActivity.this,ParcelableActivity.class);
startActivity(intentParcelable);

4.3 接收参数
//接收参数
Intent i = getIntent();
XclParcelable xp = i.getParcelableExtra(“Parcelable”);
TextVIEw tv = (TextVIEw)findVIEwByID(R.ID.tv);
tv.setText( ” mInt =”+xp.mInt
+”\r\n mStr”+xp.mStr
+”\r\n size()=”+xp.mMap.size());


5. 通过单例模式实现参数传递

单例模式的特点就是可以保证系统中一个类有且只有一个实例。这样很容易就能实现,

在A中设置参数,在B中直接访问了。这是几种方法中效率最高的。

5.1 定义一个单实例的类

//单例模式
public class XclSingleton
{
//单例模式实例
private static XclSingleton instance = null;
//synchronized 用于线程安全,防止多线程同时创建实例
public synchronized static XclSingleton getInstance(){
if(instance == null){
instance = new XclSingleton();
}
return instance;
}
final HashMap%lt;String,Object> mMap;
public XclSingleton()
{
mMap = new HashMap<String,Object>();
}
public voID put(String key,Object value){
mMap.put(key,value);
}
public Object get(String key)
{
return mMap.get(key);
}

}

5.2 设置参数

//通过单例模式传参数的例子
XclSingleton.getInstance().put(“key1”,“value1”);
XclSingleton.getInstance().put(“key2”,“value2”);

Intent intentSingleton = new Intent();
intentSingleton.setClass(MainActivity.this,
SingletonActivity.class);
startActivity(intentSingleton);

5.3 接收参数
//接收参数
HashMap<String,Object> map = XclSingleton.getInstance().mMap;
String sResult = “map.size() =”+map.size();
//遍历参数
I@R_403_6704@tor iter = map.entrySet().i@R_403_6704@tor();
while(iter.hasNext())
{
Map.Entry entry = (Map.Entry)iter.next();
Object key = entry.getKey();
Object value = entry.getValue();
sResult +=”\r\n key—-> “+(String)key;
sResult +=”\r\n value—-> “+(String)value;
}

推荐阅读:
Intent 传递数据,bundle 传递数组
intent几种传值数组、对象、集合(Array,Object,List)

总结

以上是内存溢出为你收集整理的Intent传各种值全部内容,希望文章能够帮你解决Intent传各种值所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://www.outofmemory.cn/web/1089382.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-27
下一篇 2022-05-27

发表评论

登录后才能评论

评论列表(0条)

保存