YangBinChina
Android开发中Bundle用法包裹数据

Bundle的经典用法,包裹数据放入Intent中,目的在于传输数据。
SDK 里是这样描述: A mapping from String values to various Parcelable types。 它帮助我将数据打包传入intent里面,为使用这些数据提供了便利。 protected void onListItemClick (ListView l, View v, int position, long id) { super.onListItemClick(l, v, position, id); // 获得选中项的 HashMap 对象 HashMap<String,String> map=(HashMap<String,String>)lv.getItemAtPosition(position); String Type=map.get("Type"); Intent i=new Intent(this,title.class); Bundle mBundle=new Bundle(); mBundle.putString("type", Type); i.putExtras(mBundle); startActivity(i); } 代码中 1、实例化Bundle 一个对象,用putString(标记,数据)来将数据导入到Bundle对象中; 2、然后将Bundle对象导入到Intent对象中; 3、Intent启动另一个activity。 从 intent 中读出需要的数据: bundle = getIntent().getExtras(); if(bundle!=null) Type=bundle.getString("type"); if(Type!=null) //从数据库依据所选类型读出文章的Title,保存在cur中 cur=myDBadapter.getTitle(new String[]{Type}); 4、Bundle对象可以从activity.getIntent().getExtras()中返回。可见,启动当前activity 的Intent 对象是由getIntent()来找到的。 5、通过Bundle的getString()方法,就可以读出所要的数据。 这就是Bundle的经典用法,包裹数据放入Intent中,目的在于传输数据。
原文摘自:http://wenku.baidu.com/view/204875e39b89680203d825d0.html###

  

分类:

技术点:

相关文章:

  • 2021-06-13
  • 2022-12-23
  • 2022-01-21
  • 2021-06-30
  • 2022-12-23
  • 2021-09-10
  • 2022-01-15
猜你喜欢
  • 2021-09-10
  • 2021-07-10
  • 2022-02-08
  • 2022-12-23
  • 2021-09-10
  • 2021-12-28
相关资源
相似解决方案