设为首页 加入收藏

TOP

Android开发中Bundle用法 包裹数据
2014-11-24 13:00:17 来源: 作者: 【 】 浏览:1
Tags:Android 开发 Bundle 用法 包裹 数据

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 map=(HashMap)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中,目的在于传输数据。


】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇Android开发:为应用程序自定义Co.. 下一篇Android开发实现HttpClient工具类

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·python数据分析岗的 (2025-12-25 10:02:21)
·python做数据分析需 (2025-12-25 10:02:19)
·成为一个优秀的pytho (2025-12-25 10:02:16)
·Java后端面试实习自 (2025-12-25 09:24:21)
·Java LTS版本有哪些 (2025-12-25 09:24:18)