[Android] Activity之間如何透過Intent傳遞複雜結構的data
2015-06-19 16:49
429 查看
原文链接:http://jimmy319.blogspot.tw/2011/12/android-activityintentdata-structure.html
Application是由許多Activities所組成,不同Activity之間是藉由Intent物件來進行啟動/關閉,Intent物件可以帶入data傳遞給它所喚起的Activity,它可能是上個Activity執行後的結果。
在Intentobject加入data最常見的方法即為putExtra(),它提供了多種overload方法,包含各種primitivedatatype以及其Array資料型態,另外它也支援實作Serializable與Parcelable兩種介面的資料型態。
當遇到需要傳遞較複雜的資料型態(自定義且putExtra沒有對應的多載函式)給即將開啟的Activity,例如:Customlistview'sdatasource就常使用HashMap、ArrayList等資料型態的組合,這些都是實作Serializable常見的類別,此時就可以採用putExtra(Stringname,Serializablevalue)將資料帶入Intentobject,接受端Activity再透過getSerializableExtra(Stringname)取得自定義的Serializable
data,藉此達到傳遞複雜資料的目的。
以下將示範透過Intent將自定義資料型態ArrayList<HashMap>傳遞給CustomListView作為其Adapter之資料來源。
程式碼說明
一、建立負責activateListActivity之Activity
IntentDemoActivity.java
二、宣告自定義資料型態
三、按鈕click事件觸發Intent並附帶自定義data
四、CustomListActivity透過getSerializableExtra()接收data並將其設為ListViewAdapter資料來源
RoleListActivity.java
ListAdapter.java
role_list.xml
role_list_row.xml
Application是由許多Activities所組成,不同Activity之間是藉由Intent物件來進行啟動/關閉,Intent物件可以帶入data傳遞給它所喚起的Activity,它可能是上個Activity執行後的結果。
在Intentobject加入data最常見的方法即為putExtra(),它提供了多種overload方法,包含各種primitivedatatype以及其Array資料型態,另外它也支援實作Serializable與Parcelable兩種介面的資料型態。
當遇到需要傳遞較複雜的資料型態(自定義且putExtra沒有對應的多載函式)給即將開啟的Activity,例如:Customlistview'sdatasource就常使用HashMap、ArrayList等資料型態的組合,這些都是實作Serializable常見的類別,此時就可以採用putExtra(Stringname,Serializablevalue)將資料帶入Intentobject,接受端Activity再透過getSerializableExtra(Stringname)取得自定義的Serializable
data,藉此達到傳遞複雜資料的目的。
以下將示範透過Intent將自定義資料型態ArrayList<HashMap>傳遞給CustomListView作為其Adapter之資料來源。
程式碼說明
一、建立負責activateListActivity之Activity
01 | package com.intentdemo; |
02 |
03 | import java.util.ArrayList; |
04 | import java.util.HashMap; |
05 |
06 | import android.app.Activity; |
07 | import android.content.Intent; |
08 | import android.os.Bundle; |
09 | import android.view.View; |
10 |
11 | public class IntentDemoActivity extends Activity { |
12 |
13 | private int [] icons={ |
14 | R.drawable.icon1,R.drawable.icon2, |
15 | R.drawable.icon3,R.drawable.icon4, |
16 | R.drawable.icon5,R.drawable.icon6 |
17 | }; |
18 |
19 | private String[] names={ |
20 | "role1" , "role2" , |
21 | "role3" , "role4" , |
22 | "role5" , "role6" |
23 | }; |
24 |
25 | private String[] tels={ |
26 | "000-11111" , "999-19191" , |
27 | "000-22222" , "919-49791" , |
28 | "101-33333" , "929-29391" |
29 | }; |
30 |
31 | @Override |
32 | public void onCreate(Bundle savedInstanceState){ |
33 | super .onCreate(savedInstanceState); |
34 | setContentView(R.layout.main); |
35 | } |
36 |
37 | } |
二、宣告自定義資料型態
01 | private ArrayList< |
02 | ArrayList new ArrayList(); |
03 | for ( int i= 0 ;i< 6 ;i++){ |
04 | HashMap< new HashMap< String,Object>(); |
05 | data.put( "icon" , icons[i]); |
06 | data.put( "name" , names[i]); |
07 | data.put( "tel" , tels[i]); |
08 | dataSet.add(data); |
09 | } |
10 | return dataSet; |
11 | } |
1 | public void onIntentButtonClick(View v){ |
2 | Intent intent= new Intent(); |
3 | intent.putExtra( "listData" , getListData()); |
4 | intent.setClass( this , RoleListActivity. class ); |
5 | startActivity(intent); |
6 | } |
01 | package com.intentdemo; |
02 |
03 | import java.util.ArrayList; |
04 | import java.util.HashMap; |
05 |
06 | import android.app.ListActivity; |
07 | import android.os.Bundle; |
08 |
09 | public class RoleListActivity extends ListActivity { |
10 |
11 | @Override |
12 | public void onCreate(Bundle savedInstanceState){ |
13 | super .onCreate(savedInstanceState); |
14 | setContentView(R.layout.role_list); |
15 |
16 | //get |
17 | ArrayList< |
18 | (ArrayList< |
19 | .getSerializableExtra( "listData" ); |
20 | this .setListAdapter( new ListAdapter( this ,data)); |
21 | } |
22 | } |
01 | package com.intentdemo; |
02 |
03 | import java.util.ArrayList; |
04 | import java.util.HashMap; |
05 |
06 | import android.content.Context; |
07 | import android.view.LayoutInflater; |
08 | import android.view.View; |
09 | import android.view.ViewGroup; |
10 | import android.widget.BaseAdapter; |
11 | import android.widget.ImageView; |
12 | import android.widget.TextView; |
13 |
14 | public class ListAdapter extends BaseAdapter { |
15 |
16 | private ArrayList< |
17 | private LayoutInflater inflater; |
18 |
19 | public ListAdapter(Context |
20 | inflater=LayoutInflater.from(c); |
21 | data=d; |
22 | } |
23 |
24 | @Override |
25 | public int getCount() { |
26 | return data.size(); |
27 | } |
28 |
29 | @Override |
30 | public Object getItem( int index) { |
31 | return data.get(index); |
32 | } |
33 |
34 | @Override |
35 | public long getItemId( int index) { |
36 | return index; |
37 | } |
38 |
39 | @Override |
40 | public View getView( int i, |
41 | ViewHolder holder; |
42 | if (v== null ){ |
43 | v=inflater.inflate(R.layout.role_list_row, null ); |
44 | holder= new ViewHolder(); |
45 | holder.icon=(ImageView)v.findViewById(R.id.icon); |
46 | holder.nameTxt=(TextView)v.findViewById(R.id.name); |
47 | holder.telTxt=(TextView)v.findViewById(R.id.tel); |
48 | v.setTag(holder); |
49 | } else { |
50 | holder=(ViewHolder)v.getTag(); |
51 | } |
52 |
53 | holder.icon.setImageResource((Integer)data.get(i).get( "icon" )); |
54 | holder.nameTxt.setText((String)data.get(i).get( "name" )); |
55 | holder.telTxt.setText((String)data.get(i).get( "tel" )); |
56 |
57 | return v; |
58 | } |
59 |
60 | static class ViewHolder{ |
61 | ImageView icon; |
62 | TextView nameTxt; |
63 | TextView telTxt; |
64 | } |
65 | } |
66 | </string,object> |
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout |
03 | xmlns:android = " |
04 | android:layout_width = "match_parent" |
05 | android:layout_height = "match_parent" |
06 | android:orientation = "vertical" > |
07 | < ListView |
08 | android:id = "@android:id/list" |
09 | android:layout_width = "match_parent" |
10 | android:layout_height = "match_parent" |
11 | /> |
12 | </ LinearLayout > |
01 | <? xml version = "1.0" encoding = "utf-8" ?> |
02 | < LinearLayout |
03 | xmlns:android = " |
04 | android:layout_width = "match_parent" |
05 | android:layout_height = "100dp" |
06 | android:orientation = "horizontal" > |
07 | < ImageView |
08 | android:id = "@+id/icon" |
09 | android:layout_width = "90dp" |
10 | android:layout_height = "90dp" |
11 | android:padding = "5dp" |
12 | /> |
13 |
14 | < LinearLayout |
15 | android:layout_width = "match_parent" |
16 | android:layout_height = "match_parent" |
17 | android:gravity = "center_vertical" |
18 | android:orientation = "vertical" |
19 | > |
20 | < TextView |
21 | android:id = "@+id/name" |
22 | android:layout_width = "match_parent" |
23 | android:layout_height = "wrap_content" |
24 | android:gravity = "center" |
25 | android:textSize = "20sp" |
26 | /> |
27 | < TextView |
28 | android:id = "@+id/tel" |
29 | android:layout_width = "match_parent" |
30 | android:layout_height = "wrap_content" |
31 | android:gravity = "center" |
32 | android:textSize = "20sp" |
33 | /> |
34 | </ LinearLayout > |
35 | </ LinearLayout > |
相关文章推荐
- 外媒评黑莓推 Android 手机:自取其辱
- Android L+ Theme 与 Toolbar 实例
- Android数据传递方法汇总
- Android Volley完全解析(一),初识Volley的基本用法
- Android 工具类(将 dp 转成 px 获取屏幕的高度/宽度 判断android SDK 版本是否大于等于5.0)
- Android图片上传(头像裁切+原图原样)
- android sqlite 一次建立好几张表
- Android:控件样式触发
- [转]Android 完美的隐藏软键盘方法 点击编辑框外软键盘消失
- Android Service(二)
- android开发中常见错误汇总
- Android studio 重置 DDMS面板布局
- 关于Android 和 JS 交互时调用不成功的问题
- android退出MainActivity后onDestroy不回调
- android触摸事件流程(一)
- Android Binder design and implementation
- Android activity的生命周期
- android 去掉顶部状态栏
- Android SERVICE后台服务进程的自启动和保持
- android activity 切换