当前位置:首页 > Android > 正文内容

Android开发中Bundle用法 包裹数据

jsc12年前 (2014-08-27)Android3958

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中,目的在于传输数据。


扫描二维码推送至手机访问。

版权声明:本文由微小站发布,如需转载请注明出处。

本文链接:https://www.jsc0.com/post/14.html

标签: bundle
分享给朋友:

“Android开发中Bundle用法 包裹数据” 的相关文章

Android基本功:支持GPS的核心API

一、LocationManager类        作用和TelephonyManager,AudioManager等服务类的作用类似,所有GPS定位相关的服务、对象都由该对象产生;    ...

Android ImageView的scaleType属性与adjustViewBounds属性

Android ImageView的scaleType属性与adjustViewBounds属性

ImageView的scaleType的属性有好几种,分别是matrix(默认)、center、centerCrop、centerInside、fitCenter、fitEnd、fitStart、fitXY android:sca...

制作一款Android APK管理器主要代码

Android APK管理器代码,主要就是两个列表,一个显示SD卡上面的APK文件的list,一个显示已经安装的app的list。1:获取SD卡上的APK安装文件后,要用代码读出APK里面的信息,如icon等,的主要代码如下:private void getUninatllApk...

Android开发之资源文件存储

在android开发中,资源文件是我们使用频率最高的,无论是string,drawable,还是layout,这些资源都是我们经常使用到的,而且为我们的开始提供很多方便,不过我们平时接触的资源目录一般都是下面这三个。        /res...

Android利用JSON发送数据到服务器

new Thread() { @Override public void run() { // TODO Auto-generated method stub Looper.prepare();&nbs...

Location服务之LocationManager

Location服务之LocationManager

LocationManager系统服务是位置服务的核心组件,它提供了一系列方法来处理与位置相关的问题,包括查询上一个已知位置、注册和注销来 自某个LocationProvider的周期性的位置更新、注册和注销接近某个坐标时对一个已定义的Intent的触发等。今天我们就一起探讨一下 L...