Reputation: 177
In order to improve my Android dev skills, I'm tring to make a simple shopping app. Something like this:
As you can guess from that picture, I have a list of products, which I'm parsing from a JSON like the next one:
I was already able to parse the JSON into an Array List, and list the main Products using a RecyclerView. However, I'd like to be able to click on a product and see its respective subproducts, so that I can add them to the cart.
I thought about using a Master-Detail flow by creating a new Activity, but... How can I pass a specific "Subproducts" array to the Detail Activity? (For example, clicking on 'Soda' and being redirected to the new activity, to see 'Coke', 'Dr. Pepper', and 'Sprite'). Should I just pass the whole ArrayList I already parsed? Or is there a way to only pass the "subarray" using an Intent?
I apologize for the newbish question, but I just started learning Java and Android development a week and a half ago.
Thanks in advance!
Upvotes: 2
Views: 122
Reputation: 1660
1.Gson is easiest way for parse JSON:
FOR EXAMPLE:
Gson gson = new Gson();
User user= gson.fromJson(jsonInString, User.class);
2.You have 2 way for pass data : 1.FIRST when you want to pass object you can use Parcelable FOR EXAMPLE: In object public class User implements Parcelable {
@PrimaryKey
@SerializedName("id")
private int mId;
@SerializedName("phone")
private String mPhone;
@SerializedName("email")
private String mEmail;
public User() {
}
public User(String phone) {
mPhone = phone;
}
public void setPhone(String phone) {
this.mPhone = phone;
}
public void setEmail(String email) {
this.mEmail = email;
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(this.mPhone);
dest.writeString(this.mEmail);
}
protected User(Parcel in) {
mPhone = in.readString();
mEmail = 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];
}
};
}
In first activity:
Intent intent = new Intent(getActivity(), LoadActivity.class);
intent.putExtra(SELECTED_USER, mUser);
startActivity(intent);
In second activity:
Bundle bundle=getIntent().getExtras();
User selectedUser=bundle.getParcelable(SELECTED_USER);
Attention:SELECTED_USER is a constant string.
2.SECOND when you want to pass non object: You can pass an ArrayList the same way, if the E type is Serializable. In first activity:
ArrayList<String> myList = new ArrayList<String>();
intent.putExtra("mylist", myList);
and ion the second activity:
ArrayList<String> myList = (ArrayList<String>) getIntent().getSerializableExtra("mylist");
Upvotes: 0
Reputation: 1
1.That's probably how it works.
Json Object obtained implements Serializable
SerInfo It's mine Example
2.Transmit ArrayList
ArrayList<SerInfo> listObj = new ArrayList<SerInfo>();
SerInfo serInfo1 = new SerInfo(name, website, weibo);
SerInfo serInfo2 = new SerInfo(name, website, weibo);
listObj.add(serInfo1);
listObj.add(serInfo2);
Intent intent = new Intent();
intent.setClass(MainActivity.this, ResultActivity.class);
intent.putExtra("listobj", (Serializable) listObj);
startActivity(intent);
3.Receive ArrayList
ArrayList<SerInfo> listObj = (ArrayList<SerInfo>) getIntent().getSerializableExtra("listobj");
Upvotes: 0
Reputation: 537
Gson is a good library that converts JSON to java objects and vice versa. You can convert the JSON to objects and work with the data that way. For example, you'd have the Mango class extend an abstract class Juice. With these hierarchical relationships, it will be much easier to do what you're trying to do.
Upvotes: 1