Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pass model object to another Activity

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

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

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.

Here is the User model:

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

}

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>

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

}

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?

Thanks.

like image 686
user5314557 Avatar asked Sep 15 '15 19:09

user5314557


People also ask

How do I pass model data from one activity to another in Android?

putExtra() method is used for send the data, data in key-value pair key is variable name and value can be Int, String, Float etc. getStringExtra() method is for getting the data(key) which is send by above method. according the data type of value there are other methods like getIntExtra(), getFloatExtra()

How do I transfer data from one activity to another second activity?

This example demonstrate about How to send data from one activity to another in Android using sharedPreferences. Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project. Step 2 − Add the following code to res/layout/activity_main. xml.


3 Answers

implements Serializable in your User class like User implements Serializable.

pass serializable class via Bundle like

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

and get

Intent i = getIntent();
Bundle bundle = i.getExtras();
User user = (User) bundle.getSerializable("user");
like image 139
Mehul Kabaria Avatar answered Nov 15 '22 07:11

Mehul Kabaria


Make User implement the Parceable interface.

If you use Android Studio, there's a great plugin to help you with that called "Android Parcelable code generator". With the plugin you can just automatically generate all the necessary code, but the basic idea is as follows:

You need an empty constructor:

public User() {}

Then implement the interface methods:

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

Afterwards, when creating an intent for the next activity, just do:

Intent yourIntent = new Intent(this, DestinyClass.class);
yourIntent.putExtra("user_identifier", user);
like image 37
Edson Menegatti Avatar answered Nov 15 '22 07:11

Edson Menegatti


Use this library

@Parcel
public class User {
    public User() {}
    // You can keep all the members private with @Parcel(Serialization.BEAN)
    public String id;
    public String username;
    public String displayName;
    public Object deletedAt;
    public Statistic stat; // Needs to be @Parcel annotated as well
}

ActivityA.java
void openActivityB() {
    Intent intent = new Intent(this, ActivityB.class);
    intent.putExtra("user", Parcels.wrap(user));
    startActivity(intent);
}

ActivityB.java
void receive() {
    User user = Parcels.unwrap(getIntent().getParcelableExtra("user"));
} 

update:

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

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

    setOnClickListener(...
        onClick() {
            Context context = holder.displayName.getContext();
            Intent intent = new Intent(context, ActivityB.class);
            intent.putExtra("user", Parcels.wrap(userItem));
            context.startActivity(intent);
    });
}
like image 36
mbmc Avatar answered Nov 15 '22 07:11

mbmc