Передача списка объектов из одного действия в другое действие в Android

У меня такая же проблема. Я следил за этой ссылкой , и это сработало. Решение состоит в том, чтобы создать промежуточное программное обеспечение

Добавить файл disable.py в одном из ваших приложений (в моем случае это «myapp»)

class DisableCSRF(object):
    def process_request(self, request):
        setattr(request, '_dont_enforce_csrf_checks', True)

И добавьте среднюю версию MIDDLEWARE_CLASSES

MIDDLEWARE_CLASSES = (
myapp.disable.DisableCSRF,
)

29
задан ישו אוהב אותך 7 December 2017 в 05:53
поделиться

4 ответа

Используйте parcelable . Вот как вы это сделаете:

public class SharedBooking implements Parcelable{

    public int account_id;
    public Double betrag;
    public Double betrag_effected;
    public int taxType;
    public int tax;
    public String postingText;

    public SharedBooking() {
        account_id = 0;
        betrag = 0.0;
        betrag_effected = 0.0;
        taxType = 0;
        tax = 0;
        postingText = "";
    }

    public SharedBooking(Parcel in) {
        account_id = in.readInt();
        betrag = in.readDouble();
        betrag_effected = in.readDouble();
        taxType = in.readInt();
        tax = in.readInt();
        postingText = in.readString();
    }

    public int getAccount_id() {
        return account_id;
    }
    public void setAccount_id(int account_id) {
        this.account_id = account_id;
    }
    public Double getBetrag() {
        return betrag;
    }
    public void setBetrag(Double betrag) {
        this.betrag = betrag;
    }
    public Double getBetrag_effected() {
        return betrag_effected;
    }
    public void setBetrag_effected(Double betrag_effected) {
        this.betrag_effected = betrag_effected;
    }
    public int getTaxType() {
        return taxType;
    }
    public void setTaxType(int taxType) {
        this.taxType = taxType;
    }
    public int getTax() {
        return tax;
    }
    public void setTax(int tax) {
        this.tax = tax;
    }
    public String getPostingText() {
        return postingText;
    }
    public void setPostingText(String postingText) {
        this.postingText = postingText;
    }
    public int describeContents() {
        // TODO Auto-generated method stub
        return 0;
    }
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeInt(account_id);
        dest.writeDouble(betrag);
        dest.writeDouble(betrag_effected);
        dest.writeInt(taxType);
        dest.writeInt(tax);
        dest.writeString(postingText);

    }

    public static final Parcelable.Creator<SharedBooking> CREATOR = new Parcelable.Creator<SharedBooking>()
    {
        public SharedBooking createFromParcel(Parcel in)
        {
            return new SharedBooking(in);
        }
        public SharedBooking[] newArray(int size)
        {
            return new SharedBooking[size];
        }
    };

}

Передача данных:

Intent intent = new Intent(getApplicationContext(),YourActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelable("data", sharedBookingObject);
intent.putExtras(bundle);
startActivity(intent);

Получение данных:

Bundle bundle = getIntent().getExtras();
sharedBookingObject = bundle.getParcelable("data");
49
ответ дан Vineet Shukla 7 December 2017 в 05:53
поделиться

Во-первых, сделайте класс списка реализующим Сериализуемый .

public class Object implements Serializable{}

Затем вы можете просто привести список к (Сериализуемый). Вот так:

List<Object> list = new ArrayList<Object>();
myIntent.putExtra("LIST", (Serializable) list);

И чтобы получить список, который вы делаете:

Intent i = getIntent();
list = (List<Object>) i.getSerializableExtra("LIST");

Вот и все.

57
ответ дан ישו אוהב אותך 7 December 2017 в 05:53
поделиться

Класс объекта посылки

    public class Student implements Parcelable {

        int id;
        String name;

        public Student(int id, String name) {
            this.id = id;
            this.name = name;

        }

        public int getId() {
            return id;
        }

        public String getName() {
            return name;
        }


        @Override
        public int describeContents() {
            // TODO Auto-generated method stub
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int arg1) {
            // TODO Auto-generated method stub
            dest.writeInt(id);
            dest.writeString(name);
        }

        public Student(Parcel in) {
            id = in.readInt();
            name = in.readString();
        }

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

            public Student[] newArray(int size) {
                return new Student[size];
            }
        };
    }

И список

ArrayList<Student> arraylist = new ArrayList<Student>();

Код из Вызова активности

Intent intent = new Intent(this, SecondActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("mylist", arraylist);
intent.putExtras(bundle);       
this.startActivity(intent);

Код по вызываемой активности

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_second);   

    Bundle bundle = getIntent().getExtras();
    ArrayList<Student> arraylist = bundle.getParcelableArrayList("mylist");
}
8
ответ дан Shahin ShamS 7 December 2017 в 05:53
поделиться

Возможно, вы захотите реализовать интерфейс Parcelable в своем классе SharedBooking и добавить их в Intent, то есть с помощью метода putParcelableArrayListExtra . Проверьте документацию здесь:

http://developer.android.com/reference/android/content/Intent.html#putParcelableArrayListExtra%28java.lang.String,%20java.util.ArrayList%3C ?% 20extends% 20android.os.Parcelable% 3E% 29

1
ответ дан Hans Hohenfeld 7 December 2017 в 05:53
поделиться
Другие вопросы по тегам:

Похожие вопросы: