Android 之数据传递小结
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                Android 之数据传递小结
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                ????? Android開發中,在不同模塊(如Activity)間經常會有各種各樣的數據需要相互傳遞,常用的的有五種傳遞方式。它們各有利弊,有各自的應用場景。下面分別介紹一下:
1、?Intent對象傳遞簡單數據
????? Intent的Extra部分可以存儲傳遞的數據,可以傳送int, long, char等一些基礎類型。
[1]發送頁面:
Intent intent = new Intent(); intent.setClass(MainActivity.this, SecondActivity.class); Bundle bundle = new Bundle(); //打包發送 bundle.putString("name","123"); //綁定參數 intent.putExtra("maps",bundle); startActivity(intent);[2]接收頁面:
@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);TextView tv = new TextView(this);Intent intent = this.getIntent();Bundle bundle = intent.getBundleExtra("maps"); //獲取打包數據bundleString name = bundle.getString("name"); //取出需要的數據tv.setText(name);setContentView(tv); }或者@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.second);TextView txt = (TextView)this.findViewById(R.id.txt);Intent intent = this.getIntent();Bundle bundle = intent.getBundleExtra("maps"); //獲取打包數據bundleString name = bundle.getString("name"); //取出需要的數據txt.setText(name);}2.、Intent對象傳遞復雜數據(引入java.util.*)
????? 有時候傳遞如ArrayList之類復雜些的數據,這種原理是和上面一種是一樣的,只是在傳參數前,要用新增加一個List將對象包起來。如下:
[1]發送頁面:
//傳遞復雜些的參數 Map<String, Object> map = new HashMap<String, Object>(); map.put("1", "傳值"); map.put("2", "成功"); List<Map<String, Object>> list = new ArrayList<Map<String, Object>>(); list.add(map);Intent intent = new Intent(); intent.setClass(MainActivity.this,SecondActivity.class); Bundle bundle = new Bundle(); //須定義一個list用于在bundle中傳遞需要傳遞的ArrayList<Object>,這個是必須要的 ArrayList bundlelist = new ArrayList(); bundlelist.add(list); bundle.putParcelableArrayList("list",bundlelist); intent.putExtras(bundle); startActivity(intent);
[2]接收頁面:
@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.second);TextView txt = (TextView)this.findViewById(R.id.txt);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); } } txt.setText(sResult); }
3、通過實現Serializable接口(引入java.util.*)
????? 通過將數據序列化后,再將其傳遞出去。
(1)發送頁面:
//通過Serializable接口傳參數的例子 HashMap<String,String> hm = new HashMap<String,String>(); hm.put("1", "傳值"); hm.put("2", "成功"); Bundle bundle = new Bundle(); bundle.putSerializable("serializable", hm); Intent intent = new Intent(); intent.putExtras(bundleSerializable); intent.setClass(MainActivity.this, SecondActivity.class); startActivity(intent);(2)接收頁面:
@Override protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.second);TextView txt = (TextView)this.findViewById(R.id.txt);//接收參數??? Bundle bundle = this.getIntent().getExtras();??
?? //傳HashMap
?? HashMap<String,String> hm =? (HashMap<String,String>)bundle.getSerializable("serializable");?
?? String sResult = "";?
?? Iterator iter = hm.entrySet().iterator();?
?? while(iter.hasNext())?
?? {?
????? Map.Entry entry = (Map.Entry)iter.next();?
????? Object key = entry.getKey();?
????? Object value = entry.getValue();?
????? sResult += (String)key;?
????? sResult += (String)value;???????????
?? }?
txt.setText(sResult); }
4、通過實現Parcelable接口
通過實現Parcelable接口,把要傳的數據打包在里面,然后在接收端自己分解出來。這個是Android獨有的,在其本身的源碼中也用得很多, 效率要比Serializable相對要好。 (1)首先要定義一個類,用于 實現Parcelable接口 實現Parcelable(2)發送頁面
Intent intent = new Intent(); Person p = new Person(); p.mInt = 1; p.mStr = "傳值"; p.mMap = new HashMap<String,String>(); p.mMap.put("key", "value"); intent.putExtra("Parcelable", p); intent.setClass(MainActivity.this, SecondActivity.class); startActivity(intent);(3)接收頁面
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.second);TextView txt = (TextView)this.findViewById(R.id.txt);//接收參數 Intent intent = getIntent(); Person p = intent.getParcelableExtra("Parcelable"); String sResult = " mInt ="+ p.mInt +"\r\n mStr" + p.mStr + "\r\n mMap.size="+p.mMap.size(); txt.setText(sResult); }
5、通過單例模式實現參數傳遞
(1)單例模式的特點就是可以保證系統中一個類有且只有一個實例。這樣很容易就能實現,在A中設置參數,在B中直接訪問了,是效率最高的。 package com.model; import java.util.*;public class XclSingleton {//單例模式實例 private static XclSingleton instance = null; //synchronized 用于線程安全,防止多線程同時創建實例 public synchronized static XclSingleton getInstance(){ if(instance == null){ instance = new XclSingleton(); } return instance; } public final HashMap<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); } } 單例模式(2)發送頁面
XclSingleton.getInstance().put("1", "傳值"); XclSingleton.getInstance().put("2", "成功"); Intent intent = new Intent(); intent.setClass(MainActivity.this, SecondActivity.class); startActivity(intent);(3)接收頁面
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.second);TextView txt = (TextView)this.findViewById(R.id.txt);//接收參數?
????HashMap<String,Object> map = XclSingleton.getInstance().mMap;??????????????????????????
????String sResult = "map.size() ="+map.size();??????
????????
????//遍歷參數?
????Iterator iter = map.entrySet().iterator();?
????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;???????????????
????}? txt.setText(sResult); }
?
轉載于:https://www.cnblogs.com/xinaixia/p/4103216.html
新人創作打卡挑戰賽發博客就能抽獎!定制產品紅包拿不停!總結
以上是生活随笔為你收集整理的Android 之数据传递小结的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: reStructuredText学习
- 下一篇: window-运行perl脚本(搭建he
