如何从另一个活动中获取ArrayList?

时间:2018-05-05 12:13:51

标签: android

我需要在另一个活动中创建并使用ArrayList,因此我可以在另一个活动中从中获取数据。该数组是一个对象,其中包含3个字符串。怎么做?

4 个答案:

答案 0 :(得分:0)

您可以将您的Arraylist用于开始活动的意图。但请确保pojo实现SerializableParcelable

答案 1 :(得分:0)

请尝试以下代码解决您的问题可能会有所帮助:

您正在创建双向Parcelable和Serialization。我创建了Parcelable。

UserBean:

public class User implements Parcelable {

    String id,firstname,lastname;

    public User(String id, String firstname, String lastname) {
        this.id = id;
        this.firstname = firstname;
        this.lastname = lastname;
    }

    protected User(Parcel in) {
        id = in.readString();
        firstname = in.readString();
        lastname = in.readString();
    }

    public static final Creator<User> CREATOR = new Creator<User>() {
        @Override
        public User createFromParcel(Parcel in) {
            return new User(in);
        }

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

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getFirstname() {
        return firstname;
    }

    public void setFirstname(String firstname) {
        this.firstname = firstname;
    }

    public String getLastname() {
        return lastname;
    }

    public void setLastname(String lastname) {
        this.lastname = lastname;
    }

    @Override
    public int describeContents() {
        return 0;
    }

    @Override
    public void writeToParcel(Parcel dest, int flags) {
        dest.writeString(id);
        dest.writeString(firstname);
        dest.writeString(lastname);
    }
}

MainActivity:

public class MainActivity extends AppCompatActivity {

    private ArrayList<User> userArrayList;
    private Button btnUserData;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ButterKnife.bind(this);

        userArrayList = new ArrayList<User>();
        btnUserData = (Button)findViewById(R.id.btnUserData);

        btnUserData.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                User userOne = new User("1","Kaushal","Gosaliya");
                User userTwo = new User("2","Viral","Shah");
                User userThree = new User("3","Shalin","Gosaliya");

                userArrayList.add(userOne);
                userArrayList.add(userTwo);
                userArrayList.add(userThree);

                startActivity(new Intent(MainActivity.this,UserActivity.class).putExtra("userdata",userArrayList));

            }
        });
    }

}

MainActivity XML:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    xmlns:android="http://schemas.android.com/apk/res/android" >

    <Button
        android:id="@+id/btnUserData"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
</LinearLayout>

UserActivity:

public class UserActivity extends AppCompatActivity {

    ArrayList<User>  userArrayList;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_user);

        if(getIntent().hasExtra("userdata")){
            userArrayList = getIntent().getParcelableArrayListExtra("userdata");
        }

        if(userArrayList != null){
            for(User user :userArrayList)
               Toast.makeText(this, " Id: "+ user.getId() + " First Name: " + user.getFirstname() + " Last Name:" + user.getLastname(), Toast.LENGTH_SHORT).show();
        }
    }
}

答案 2 :(得分:0)

您可以将ArrayList定义为静态,以便可以从任何类或活动访问它。像这样:
public static ArrayList<String> = new ArrayList<>();

答案 3 :(得分:0)

根据您使用数据的方式处理它的方式:

  • 如果您的列表已创建并主要用于Activity1,则Activity2仅使用它一段时间,然后完成再次返回Activity1。然后,您可以使用yourIntent.putParcelableArrayListExtra
  • 如果您的列表在项目的全球许多地方使用,则应该static而不是减少许多parcel / unparcel进程