Klasa Android Parcelable z ArrayList
Mam projekt na Androida, w którym mam zajęcia. W tej klasie jest ArrayList<Choices>
. Będę dostawał jakiś XML, analizował go, a następnie robił z niego obiekty, które będę przekazywał do innej aktywności. Wybieram Parcelable.
import java.util.ArrayList;
import android.os.Parcel;
import android.os.Parcelable;
import android.support.v4.os.ParcelableCompat;
public class Question implements Parcelable{
String id;
String text;
String image;
ArrayList<Choices> CHOICES;
public Question(String id, String text, String image) {
super();
this.id = id;
this.text = text;
this.image = image;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public String getImage() {
return image;
}
public void setImage(String image) {
this.image = image;
}
@Override
public String toString() {
return "Question [id=" + id + ", text=" + text + ", image=" + image
+ "]";
}
// Answer Choices class
class Choices {
boolean isCorrect;
String choice;
public Choices(boolean isCorrect, String choice) {
this.isCorrect = isCorrect;
this.choice = choice;
}
public String getChoice() {
return choice;
}
public boolean getIsCorrect() {
return isCorrect;
}
@Override
public String toString() {
return "Choices [isCorrect=" + isCorrect + ", choice=" + choice
+ "]";
}
}
public static final Parcelable.Creator<Question> CREATOR = new Parcelable.Creator<Question>() {
@Override
public Question createFromParcel(Parcel in) {
return new Question(in);
}
@Override
public Question[] newArray(int size) {
return new Question[size];
}
};
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(text);
dest.writeString(image);
dest.writeList(CHOICES);
}
private Question(Parcel in) {
this.id = in.readString();
this.text = in.readString();
this.image = in.readString();
this.CHOICES = in.readArrayList(Choices.class.getClassLoader());
}
}
Dzięki za pomoc!
4 answers
Jeśli trzeba przejść ArrayList
pomiędzy działaniami, to ja też bym poszedł z implementacją Parcelable
, bo chyba nie ma innego wyjścia.
Jednak nie sądzę, że będziesz potrzebował tyle getterów i seterów. Oto twoja Question
klasa, która implementuje Parcelable
:
public class Question implements Parcelable {
public String id;
public String text;
public String image;
public ArrayList<Choice> choices;
/**
* Constructs a Question from values
*/
public Question (String id, String text, String image, ArrayList<Choice> choices) {
this.id = id;
this.text = text;
this.image = image;
this.choices = choices;
}
/**
* Constructs a Question from a Parcel
* @param parcel Source Parcel
*/
public Question (Parcel parcel) {
this.id = parcel.readString();
this.text = parcel.readString();
this.image = parcel.readString();
this.choices = parcel.readArrayList(null);
}
@Override
public int describeContents() {
return 0;
}
// Required method to write to Parcel
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(id);
dest.writeString(text);
dest.writeString(image);
dest.writeList(choices);
}
// Method to recreate a Question from a Parcel
public static Creator<Question> CREATOR = new Creator<Question>() {
@Override
public Question createFromParcel(Parcel source) {
return new Question(source);
}
@Override
public Question[] newArray(int size) {
return new Question[size];
}
};
}
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-17 06:12:51
Masz to prawie, ale nie do końca, prawda. Klasa Question wygląda prawie poprawnie. Jedyną rzeczą, która nie zadziała, jest parcelacja wachlarza wyborów.
Można to zrobić na dwa sposoby:
- Dokonuj Wyborów Parcelacyjnych. Będziesz musiał dodać wszystkie wymagane metody i twórcę. Ponieważ Android wie, jak parcel ArrayLists Parcelables, to zadziała.
- Uczyń parcelację tablicy wyborów częścią parcelacji pytania. Do zrobienia to, prawdopodobnie wciśniesz rozmiar tablicy do paczki, a następnie zapętlisz wybory, przesuwając ich wartości. Z drugiej strony, najpierw odczytałeś licznik, a następnie odczytałeś wartości dla każdego wyboru, tworząc każdy z nich i wciskając go do nowego pytania.
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-03-17 03:52:40
Użycie:
in.createTypedArrayList(Product.CREATOR)
W konstruktorze, który przyjmuje obiekt przypowieści jako param.
W metodzie writeToParcel użyj dest.writeTypedList(product);
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-12-18 02:31:40
Utwórz nowy plik java dla "Choices" i zaimplementuj "Parcelable". Jeśli nie zaimplementujesz parcelable, otrzymasz wyjątek run-time (Nie można go zaimplementować). Więc użyj poniższego kodu:
public class Choices implements Parcelable{
boolean isCorrect;
String choice;
public Choices(boolean isCorrect, String choice) {
this.isCorrect = isCorrect;
this.choice = choice;
}
//Create getters and setters
protected Choices(Parcel in) {
isCorrect = in.readByte() != 0;
choice = in.readString();
}
public static final Creator<Choices> CREATOR = new Creator<Choices>() {
@Override
public Choices createFromParcel(Parcel in) {
return new Choices(in);
}
@Override
public Choices[] newArray(int size) {
return new Choices[size];
}
};
@Override
public String toString() {
return "Choices [isCorrect=" + isCorrect + ", choice=" + choice
+ "]";
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeByte((byte) (isCorrect ? 1 : 0));
dest.writeString(choice);
}
}
Jak wspomniano w powyższej odpowiedzi przez @G. Blake trzeba dokonać wyborów Parcelable i Android wie, jak parcel ArrayLists Parcelables
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-08-28 00:19:27