gpt4 book ai didi

android - 将包发送到另一个 Activity

转载 作者:太空狗 更新时间:2023-10-29 16:23:41 24 4
gpt4 key购买 nike

我正在尝试将一个包从一个 Activity 发送到另一个 Activity 。当我在接收 Activity 中加载包时,所有信息似乎都是空的。这是一些代码:

Activity A(发送包):

public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
Intent intent = new Intent(HotelsActivity.this, EditHotelActivity.class);
Bundle b = new Bundle();
b = toBundle(app.getHotelList().get(position));
intent.putExtra("Hotel Bundle", b);
startActivity(intent);
}

});

toBundle 方法只是将对象中的字符串添加到包中。我已将日志记录语句放入此方法中,并且包肯定不为空。

Activity B(加载包):

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView (R.layout.edit_hotel);
setTitleFromActivityLabel (R.id.title_text);

// Retrieve sent bundle
Bundle b = this.getIntent().getExtras();
String hotelName = b.getString("hotelname");
if (hotelName == null)
MyLog.i(TAG, "IT IS NULL");
}

loggin 语句然后打印“IT IS NULL”,因为出于某种原因 hotelName 为 null,但这肯定是正确的键。

有人可以帮忙吗?

toBundle 方法:

public Bundle toBundle(HotelItem hotel) {
Bundle b = new Bundle();
b.putString("hotelname",hotel.getHotelName());
b.putString("hotel address", hotel.getHotelAddress());
b.putString("hotel telephone", hotel.getHotelTelephone());
b.putString("hotel website", hotel.getHotelWebsite());


return b;
}

最佳答案

Bundle b = new Bundle();
b = toBundle(app.getHotelList().get(position));
intent.putExtras(b);
startActivity(intent);




//In your next activity
//Create another Bundle object and get the string by name

Bundle bundle = getIntent().getExtras();
if(bundle!=null){
String hotelName = bundle.getString("hotelname");
}

关于android - 将包发送到另一个 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7815660/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com