Bundle类是一个final类:
public final class
Bundle
{
public final class
Bundle
extends Objectimplements Parcelable Cloneable
简介:
bundle类中加入数据以key-value的形式
当取出数据时是根据key的值,找到对应的value取出的
作用:
①两个activity之间的通讯可以通过bundle类来实现
如:
//在Actibityone中的代码
Intent intent=new Intent(this,Activitytwo);
intent.putExtra("Data","这是第一个页面");
//在Activitytwo中的代码
Bundle bundle=this.getIntent().getExtras();
String data=bundle.getString("Data");
</pre><pre name="code" class="java"> 在安卓开发中的小应用
第一个activity:
<pre name="code" class="java">protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
arr_Adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, names);
listview=(ListView)findViewById(R.id.listView1);
listview.setAdapter(arr_Adapter);
listview.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
// TODO Auto-generated method stub
Intent intent=new Intent(this,Information.class);
intent.putExtra("Position",position);<span style="white-space:pre"> </span>//将点击的listview的位置信息附在intent上传递到第二个activity
startActivity(intent);
}
第二的avtivity:
<pre name="code" class="java">protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.information);
imgview=(ImageView)findViewById(R.id.imageView1);
imgbutton=(ImageButton)findViewById(R.id.imageButton1);
Bundle bundle=this.getIntent().getExtras();
int listposition=bundle.getInt("Position");
if(listposition==0) {
<span style="white-space:pre"> </span>//根据listview点击的位置实现不同的功能
<span style="white-space:pre"> </span>}
</pre><pre name="code" class="java">