简体   繁体   English

将对象的数组列表从一个活动发送到另一个活动

[英]Send an Arraylist of an object from one activity to another

I need to duplicate an ArrayList from one activity from another. 我需要从一个活动复制另一个ArrayList This is an ArrayList of an object that I named Dias , and it contains a String and a boolean: 这是我命名为Dias的对象的ArrayList ,它包含一个String和一个布尔值:

Arraylist {Dias}  // Dias contains(String Dias, boolean estado)

And I have to pass this ArrayList to other activity. 而且我必须将此ArrayList传递给其他活动。

My Dias class: 我的Dias课:

public class Dias {

  private String Dia;
  private boolean estado;

//CONSTRUCTOR DE LA CLASE//
  public Dias(String Dia, boolean estado) {
      this.Dia = Dia;
      this.estado = estado;
  }

//GETTERS Y SETTERS DE LA CLASE//

  public String getDia() {
      return Dia;
  }

  public void setDia(String dia) {
      Dia = dia;
  }

  public boolean isChekeado() {
      return estado;
  }

  public void setChekeado(boolean chekeado) {
      estado = chekeado;
  }

} }

My Primary Class: 我的小学班:

 public class Primera extends Activity {
    ArrayList<Dias> dias = new ArrayList<Dias>();
     //OnClick Method
    public void lanzar2(View view){
      dias.add(new Dias("Lu", false));
      dias.add(new Dias("MAr", false));
      Intent i = new Intent();
      Bundle b = new Bundle();
      b.putParcelableArrayList("arreglo", (ArrayList<? extends Parcelable>) dias);
      i.putExtras(b);
      i.setClass(this, ListasActivity.class);
      startActivity(i);
   }

}

How can I send my Arraylist to another activity?, I just don't understand how does it work(parcelable) and also I don't know the syntax to use it. 我如何将我的Arraylist发送到另一个活动?,我只是不了解它是如何工作的(可打包),而且我也不知道使用它的语法。

Thank you! 谢谢!

You have to implement the interface Parcelable in the object Dias. 您必须在对象Dias中实现接口Parcelable。 You can see more about how to do it here: http://developer.android.com/reference/android/os/Parcelable.html 您可以在此处查看有关如何执行操作的更多信息: http : //developer.android.com/reference/android/os/Parcelable.html

Besides that you would send through an Intent that you use to start the other activity the ArrayList using the method: 除此之外,您还将通过以下方法通过Intent发送用于启动ArrayList的其他活动:

)">intent.putParcelableArrayListExtra(nameToSaveAs, yourList) )“> intent.putParcelableArrayListExtra(nameToSaveAs,yourList)

Then you startActivity(intent) and you can receive the data in the other activity with getIntent().getParcelableArrayListExtra(nameToSaveAs); 然后,您启动startActivity(intent)并可以使用getIntent().getParcelableArrayListExtra(nameToSaveAs);在其他活动中接收数据getIntent().getParcelableArrayListExtra(nameToSaveAs); You will probably have to cast it, but that is the basic steps to pass ArrayLists. 您可能必须强制转换它,但这是传递ArrayLists的基本步骤。

Another simple method is to serialize it in your own way. 另一个简单的方法是以您自己的方式对其进行序列化。

For example : 例如

If you have 5 elements in your arraylist like "hello", "how" , "are", "you", "brother" . 如果您的数组列表中有5个元素,例如"hello", "how" , "are", "you", "brother" You can loop through the arraylist and make a string as hello,how,are,you,brother 您可以遍历arraylist并创建一个字符串,如: hello,how,are,you,brother

String serializedString = "";
for(String anElement:arrayList)
    serilaizedString = serializedString + "," +anElement;

and send it to another activity using putExtra method of Intent. 并使用Intent的putExtra方法将其发送到另一个活动。

And in the receiving end, you can split the text using split(",") then you will get an array which you can change to array list again if needed. 在接收端,您可以使用split(“,”)分割文本然后将获得一个数组,可以根据需要再次更改为数组列表。

String[] myArray = recievedString.split(",");
List<String> myList = new List<String>();
for(String anElement:myList)
    myList.add(anElement);

Now you have myList as the array list. 现在,您将myList作为数组列表。

PS Your strings shouldn't contain any comma though. PS您的字符串不应包含任何逗号。 So for cases with comma, you can use something like $ or % or what ever is comfortable for case. 因此,对于带有逗号的情况,可以使用$%类的值,也可以使用适合情况的值。

You can pass it either using Serialization or by using Parcelable interface. 您可以使用序列化或使用Parcelable接口传递它。 Refer to this for Parcelable interface http://developer.android.com/reference/android/os/Parcelable.html . 请参阅此内容以了解Parcelable界面http://developer.android.com/reference/android/os/Parcelable.html

我如何通过 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.

相关问题 如何在android中将Calendar对象从一个活动发送到另一个活动? - How to send a Calendar object from one activity to another in android? 获取空对象-将数据从一个活动发送到另一个活动 - Getting null object - send data from one activity to another 我如何通过 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 将arraylist从一个活动传递到另一个活动的片段 - Passing arraylist from one activity to fragment of another 如何将数组列表从活动发送到另一个活动,而不访问另一个活动 - How to send arraylist from activity to another, without visiting that other activity 如何将ArrayList中的类从一个活动传递给另一个活动 - How to pass Class of Class in ArrayList from one Activity to Another Activity 传递一个 ArrayList<Object> 从一个意图到另一个意图 - Passing an ArrayList<Object> from one Intent to another 如何将 object(在这种情况下为 arraylist)从一个片段发送到 java(Android)中的另一个片段 - How to send an object (an arraylist in this situation) from one fragment to another fragment in java (Android) 将listView数据从一个活动发送到另一个活动 - Send listView data from one activity to another 将图像从一项活动发送到另一项活动 - send image from one activity to another
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM