GeezerVisonThe World Wide Web, restored to 800 pixels of glory. |
| Home|About|Gaming cabinet|Archives|How it works|Starter sites |
GeezerVison is showing www.runoob.com — 4.5.2 Intent之复杂数据的传递 | 菜鸟教程 Android 基础入门教程
4.5.2 Intent之复杂数据的传递分类 Android 基础入门教程本节引言:
1.Intent传递简单数据
2.Intent传递数组
写入数组: bd.putStringArray("StringArray", new String[]{"呵呵","哈哈"});
//可把StringArray换成其他数据类型,比如int,float等等...
读取数组: String[] str = bd.getStringArray("StringArray")
3.Intent传递集合
1)List<基本数据类型或String>写入集合: intent.putStringArrayListExtra(name, value) intent.putIntegerArrayListExtra(name, value) 读取集合: intent.getStringArrayListExtra(name) intent.getIntegerArrayListExtra(name) 2)List< Object>将list强转成Serializable类型,然后传入(可用Bundle做媒介) 写入集合: putExtras(key, (Serializable)list) 读取集合: (List<Object>) getIntent().getSerializable(key) PS:Object类需要实现Serializable接口 3)Map<String, Object>,或更复杂的解决方法是:外层套个List //传递复杂些的参数
Map<String, Object> map1 = new HashMap<String, Object>();
map1.put("key1", "value1");
map1.put("key2", "value2");
List<Map<String, Object>> list = new ArrayList<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);
4.Intent传递对象传递对象的方式有两种:将对象转换为Json字符串或者通过Serializable,Parcelable序列化 不建议使用Android内置的抠脚Json解析器,可使用fastjson或者Gson第三方库! 1)将对象转换为Json字符串Gson解析的例子: Model: public class Book{
private int id;
private String title;
//...
}
public class Author{
private int id;
private String name;
//...
}
写入数据: Book book=new Book();
book.setTitle("Java编程思想");
Author author=new Author();
author.setId(1);
author.setName("Bruce Eckel");
book.setAuthor(author);
Intent intent=new Intent(this,SecondActivity.class);
intent.putExtra("book",new Gson().toJson(book));
startActivity(intent);
读取数据: String bookJson=getIntent().getStringExtra("book");
Book book=new Gson().fromJson(bookJson,Book.class);
Log.d(TAG,"book title->"+book.getTitle());
Log.d(TAG,"book author name->"+book.getAuthor().getName());
2)使用Serializable,Parcelable序列化对象1.Serializable实现:
2.Parcelable实现: 一般流程:
一些解释:
实现Parcelable接口的代码示例: //Internal Description Interface,You do not need to manage
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel parcel, int flags){
parcel.writeString(bookName);
parcel.writeString(author);
parcel.writeInt(publishTime);
}
public static final Parcelable.Creator<Book> CREATOR = new Creator<Book>() {
@Override
public Book[] newArray(int size) {
return new Book[size];
}
@Override
public Book createFromParcel(Parcel source) {
Book mBook = new Book();
mBook.bookName = source.readString();
mBook.author = source.readString();
mBook.publishTime = source.readInt();
return mBook;
}
};
Android Studio生成Parcleable插件: Intellij/Andriod Studio插件android-parcelable-intellij-plugin 只要ALT+Insert,即可直接生成Parcleable接口代码。 另外:Android中大量用到Parcelable对象,实现Parcable接口又是非常繁琐的,可以用到 第三方的开源框架:Parceler,因为Maven的问题,暂时还没试。 3.两种序列化方式的比较: 两者的比较:
5.Intent传递Bitmapbitmap默认实现Parcelable接口,直接传递即可 实现代码: Bitmap bitmap = null;
Intent intent = new Intent();
Bundle bundle = new Bundle();
bundle.putParcelable("bitmap", bitmap);
intent.putExtra("bundle", bundle);
6.传来传去不方便,直接定义全局数据
关键部分代码: 1)自定义Application类: class MyApp extends Application {
private String myState;
public String getState(){
return myState;
}
public void setState(String s){
myState = s;
}
}
2)AndroidManifest.xml中声明: <application android:name=".MyApp" android:icon="@drawable/icon" android:label="@string/app_name"> 3)在需要的地方调用: class Blah extends Activity {
@Override
public void onCreate(Bundle b){
...
MyApp appState = ((MyApp)getApplicationContext());
String state = appState.getState();
...
}
}
高逼格写法 :在任何位置都能获取到Application全局对象。Applicaiton是系统的一个组件,他也有自己的一个生命周期,我们可以在onCraete里获得这个 Application对象。贴下修改后的代码吧! class MyApp extends Application {
private String myState;
private static MyApp instance;
public static MyApp getInstance(){
return instance;
}
public String getState(){
return myState;
}
public void setState(String s){
myState = s;
}
@Override
public void onCreate(){
onCreate();
instance = this;
}
}
然后在任意地方我们就可以直接调用:MyApp.getInstance()来获得Application的全局对象! 注意事项:
7.单例模式传参
范例代码:(代码来自于网上~) ①定义一个单例类: public class XclSingleton
{
//单例模式实例
private static XclSingleton instance = null;
//synchronized 用于线程安全,防止多线程同时创建实例
public synchronized static XclSingleton getInstance(){
if(instance == null){
instance = new XclSingleton();
}
return instance;
}
final HashMap<String, Object> mMap;
private 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);
}
}
②设置参数: XclSingleton.getInstance().put("key1", "value1");
XclSingleton.getInstance().put("key2", "value2");
本节小结:
点我分享笔记取消
Android 基础入门教程
|
| You are visitor 01402792 — GeezerVison does not store the pages you look up. Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required. |