簡體   English   中英

如何傳遞ArrayList <File> 對象從一個活動到另一個活動使用意圖?

[英]How to pass ArrayList<File> object from one Activity to another Activity using Intent?

我有一個ArrayList文檔,我想將此文件發送到另一個Activity。 但是如何在SecondActivity中獲得此值

我嘗試的是首次活動

 private ArrayList<File> documents;
Intent selectLabInt = new Intent(FirstActivity.this, SecondActivity.class);

 selectLabInt.putExtra("patient_name", patientNameStr);
 selectLabInt.putExtra("referal_notes", referalNotesStr);
 selectLabInt.putExtra("fileList",documents);

 startActivity(selectLabInt);

如果您的File可序列化,則可以像這樣傳遞ArrayList

在發送活動中:

ArrayList<File> documents= new ArrayList<File>();
intent.putExtra("documents", documents);

在接收活動中:

ArrayList<File> documents = (ArrayList<File>)getIntent().getSerializableExtra("documents");

您需要使File類可拆分,以便可以通過putParcelable故意傳遞該類的arraylist。

這是相同的https://stackoverflow.com/a/6681784/942224的示例

我如何通過 ArrayList<object> 從一個活動到另一個活動<div id="text_translate"><p>我想將 Object 的 ArrayList 從一個活動傳遞到另一個活動。</p><p> 這是示例代碼:</p><pre> { ArrayList&lt;object&gt; list=new ArrayList&lt;&gt;(); Object value[]=new Object[m.size()]; int n=0; value[]=new Object[n]; value[n]=data.getName("name"); value[n]=data.getLocation("location"); list.add(value[n]); n++; //Since there are "n" number of object } //here the value a which is ArrayList Object // which I want to pass it from here to another activity.</pre><p> 那么現在我將如何將 object 從這里傳遞給另一個活動。 <strong>但是</strong>,我可以使用<strong>Gson</strong>獲得值。 通過將值轉換為<strong>json</strong>並將其作為字符串傳遞給<strong>SharedPreference</strong>並從另一個活動中檢索,然后使用<strong>類型</strong>從<strong>Json</strong>轉換回來以支持其原始形式。</p><p> 我想知道是否可以使用<strong>Parceable</strong>傳遞值。 因為我在使用它時得到了 null 值。</p><p> 這是我試圖使我的 Object 變量 Parceable 的代碼:</p><pre> public class ProductList implements Parcelable { private String NAME; private String LOCATION; public ProductList() { } protected ProductList(Parcel in) { LOCATION= in.readString(); NAME= in.readString(); } public static final Creator&lt;ProductList&gt; CREATOR = new Creator&lt;ProductList&gt;() { @Override public ProductList createFromParcel(Parcel in) { return new ProductList(in); } @Override public ProductList[] newArray(int size) { return new ProductList[size]; } }; public String getNAME() { return NAME; } public void setNAME(String NAME) { this.NAME= NAME; } public String getLOCATION() { return LOCATION; } public void setITEM_IMAGE(String LOCATION) { this.LOCATION= LOCATION; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(NAME); dest.writeString(LOCATION); } }</pre></div></object>

[英]How do I pass ArrayList<Object> from one Activity to another Activity

暫無
暫無

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

相關問題 如何在不使用Intent的情況下將對象從一個活動傳遞到另一個活動 如何使用 android 中的 Intent 將 ArrayList 個對象從一個活動傳遞到另一個活動? 如何將mp3文件從一種活動傳遞到另一種意圖 將 Intent 從一個活動傳遞到另一個活動 如何使用意圖將ArrayList傳遞給另一個Activity? 如何將ArrayList中的類從一個活動傳遞給另一個活動 將數組意圖從一個活動傳遞到另一個活動 我如何通過 ArrayList<object> 從一個活動到另一個活動<div id="text_translate"><p>我想將 Object 的 ArrayList 從一個活動傳遞到另一個活動。</p><p> 這是示例代碼:</p><pre> { ArrayList&lt;object&gt; list=new ArrayList&lt;&gt;(); Object value[]=new Object[m.size()]; int n=0; value[]=new Object[n]; value[n]=data.getName("name"); value[n]=data.getLocation("location"); list.add(value[n]); n++; //Since there are "n" number of object } //here the value a which is ArrayList Object // which I want to pass it from here to another activity.</pre><p> 那么現在我將如何將 object 從這里傳遞給另一個活動。 <strong>但是</strong>,我可以使用<strong>Gson</strong>獲得值。 通過將值轉換為<strong>json</strong>並將其作為字符串傳遞給<strong>SharedPreference</strong>並從另一個活動中檢索,然后使用<strong>類型</strong>從<strong>Json</strong>轉換回來以支持其原始形式。</p><p> 我想知道是否可以使用<strong>Parceable</strong>傳遞值。 因為我在使用它時得到了 null 值。</p><p> 這是我試圖使我的 Object 變量 Parceable 的代碼:</p><pre> public class ProductList implements Parcelable { private String NAME; private String LOCATION; public ProductList() { } protected ProductList(Parcel in) { LOCATION= in.readString(); NAME= in.readString(); } public static final Creator&lt;ProductList&gt; CREATOR = new Creator&lt;ProductList&gt;() { @Override public ProductList createFromParcel(Parcel in) { return new ProductList(in); } @Override public ProductList[] newArray(int size) { return new ProductList[size]; } }; public String getNAME() { return NAME; } public void setNAME(String NAME) { this.NAME= NAME; } public String getLOCATION() { return LOCATION; } public void setITEM_IMAGE(String LOCATION) { this.LOCATION= LOCATION; } @Override public int describeContents() { return 0; } @Override public void writeToParcel(Parcel dest, int flags) { dest.writeString(NAME); dest.writeString(LOCATION); } }</pre></div></object> 如何使用Xamarin在Android中通過意圖將列表從一個活動傳遞到另一個活動 使用Intent將對象從一個活動傳遞到另一個活動時會出現什么問題
 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM