簡體   English   中英

嘗試使用ArrayList創建捆綁 <string> 並通過意圖將其傳遞給另一個活動

[英]Trying to create bundle with ArrayList<string> and pass it to another activity VIA Intent

我希望有人可以幫助我解決這個問題。 我四處張望,並嘗試了許多關於如何將捆綁商品從一項活動轉移到另一項活動的建議。 我嘗試過的大多數建議都仍然無效,我認為以下代碼可以工作,但無效。 我相信當我嘗試在新活動中獲取捆綁商品時,我得到的是空值。 這是代碼:

在我的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);

在我的BlueToothConnThread類中,我這樣做:

 public BlueToothConnThread() {

    Bundle dataBundle = getIntent().getExtras();    
    deviceList = dataBundle.getStringArrayList("DeviceList");

}

在嘗試使用getIntent()。getExtras()初始化dataBundle之后,引發了錯誤。 任何幫助,不勝感激。

謝謝,

羅素

這是您通過的方式。

Intent intent = new Intent(ABC.this, DEF.class);
Bundle bundle = new Bundle();
bundle.putSerializable("ArrayList", yourArrayList);
intent.putExtras(bundle);  

這就是你如何得到它。

Bundle bundle = data.getExtras();
ArrayList<Bluetooth> bluetoothArrayList= (ArrayList<Bluetooth>) bundle.getSerializable("ArrayList");

希望對您有所幫助:)

您無法通過任意方法接收捆綁包。 您可以在Activity的OnCreate方法中獲取捆綁包。 然后只有它起作用。

 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM