数据 通过意图进行交互 这是一个key_value 方法
首先Serilizable是一个可序列化对象接口
在运用这个putSerializable()时 ,可以先创建一个类,该类实现了Serilizable接口 如下:
public class Person implements Serializable{
private Integer id;
private String name;
private String passwd;
private String gender;
public Person(String name, String passwd, String gender) {
this.name = name;
this.passwd = passwd;
this.gender = gender;
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPasswd() {
return passwd;
}
public void setPasswd(String passwd) {
this.passwd = passwd;
}
public String getGender() {
return gender;
}
public void setGender(String gender) {
this.gender = gender;
}
}
随后在传递Bundle的时候
EditText name = (EditText) findViewById(R.id.name); EditText security = (EditText) findViewById(R.id.security); RadioButton male = (RadioButton) findViewById(R.id.male); String gender = male.isChecked() ? "男" :"女";
//创建Person对象 Person p = new Person(gender,name.getText().toString(),security.getText().toString()); Bundle bundle = new Bundle();
//传入Person对象 bundle.putSerializable("person",p);
Intent intent =new Intent(MainActivity.this,ResultActivity.class);
//putExtras(Bundle bundle) putExtra(key-values) 方法是直接加入键值对 ,内容和Bundle存入的键值对是一样的
intent.putExtras(bundle); startActivity(intent);
传入的那个Activity中
Intent intent =getIntent();
//intent接收的进来的是Person这个实现了Serializable接口的类 Person p = (Person) intent.getSerializableExtra("person"); TextView name = (TextView) findViewById(R.id.resultName); TextView security = (TextView) findViewById(R.id.resultSecurity); TextView sex = (TextView) findViewById(R.id.resultMale); name.setText("您的用户名是:"+p.getName()); security.setText("您的密码是:"+p.getSecurity()); sex.setText("您的性别是:"+p.getSex());
早期使用的例子
//findAll
public ArrayList<HashMap<String, Object>> findAll(){//查询所有
Cursor c;
c = db.rawQuery("select g.ID,g.GoodsID,g.GoodsName,g.SellerID,g.Price,g.Specification,g.Unit,s.ID,s.SellerExtraName from T_Goods as g inner join T_Seller as s on g.SellerID = s.ID", null);
TGoods gods=new TGoods();
ArrayList<HashMap<String, Object>> arrayList=new ArrayList<HashMap<String,Object>>();
while(c.moveToNext()){
HashMap<String,Object> map=new HashMap<String, Object>();
map.put("ID", c.getInt(c.getColumnIndex("ID")));
map.put("GoodsID", c.getString(c.getColumnIndex("GoodsID")));
map.put("GoodsName", c.getString(c.getColumnIndex("GoodsName")));
map.put("SellerExtraName", c.getString(c.getColumnIndex("SellerExtraName")));
map.put("Price", c.getFloat(c.getColumnIndex("Price")));
map.put("Specification", c.getString(c.getColumnIndex("Specification")));
map.put("Unit", c.getString(c.getColumnIndex("Unit")));
arrayList.add(map);
}
Bundle bundle=new Bundle();
bundle.putSerializable("arrayList", arrayList);
Intent intent=new Intent(UpdateActivity.this, SelTGoodsActivity.class);
intent.putExtras(bundle);
startActivity(intent);
return arrayList;
}
这个是上一个SelActivity的查询页面的代码:
//获取intent中携带的数据
Intent intent = new Intent();
Bundle bundle = intent.getExtras();
//从Bundle数据包取出数据 ,强转
ArrayList<HashMap<String, Object>> arrayList= (ArrayList<HashMap<String,Object>>) bundle.getSerializable("arraylist");
SimpleAdapter adapter=new SimpleAdapter(
this,
arrayList,
R.layout.selectlist,
new String[]{"GoodsID","GoodsName","SellerExtraName","Price","Specification","Unit"},
new int[]{R.id.goodsId,R.id.goodsName,R.id.prodctorName,R.id.price,R.id.guige,R.id.danwei});
selList.setAdapter(adapter);