簡體   English   中英

幫助將對象的 ArrayList 傳遞給新活動

[英]Help passing an ArrayList of Objects to a new Activity

我有一個 arraylist 對象。 即ArrayList。

我想把它傳遞給一個新的活動。 我嘗試使用 putParcelableArrayList,但 object 存在問題。 我從變量的創建中刪除了該部分,並且該方法有效,但隨后我得到了 eciplse 抱怨不安全的東西。

如何將此 arrayList 傳遞給新活動

謝謝你的時間

編輯我試過這個:

ArrayList<ObjectName> arraylist = new Arraylist<ObjectName>();
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("arraylist", arraylist);

我收到以下錯誤:

The method putParcelableArrayList(String, ArrayList<? extends Parcelable>) in the type Bundle is not applicable for the arguments (String, ArrayList<ObjectName>)

EDIT2 Object 示例代碼。 我是否需要更改此參數才能使 paracbale 工作?

public class ObjectName {
    private int Value1;
    private int Value2;
    private int Value3;

    public ObjectName (int pValue1, int pValue2, int Value3) {
        Value1 = pValue1;
        Value2 = pValue2;
        Value3 = pValue3;
    }

    // Get Statements for each value below
    public int getValue1() {
        return Value1;
    } 
    // etc

您的 object class 應該實現 parcelable。 下面的代碼應該可以幫助您入門。

    public class ObjectName implements Parcelable {

    // Your existing code

        public ObjectName(Parcel in) {
            super(); 
            readFromParcel(in);
        }

        public static final Parcelable.Creator<ObjectName> CREATOR = new Parcelable.Creator<ObjectName>() {
            public ObjectName createFromParcel(Parcel in) {
                return new ObjectName(in);
            }

            public ObjectName[] newArray(int size) {

                return new ObjectName[size];
            }

        };

        public void readFromParcel(Parcel in) {
          Value1 = in.readInt();
          Value2 = in.readInt();
          Value3 = in.readInt();

        }
        public int describeContents() {
            return 0;
        }

        public void writeToParcel(Parcel dest, int flags) {
            dest.writeInt(Value1);
            dest.writeInt(Value2);  
            dest.writeInt(Value3);
       }
    }

要使用上述內容,請執行以下操作:

在“發送”活動中使用:

ArrayList<ObjectName> arraylist = new Arraylist<ObjectName>();  
Bundle bundle = new Bundle();  
bundle.putParcelableArrayList("arraylist", arraylist);

在“接收”活動中使用:

Bundle extras = getIntent().getExtras();  
ArrayList<ObjectName> arraylist  = extras.getParcelableArrayList("arraylist");  
ObjectName object1 = arrayList[0];

等等。

非常簡單的方法,請嘗試以下方法:

bundle.putSerializable("lstContact", (Serializable) lstObject);
lstObj = (List<Contacts>) bundle.getSerializable("lstContact");

而且您還需要從 Serializable 接口實現您的 Contact class。 前任:

public class Contact implements Serializable{
   ...
   ...
   ...
}

你有兩個選擇:

Android 通過序列化和反序列化(通過 Serializable 或 Parcelable)在 Bundle 中傳遞 Object。 因此,您要在 Bundle 中傳遞的所有對象都必須實現這兩個接口之一。

大多數 Object 使用Serializable輕松序列化,實現Serializable並將 static UID 添加到 class 比編寫Parcelable所需的所有方法/類要簡單得多。

您可以使用 Serializable 接口傳遞您的 arraylist。 (Serialization is the process of converting an object into a stream of bytes in order to store the object or transmit it to memory, a database, or a file.)

public class ObjectName implements Serializable{
    private int Value1;
    private int Value2;
    private int Value3;

    public ObjectName (int pValue1, int pValue2, int Value3) {
        Value1 = pValue1;
        Value2 = pValue2;
        Value3 = pValue3;
    }

    // Get Statements for each value below
    public int getValue1() {
        return Value1;
    } 
    // etc
}

現在在您當前的活動中

// here is the arrayList of yours
ArrayList<ObjectName> objNamesList = new ArrayList<>();
objNamesList.add(new ObjectName(1,1,1)); 
objNamesList.add(new ObjectName(2,2,2)); 
//intialize bundle instance
Bundle b = new Bundle();
//adding data into the bundle 
//key => objNames referece for the arrayList
//value => objNamesList , name of the arrayList 
//cast the arraylist into Serializable 
b.putSerializable("objNames", (Serializable) objNamesList);
//init the intent
Intent i = new Intent(CurrentActivity.this, NextActivity.class);
i.putExtras(b);
startActivity(i);

在下一個活動中,您需要執行以下操作才能獲得 arraylist

public class NextActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_next_activity);
        // get the bundle
        Bundle b = getIntent().getExtras();
        ArrayList<ObjectName> q = (ArrayList<ObjectName>) b.getSerializable("objNames");
    }
}

查看putParcelableArrayList的文檔,您的ObjectName似乎需要擴展“Parcelable”接口才能使其正常工作。 您得到的錯誤似乎也表明了這一點。

我會看這里: http://developer.android.com/reference/android/os/Parcelable.html

1) 確保您要包裹的 object 已正確實現Parcelable

2) 在putParcelableArrayList中使用這個參數:new ArrayList<>(list)

例子:

    protected void onSaveInstanceState(Bundle outstate) {
        super.onSaveInstanceState(outstate);
        outstate.putParcelableArrayList("myObjects", new ArrayList<>(myListOfObjects));
    }

對我有幫助的是:在寫入包裹時設置所有值。 如果任何值為 NULL,我將為該特定屬性設置默認值。 為幾個參數設置了 Null 值,這就是引發此錯誤的原因。

當我不得不這樣做時,我只需將對象的 ArrayList 放入附加包中(它必須是 ArrayList,List 顯然不可序列化)。 那么你所要做的就是確保列表中的對象也是可序列化的。

我在同一個問題上苦苦掙扎,並找到了一種更好的方法,可以將我的陣列放入可以在所有活動中訪問的 Global Class 中。 感謝這篇文章描述了如何使用可以跨多個活動訪問的全局變量。

我的全球 class 是

public class GlobalClass extends Application{ public MyObject[] objArray }

Android 清單更改

android:name=".GlobalClass"

在多個活動中訪問為

GlobalClass g = (GlobalClass)getApplicationContext();
g.objArray[0].x = 100;
int x = g.objArray[0].x;

為了更好地理解,請通過帖子 go。

您可以在某些類中使用public static字段。

有關更多建議,請參見此處

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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