将模型对象传递给另一个Activity

use*_*557 3 android android-intent android-layout android-fragments android-activity

我有一个RecyclerView利用Recycler Adapter来输出列表布局,如下所示:

http://i.imgur.com/ORkXXTb.png

我需要将下面的模型附加到每个列表项,这样,如果用户单击列表项中的任何元素(如圆圈或两个TextView之一),它会将模型对象传递给下一个Activity.

这是User模型:

public class User {

    private String id;
    private String username;
    private String displayName;
    private Object deletedAt;
    private Statistic stat;

    public String getId() {
        return id;
    }

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

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username= username;
    }

    public String getDisplayName() {
        return displayName;
    }

    public void setDisplayName(String displayName) {
        this.displayName = displayName;
    }

    public Object getDeletedAt() {
        return deletedAt;
    }

    public void setDeletedAt(Object deletedAt) {
        this.deletedAt = deletedAt;
    }

    public Statistic getStat() {
        return stat;
    }

    public void setStat(Statistic stat) {
        this.stat = stat;
    }

}
Run Code Online (Sandbox Code Playgroud)

这是每个列表项(user_layout.xml)的布局:

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/user_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <RelativeLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@color/white">

            <ImageView
                android:id="@+id/avatar"
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:layout_alignParentLeft="true"
                android:background="@drawable/avatar" />


            <TextView
                android:id="@+id/display_name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignTop="@+id/avatar"
                android:layout_toRightOf="@+id/avatar"
                />

            <TextView
                android:id="@+id/username"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_below="@+id/display_name"
                />

        </RelativeLayout>

    </LinearLayout>

</LinearLayout>
Run Code Online (Sandbox Code Playgroud)

以下是UserRecyclerAdapter用于夸大上面布局的内容:

public class UserRecyclerAdapter extends RecyclerView.Adapter<UserRecyclerAdapter.ViewHolder> {

    private Context context;
    private List<User> mDataset;

    public static class ViewHolder extends RecyclerView.ViewHolder {
        public ImageView avatar;
        public TextView displayName;
        public TextView username;

        public ViewHolder(LinearLayout view) {
            super(view);

            avatar = (ImageView) view.findViewById(R.id.avatar);
            displayName = (TextView) view.findViewById(R.id.display_name);
            username = (TextView) view.findViewById(R.id.username);
        }
    }

    public UserRecyclerAdapter(Context context, List<User> myDataset) {
        this.context = context;
        this.mDataset = myDataset;
    }

    @Override
    public UserRecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.user_layout, parent, false);
        ViewHolder vh = new ViewHolder((LinearLayout) view);

        return vh;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        User userItem = mDataset.get(position);

        holder.displayName.setText(userItem.getDisplayName());
        holder.username.setText(userItem.getUsername());
    }

    @Override
    public int getItemCount() {
        return mDataset.size();
    }

}
Run Code Online (Sandbox Code Playgroud)

所以我的问题是,如何将User模型对象附加到每个列表项,以便在单击某个元素(如圆圈或两个TextView)时,它会将模型对象传递给下一个Activity

谢谢.

Meh*_*ria 7

implements Serializable在你User班上喜欢User implements Serializable.

通过Bundle类似的传递可序列化的类

User userItem = mDataset.get(position);

Intent yourIntent = new Intent(this, YourNextActivity.class);
Bundle b = new Bundle();
b.putSerializable("user", userItem);
yourIntent.putExtras(b); //pass bundle to your intent
startActivity(yourIntent);
Run Code Online (Sandbox Code Playgroud)

得到

Intent i = getIntent();
Bundle bundle = i.getExtras();
User user = (User) bundle.getSerializable("user");
Run Code Online (Sandbox Code Playgroud)


Eds*_*tti 4

让用户实现Parceable接口。

如果您使用 Android Studio,有一个很棒的插件可以帮助您,称为“ Android Parcelable 代码生成器”。使用该插件,您可以自动生成所有必要的代码,但基本思想如下:

你需要一个空的构造函数:

public User() {}
Run Code Online (Sandbox Code Playgroud)

然后实现接口方法:

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

@Override
public void writeToParcel(Parcel dest, int flags) {
    dest.writeString(id);
    dest.writeString(userName);
    dest.writeString(displayName);
    ...
    // Any object to be added to dest must implement Parceable in its turn
    // Please note that Lists and Serializable objects are already supported out of the box
}

public static final Parcelable.Creator<User> CREATOR = new Parcelable.Creator<User>() {

    @Override
    public EventFrame createFromParcel(Parcel source) {
        return new User(source);
    }

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

private EventFrame(Parcel source) {
    id = source.readString();
    accessToken = source.readString();
    displayName = source.readString();
    ...
}
Run Code Online (Sandbox Code Playgroud)

之后,当为下一个活动创建意图时,只需执行以下操作:

Intent yourIntent = new Intent(this, DestinyClass.class);
yourIntent.putExtra("user_identifier", user);
Run Code Online (Sandbox Code Playgroud)