[英]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 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.