菜鸟求助:安卓页面跳转数据储存的问题

qq_37354250 2018-09-10 08:48:52
现将AActivity中Listview的Item的内容传至BActivity,返回AActivity中再次传递另一个Item的内容,如何从B返回A再次传递给B的过程中第一次传过去的数据依旧存在呢?
传递item使用的是简单的intent:
intent.setClass(TestFragment2.this.getActivity(), TestFragment2SecondActivity.class);
intent.putExtra("dish_img", dishList.get(position).getDish_picture());
intent.putExtra("dish_name", dishList.get(position).getDish_name());
intent.putExtra("dish_price", dishList.get(position).getDish_price());
intent.putExtra("dish_time", dishList.get(position).getDish_time());
intent.putExtra("dish_window", dishList.get(position).getDish_window());
intent.putExtra("dish_renqi", renqi);
startActivityForResult(intent,123);

接受数据:
Intent intent=getIntent();//getIntent将该项目中包含的原始intent检索出来,将检索出来的intent赋值给一个Intent类型的变量intent
Bundle bundle=intent.getExtras();//.getExtras()得到intent所附带的额外数据
dish.setDish_picture(bundle.getInt("dish_img"));
dish.setDish_name(bundle.getString("dish_name"));
dish.setDish_price(bundle.getString("dish_price"));
dish.setDish_time(bundle.getString("dish_time"));
dish.setDish_window(bundle.getString("dish_window"));
dish.setDish_renqi(bundle.getInt("dish_renqi"));

dishList.add(dish);

yudinglistView.setAdapter(adapter);

麻烦各位大佬帮忙看一下
...全文
973 10 打赏 收藏 转发到动态 举报
写回复
用AI写文章
10 条回复
切换为时间正序
请发表友善的回复…
发表回复
qq_31389927 2018-09-13
  • 打赏
  • 举报
回复
6楼正解~~~~~~~~
lfl521kfc 2018-09-12
  • 打赏
  • 举报
回复
首先确定从B返回A时,A有没有重新创建:
若重新创建,则A传递给B的数据需要保存在全局变量或sharedpreferences里,第二次传递时再取出来赋值给intent;
若不会重新创建,则只需要在A里保存一个私有变量存储A传递给B的数据,第二次传递时再赋值给intent。
雕·不懒惰 2018-09-12
  • 打赏
  • 举报
回复
引用 6 楼 qq_37354250 的回复:
[quote=引用 5 楼 qq_33451426 的回复:]
你要实现的是第一次穿过去的数据,再第二次开启B的时候第一次的数据还在是吧 ,你在B回A的时候,在B把第一次的数据SharedPerefence保存到本地就行了,第二次再过来的时候读取第一次的数据,加上新传来的数据展示在列表
你好,你说的思路我懂了,但是如何将取出的数据加入到list里面呢?我现在的想法是设一个int,每次打开B都++,然后:

listSize = sp.getInt("size", 0);
for(int i = 0; i < listSize; i++){
dishList.get(i).setDish_picture(sp.getInt("dish_img", 0));
dishList.get(i).setDish_name(sp.getString("dish_name", "null"));
dishList.get(i).setDish_name(sp.getString("dish_price", "null"));
dishList.get(i).setDish_name(sp.getString("dish_time", "null"));
dishList.get(i).setDish_name(sp.getString("dish_window", "null"));
}

不知道这个思路对不对(我尝试失败了),或者有什么更好的思路。如果大佬方便的话麻烦详细解答一下。谢谢大佬[/quote]

B中之前的数据又没有保存,你传个int什么么用都没有,你再B回A的时候把dishList用Gson转字符串用SharePerefence保存到本地,再从A到B的时候,在B中读取保存的字符串,Gson转dishList,再执行接收数据
Sevvvvv 2018-09-12
  • 打赏
  • 举报
回复
第一次intent过去的数据 在第二次传的过程中就会被重新赋值在传递,如果你想保留用户操作数据,需要做个缓存。更具需求 是否清空。
qq_37354250 2018-09-11
  • 打赏
  • 举报
回复
引用 5 楼 qq_33451426 的回复:
你要实现的是第一次穿过去的数据,再第二次开启B的时候第一次的数据还在是吧 ,你在B回A的时候,在B把第一次的数据SharedPerefence保存到本地就行了,第二次再过来的时候读取第一次的数据,加上新传来的数据展示在列表
你好,你说的思路我懂了,但是如何将取出的数据加入到list里面呢?我现在的想法是设一个int,每次打开B都++,然后:

listSize = sp.getInt("size", 0);
for(int i = 0; i < listSize; i++){
dishList.get(i).setDish_picture(sp.getInt("dish_img", 0));
dishList.get(i).setDish_name(sp.getString("dish_name", "null"));
dishList.get(i).setDish_name(sp.getString("dish_price", "null"));
dishList.get(i).setDish_name(sp.getString("dish_time", "null"));
dishList.get(i).setDish_name(sp.getString("dish_window", "null"));
}

不知道这个思路对不对(我尝试失败了),或者有什么更好的思路。如果大佬方便的话麻烦详细解答一下。谢谢大佬
zhang106209 2018-09-11
  • 打赏
  • 举报
回复
在onActivityResult中记录第一次传的数据,然后将两次的数据都传过去不就行啦
verejava 2018-09-11
  • 打赏
  • 举报
回复
Android 之 Activity Intent跳转和Bundle传值

http://www.verejava.com/?id=17458213555026
雕·不懒惰 2018-09-11
  • 打赏
  • 举报
回复
你要实现的是第一次穿过去的数据,再第二次开启B的时候第一次的数据还在是吧 ,你在B回A的时候,在B把第一次的数据SharedPerefence保存到本地就行了,第二次再过来的时候读取第一次的数据,加上新传来的数据展示在列表
zhang106209 2018-09-11
  • 打赏
  • 举报
回复
A中不是有数据吗?你只要用一个值记录一下上次穿的数据就可以了,第一次为空就可以了,第二次就在onActivityResult中设置值
qq_37354250 2018-09-11
  • 打赏
  • 举报
回复
引用 2 楼 zhang106209 的回复:
在onActivityResult中记录第一次传的数据,然后将两次的数据都传过去不就行啦
请问你的意思是说在B里面把A传过来的数据再传回A 然后再从A将两次的数据一起传回B吗

80,355

社区成员

发帖
与我相关
我的任务
社区描述
移动平台 Android
androidandroid-studioandroidx 技术论坛(原bbs)
社区管理员
  • Android
  • yechaoa
  • 失落夏天
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧