将模型对象传递给另一个Activity [英] Pass model object to another Activity

查看:65
本文介绍了将模型对象传递给另一个Activity的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个RecyclerView,它利用回收站适配器输出列表布局,如下所示:

I have a RecyclerView that utilizes a Recycler Adapter to output a list layout, like this:

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

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

I need to attach the model below to each of the list items, such that if the user clicks on any element in the list item (like the circle or one of the two TextViews), it passes the model object to the next 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;
    }

}

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

Here is the layout for each list item (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>

这是UserRecyclerAdapter,用于膨胀上面的布局:

Here is the UserRecyclerAdapter that's used to inflate the layout above:

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();
    }

}

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

So my question is, how can I attach the User model object to each list item so that when an element (like the circle or two TextViews) are clicked, it passes the model object to the next Activity?

谢谢.

推荐答案

implements Serializable在您的User类中,例如User implements Serializable.

通过

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);

并获得

Intent i = getIntent();
Bundle bundle = i.getExtras();
User user = (User) bundle.getSerializable("user");

这篇关于将模型对象传递给另一个Activity的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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