[英]Trying to create bundle with ArrayList<string> and pass it to another activity VIA Intent
I hope someone can help me with the question. 我希望有人可以帮助我解决这个问题。 I have looked all over and tried many suggestions as to how to get bundles from one activity to another. 我四处张望,并尝试了许多关于如何将捆绑商品从一项活动转移到另一项活动的建议。 Most suggestions I have tried and they still do not work, I thought the following code would work but it doesn't. 我尝试过的大多数建议都仍然无效,我认为以下代码可以工作,但无效。 I believe I am getting null when I try to get the bundle in the new activity. 我相信当我尝试在新活动中获取捆绑商品时,我得到的是空值。 Here is the code: 这是代码:
In my SyncActivity Class I have this: 在我的SyncActivity类中,我有以下内容:
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
// If there are paired devices
if (pairedDevices.size() > 0) {
// Loop through paired devices
for (BluetoothDevice device : pairedDevices) {
// Add the name and address to an array adapter to show in a ListView
deviceList.add(device.getName());
deviceList.add(device.getAddress());
deviceList.add(device.getUuids().toString());
}
}
Intent myIntent = new Intent(SyncActivity.this, BlueToothConnThread.class);
Bundle myBundle = new Bundle();
myBundle.putStringArrayList("DeviceList", deviceList);
myIntent.putExtras(myBundle);
startActivity(myIntent);
In my BlueToothConnThread Class I do this: 在我的BlueToothConnThread类中,我这样做:
public BlueToothConnThread() {
Bundle dataBundle = getIntent().getExtras();
deviceList = dataBundle.getStringArrayList("DeviceList");
} }
The error is thrown after I try to initialize dataBundle with the getIntent().getExtras(); 在尝试使用getIntent()。getExtras()初始化dataBundle之后,引发了错误。 Any help greatly appreciated. 任何帮助,不胜感激。
Thanks, 谢谢,
Russell 罗素
Here is how you pass it. 这是您通过的方式。
Intent intent = new Intent(ABC.this, DEF.class);
Bundle bundle = new Bundle();
bundle.putSerializable("ArrayList", yourArrayList);
intent.putExtras(bundle);
And here is how you get it. 这就是你如何得到它。
Bundle bundle = data.getExtras();
ArrayList<Bluetooth> bluetoothArrayList= (ArrayList<Bluetooth>) bundle.getSerializable("ArrayList");
I hope it helps :) 希望对您有所帮助:)
You cannot receive Bundle in some arbitrary method. 您无法通过任意方法接收捆绑包。 You can get the bundle in OnCreate method of the Activity. 您可以在Activity的OnCreate方法中获取捆绑包。 Then only it works. 然后只有它起作用。
public BlueToothConnThread extends Activity {
@Override
public void onCreate(Bundle onSavedInstanceState) {
//Some code
Bundle dataBundle = getIntent().getExtras();
deviceList = dataBundle.getStringArrayList("DeviceList");
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.