关于不同的activity之间的传值个人感觉如果两个activity相关的话,使用intent和bundle还是挺多的,如果关联不是很密切的话一般都是用service传值,后文会将四大组建结合起来讲解下,现在先说说bundle,原来博主一般都是直接itntent.putextra();键值对少的时候控制的比较精细,而且感觉也不是很麻烦,但是一旦键值对很多的时候,那就比较可怕了,一个是程序的性能问题,另一便是代码量的问题,不过程序的性能应该优先考虑,好,废话不多说,上代码证明:
这个用的intent自有的传值
for (int i = 0; i < buddhistServicesArray.length; i++) {
BuddhistServicesBean buddhistServicesBeans = new BuddhistServicesBean(bsTitle, bsDetail, bsFirst, bsSecond, bsThird);
String bstitle = buddhistServicesArray[i].getBSTitle();
String bsdetail = buddhistServicesArray[i].getBSDetail();
String bsfirst = buddhistServicesArray[i].getBSFirst();
String bssecond = buddhistServicesArray[i].getBSSecond();
String bsthird = buddhistServicesArray[i].getBSThird();
//博主当时感觉还可以,不是很麻烦,但是多了的话,可想而知,真的让人很头疼,很恶心
Map<String, Object> map = new HashMap<String, Object>();
map.put("bsTitle", bstitle);
map.put("bsDetail", bsdetail);
map.put("bsFirst", bsfirst);
map.put("bsSecond", bssecond);
map.put("bsThird", bsthird);
buddhistServicesList.add(map);
}
Arrays.sort(buddhistServicesArray);
buddhistServicesBeans = Arrays.asList(buddhistServicesArray);
buddhistServicesAdapter = new BuddhistServicesAdapter(getActivity(), buddhistServicesBeans);
buddhistservices_listview.setAdapter(buddhistServicesAdapter);
buddhistservices_listview.setOnItemClickListener(new bsInfoOnItemClickListener());
protected class bsInfoOnItemClickListener implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent it_bs_info = new Intent(getActivity(), BuddhistServicesInfo.class);
for (int i = 0; i <= position; i++) {
if (position == i) {
Map map = (Map) buddhistServicesList.get(i);
String btitle = (String) map.get("bsTitle");
String bdetail = (String) map.get("bsDetail");
String bfirst = (String) map.get("bsFirst");
String bsecond = (String) map.get("bsSecond");
String bthird = (String) map.get("bsThird");
it_bs_info.putExtra("btitle_info", btitle);
it_bs_info.putExtra("bdetail_info", bdetail);
it_bs_info.putExtra("bfirst_info", bfirst);
it_bs_info.putExtra("bsecond_info", bsecond);
it_bs_info.putExtra("bthird_info", bthird);
//传值到下个activity中
}
}
startActivity(it_bs_info);
}
}
这个是下个activity初始化数据的时候从上个activity得到相应的值
private void initData() {
Intent it = this.getIntent();
info_title = it.getStringExtra("btitle_info");
this.bsi_title.setText(info_title);
title = info_title;
String info_detail = it.getStringExtra("bdetail_info");
String info_first = it.getStringExtra("bfirst_info");
String info_second = it.getStringExtra("bsecond_info");
String info_third = it.getStringExtra("bthird_info");
this.bsi_detail.setText(info_detail);
this.bsi_first.setText(info_first);
this.bsi_second.setText(info_second);
this.bsi_third.setText(info_third);
}
好,下面是bundle的事例:
protected class eventInfoOnItemClickListener implements AdapterView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent it_event_info = new Intent(EventsActivity.this, EventInfoActivity.class);
for (int i = 0; i <= position; i++) {
if (position == i) {
Map map = (Map) eventsList.get(i);
Bundle bundle = new Bundle();
bundle.putString("eventimg_info", (String) map.get("eventImg"));
bundle.putString("eventname_info", (String) map.get("eventName"));
bundle.putString("eventtime_info", (String) map.get("eventTime"));
it_event_info.putExtras(bundle);
}
}
startActivity(it_event_info);
}
}
这样的话是不是简单多了,个人认为bundle是对键值对的封装,而且得到和初始化的时候也很容易,以上代码还涉及到了,list,map,和for循环,activity之间传递的时候用的很多,大家可以学习下