哪种方法更适合将大量数据从一个活动传递到另一个活动-Android? [英] Which way is better for pass a large list of data from one activity to another activity - android?

查看:80
本文介绍了哪种方法更适合将大量数据从一个活动传递到另一个活动-Android?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将大量数据从一个 activity 传递到另一个 activity ,哪种方法更好?

I need to pass a large list of data from one activity to another activity,which way is better?

第一种方法(例如):

ArrayList<myModel> myList = new ArrayList<myModel>();
intent.putExtra("mylist", myList);

第二种方式(例如):

ActivityTwo act = new ActivityTwo();
act.getDataMethod(listValues);
Intent i = new Intent(this, ActivityTwo.class);
startActivity(i);

然后在另一个 activity (ActivityTwo)中,我从 getDataMethod 获取数据.

And in another activity(ActivityTwo) I get data from getDataMethod.

推荐答案

在Android中将大型数据列表从一个Activity传递到另一个Activity的最佳方法是Parcelable.您首先创建Parcelable pojo类,然后创建Array-List并将其传递给键和值对之类的bundle,然后将bundle传递给intent extras.

Best way to pass large data list from one Activity to another in Android is Parcelable . You first create Parcelable pojo class and then create Array-List and pass into bundle like key and value pair and then pass bundle into intent extras.

下面,我放一个示例User Pojo类,该类实现了Parcelable接口.

Below I put one sample User Pojo class that implements Parcelable interface.

import android.os.Parcel;
import android.os.Parcelable;
/**
 * Created by CHETAN JOSHI on 2/1/2017.
 */

public class User implements Parcelable {

    private String city;
    private String name;
    private int age;


    public User(String city, String name, int age) {
        super();
        this.city = city;
        this.name = name;
        this.age = age;
    }

    public User(){
        super();
    }

    public String getCity() {
        return city;
    }

    public void setCity(String city) {
        this.city = city;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @SuppressWarnings("unused")
    public User(Parcel in) {
        this();
        readFromParcel(in);
    }

    private void readFromParcel(Parcel in) {
        this.city = in.readString();
        this.name = in.readString();
        this.age = in.readInt();
    }

    public int describeContents() {
        return 0;
    }

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

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


    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(city);
        dest.writeString(name);
        dest.writeInt(age);
    }
}
ArrayList<User> info = new ArrayList<User>();
info .add(new User("kolkata","Jhon",25));
info .add(new User("newyork","smith",26));
info .add(new User("london","kavin",25));
info .add(new User("toranto","meriyan",30));

Intent intent = new Intent(MyActivity.this,NextActivity.class);
Bundle bundle = new Bundle();
bundle.putParcelableArrayList("user_list",info );
intent.putExtras(bundle);`
startActivity(intent );

这篇关于哪种方法更适合将大量数据从一个活动传递到另一个活动-Android?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆