I'm hitting an external API that's returning JSON data (new dvd titles). I'm able to parse out the JSON and list each dvd title and other dvd information into a ListView just fine. I was also able to setup an onListItemClick method just fine for primitive data (title string). I ended up writing something like so for the onListItemClick method:
Just to note, the productArray is a class var that's being set by another method that holds an array of JSONObjects.
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
Intent i = new Intent(DvdListingActivity.this, MovieProductActivity.class);
try {
JSONObject jsonObj = productArray.getJSONObject(position);
i.putExtra("mTitle", jsonObj.getJSONObject("Title").opt("val").toString());
i.putExtra("mRelDate", jsonObj.getJSONObject("RelDate").opt("val").toString());
i.putExtra("mDesc", jsonObj.getJSONObject("Desc").opt("val").toString());
i.putExtra("mRating", jsonObj.getJSONObject("MPAA").getJSONObject("Rating").opt("val").toString());
i.putExtra("mActors", jsonObj.getJSONObject("Actors").opt("val").toString());
i.putExtra("mImage", jsonObj.getJSONObject("Image").opt("val").toString());
startActivity(i);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The above code all works, but I'm thinking there's GOTTA be a better way for me to pass in data to another Activity. I was thinking that I would be able to pass a JSONObject that contains all the data for a dvd movie instead of setting each data point individually.
I tried for a week and a half to figure out how to use Parcelable. I tried instantiating a new JSONObject jsonObj that implements Parcelable with no luck. I kept getting an error in my LogCat that said that the object was un-parcelable.
I've tried reading the Android developer site and other blogs, but I couldn't apply their examples to what I needed to do.
Any help would be much appreciated
You can simply put an entire JSONObject as a string. Something like this:
i.putString("product", jsonObj.toString);
And then in the MovieProductActivity you could
JSONObject jsonObj = new JSONObject(getIntent().getStringExtra("product"));
From Current Activity
Intent mIntent = new Intent(CurrentActivity.this, TargetActivity.class);
mIntent.putExtra("ITEM_EXTRA", json_object.toString());
startActivity(mIntent);
From Target Activity's onCreate
try {
json_object = new JSONObject(getIntent().getStringExtra("ITEM_EXTRA"));
Log.e(TAG, "Example Item: " + json_object.getString("KEY"));
} catch (JSONException e) {
e.printStackTrace();
}
You can just encapsulate all of the information about a movie into a Movie object, which implements Parcelable.
The code will look similar to above, but instead of passing 6 different extras you can just pass one extra that is the movie.
Movie movie = new Movie();
movie.setTitle(jsonObj.getJSONObject("Title").opt("val").toString());
movie.setRelDat(jsonObj.getJSONObject("RelDate").opt("val").toString());
.
.
.
i.putExtra("movie", movie);
For information on implementing a Parcelable object, see Parcelable docs. You basically just write out each string in 'writeToParcel', and read in each string in 'readFromParcel' in the correct order.
The answer posted here by Cheryl Simon is completely correct however this might not explain it too clearly for someone who is new to android or java. Please let me build on her answer.
The best and cleanest way to do this is with the Parcelable interface.
Declare a class as below that implements Parcelable. This one uses an accessor and mutator which isn't actually necessary.
public class JSonArrayParser implements Parcelable {
private JSONArray jsonArray;
public JSonArrayParser() {}
public JSonArrayParser(JSONArray jsonArray) {
this.jsonArray = jsonArray;
}
public void setJsonArray(JSONArray jsonArray) {
this.jsonArray = jsonArray;
}
public JSONArray getJsonArray() {
return jsonArray;
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
}
}
The next step would be to create an instance of that class (in this case JSonArrayParser) and pass it through the Bundle as in the example below.
//This could be an Activity, Fragment, DialogFragment or any class that uses the Bundle Type such as savedInstanceState.
jobItemsPopUp = new JobItemsPopUp();//This example uses a DialogFragment.
Bundle bundle = new Bundle();
JSonArrayParser jSonArrayParser = new JSonArrayParser(jsonArray);
bundle.putParcelable("jsonArray", jSonArrayParser);
jobItemsPopUp.setArguments(bundle);
jobItemsPopUp.show(getActivity().getSupportFragmentManager(), "JobItemsPopUp");
The the way your would retrieve the value would be as follows (I've used the onCreate but you can retrieve the value anywhere):
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
this.jsonArray = ((JSonArrayParser) getArguments().getParcelable("jsonArray")).getJsonArray();
}
}
Just as an additional note: you can do this with custom constructors, but this method has been deprecated by Google.
The old deprecated method (associated with app compat v4 NOT RECOMMENDED) would be:
private Fragment MyFragment;
MyFragment = new MyFragment(jsonArray)
Hope this help and clears a few things up.
The Parcelable interface can be used with any data class with or without mutators and/or accessors.
Are you storing the information in a DB? If you are, you can simply pass the ID of the desired title (via the intent).
Have a look at gson. It allows you to sereialise and deserialise JSON blobs to entire class instances.
Just create a Parcel like this:
public class Parcel implements Serializable {
private JSONObject obj;
public Parcel(JSONObject obj) {
this.obj = obj;
}
public JSONObject getObj() {
return obj;
}}
And put into the bundle:
Parcel p = new Parcel(data);
Bundle args = new Bundle();
args.putSerializable("events", p);
Finally, you can get the JSONObject back using:
JSONObject obj = ((Parcel) ((Bundle) getArguments()).getSerializable("events")).getObj();
Related
I have an array list of JSON Objects that I'm trying to pass like so.
ArrayList<JSONObject> entryCities = new ArrayList<>();
for(int i = 0; i < entries.length(); i++){
entryCities.add(entries.getJSONObject(i));
}
Intent intent = new Intent(UnitedStatesActivity.this, StateActivity.class);
intent.putExtra("object", entryCities);
startActivity(intent);
I know this doesn't work and that I need to make my array, or the JSON Object in the array parcelable. I started looking at the documentation and have come up with this class that I am wanting to pass the ArrayList<JSONObject> into, but I'm not sure what I'm supposed to be adding in my writeToParcel() method.
Am I writing out the specific values of the JSON Object I want to pull? i.e.
out.object.getString("string") and then passing my parcelable object on to the next activity? I'm just not sure what the content of the method should be in the context of making JSON Objects parcelable.
public class StateParcelable extends JSONObject implements Parcelable {
ArrayList<JSONObject> list;
public StateParcelable(ArrayList<JSONObject> list){
this. list = list;
}
public void writeToParcel(Parcel out, int flags) {
}
public int describeContents(){
return 0;
}
public static final Parcelable.Creator<StateParcelable> CREATOR
= new Parcelable.Creator<StateParcelable>() {
public StateParcelable createFromParcel(Parcel in) {
return new StateParcelable(in);
}
public StateParcelable[] newArray(int size) {
return new StateParcelable[size];
}
};
private StateParcelable(Parcel in) {
}
Since you already have (what appears to be) a JSONArray of JSONObjects, why not just convert it to a String, pass that, and rebuild the JSONArray from that?
Intent intent = new Intent(UnitedStatesActivity.this, StateActivity.class);
intent.putExtra("object", entries.toString());
startActivity(intent);
And then in the following Activity:
JSONArray entries = new JSONArray(getIntent().getStringExtra("object"));
Otherwise, yes, you'd have to loop through and serialize the individual items in your ArrayList and then rebuild and add them to the list.
Also, a side note, you can pass the individual JSONObjects the same way: via toString() and new JSONObject(getIntent().getStringExtra("object")), respectively.
i want to Pass Hash_set object when click on button,object pass from one fragment to another fragment. in Hash_set object I Put multiple Parse Object in Hash_set object .How to do it ? i tried with Intent and Bundle but not get successful to pass object.
please provide me any best idea. thanks `
You can Also use Application.
step1:Use this class
public class JsonApplication extends Application {
public JSONObject jsonObject;
public JSONObject getJsonObject() {
return jsonObject;
}
public void setJsonObject(JSONObject jsonObject) {
this.jsonObject = jsonObject;
}}
step2: set data and get data as your desire
Simply, create an static object of your hashset in the activity like this,
public static Set<YourObject> mObject = new HashSet<YourObject>();
and from your BaseAdapter-button-onClick call it in this way,
ActivityName.mObject = mYourHashSet;
A public static field/method
An alternate way to make data accessible across Activities/Services is
to use public static fields and/or methods. You can access these
static fields from any other class in your application. To share an
object, the activity which creates your object sets a static field to
point to this object and any other activity that wants to use this
object just accesses this static field.
Other way todo this,
In you baseAdapter-onClick, convert it to JSONObject like that,
JSONObject multiple = new JSONObject();
multiple.put("hash_set_object", mHashSetObject);
Intent ii = new Intent(mActivity, ActivityName.class);
ii.putExtra("hash_set_json", multiple.toString());
startActivity(ii);
In Activity's onCreate call it in this way,
String HashString = getIntent().getExtras().getString("hash_set_json");
JSONArray array = new JSONArray(new JsonObject(HashString).getString("hash_set_object"));
//Now make for-loop to re-populate hashSet from JSONArray
I have been working on a e-commerce application for a while, and now I have a ListView that displays a list of products - Each products = 1 ImageView and some TextViews-.
I set an onItemClick listener on that ListView, the event that I want to occur when I click on one of that listView products is, Start the i new Activity, 'ProductDetails' that displays more informations on the product I have clicked on.
I have already asked this question somewhere else, but I didn't get a clear answer,
They said that I have to create a new 'list\details' project, but this can't happen now, as I've working on that project for like 20 days, and can't start all over again.
You can't send a TextView or an ImageView from Activity to Activity. what you can send instead is it's contents. so for example if you want to pass the information from the TextView you will need to pass the source String that is displayed there (Or extract the String from the TextView) you do that by passing a Bundle from the calling Activity to the invoked one or simply by putting it as an Extra:
intent.putExtra("string name", value);
and in the following Activity you get this data:
Intent intent = getIntent();
bundle = intent.getExtras();
bundle.getString("string name");
Then in the following Activity create a TextView with the passed String.
Same way is handled with the ImageView by passing the it's path.
Sorry I have posted an answer but was only a link to another site so it was deleted :-(
Here I come again with what could be the solution:
In your first activity, you need to use intent.putExtra(...) and retrieve the datas in your second activity with intent.getExtra(...).
Example activity 1:
i.putExtra("title01", yourDataCollection.get(position).get(KEY_TITLE01));
Activity 2 :
this.title01 = i.getStringExtra("title01");
There is a full project here : http://www.codeproject.com/Articles/507651/Customized-Android-ListView-with-Image-and-Text
Android uses intent to allow Activitys to interact. If you want to show a details of product, probably you have a Class that describe that product and with the information it holds you fill up your ListView. If you let the class Product implements Serializable you can use Intent.putExtra(String name, Serializable obj), to pass the Product description between Activitys. In ProductDetailsActivity you can use:
getIntent(). getSerializableExtra (String name)
to retrive the serializable you put inside the Intent you created to start ProductDetailsActivity
If you pass your model class around many times using Intent, perhaps it could implement the Parcelable interface for convenience. For example:
class ProductData implements Parcelable {
protected String name;
protected String someOtherData;
public static final Parcelable.Creator<ProductData> CREATOR = new Parcelable.Creator<ProductData>() {
#Override
public ProductData createFromParcel(Parcel source) {
return new ProductData(source);
}
#Override
public ProductData[] newArray(int size) {
return new ProductData[size];
}
};
public ProductData(){
}
public ProductData(Parcel source){
readFromParcel(source);
}
#Override
public int describeContents() {
return 0;
}
#Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(name);
dest.writeString(someOtherData);
}
public void readFromParcel(Parcel source){
name = source.readString();
someOtherData = source.readString();
}
// Here goes the rest of your model code
}
Then you can easily pass the object to another Activity using an Intent:
intent.putExtra("product", productData);
and later get the data using
ProductData productData = intent.getParcelableExtra("product");
i'm continuously running into problems trying to pass an ArrayList from one Activity to another. My code is failing with a Null Pointer Exception when i try to iterate through the ArrayList in my XMLParser Class. I've put print statements into the Activity that generates the ArrayList and it looks fine. Can anyone see what i'm doing wrong or why i get a Null pointer Exception when retrieving the ArrayList?
public void onClick(View v) {
if (selItemArray[0] == null) {
Toast.makeText(getApplicationContext()," Please make a Selection ", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(Recipes2.this, XMLParser.class);
Log.v("Recipes2", "selItemArray[0] before call to XML Parser : " + selItemArray[0]);
//Log.v("Recipes2", "selItemArray[1] before call to XML Parser : " + selItemArray[1]);
intent.putExtra("items_to_parse", selItemArray);
startActivityForResult(intent, 0);
}
}
public class XMLParser extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//Bundle b = getIntent().getExtras();
//itemsToParse = b.getStringArrayList("items_to_parse");
ArrayList<String> itemsToParse = new ArrayList<String>();
itemsToParse = getIntent().getExtras().getStringArrayList("items_to_parse"); Iterator<String> iterator = itemsToParse.iterator(); while(iterator.hasNext())
Log.v("XMLParser", iterator.next().toString());
It looks like you're putting a String array, not a ArrayList<String>.
You used a string array on the "sender" side and are trying to get it back as an ArrayList on the receiver side. That won't work. Use a String array on both sides and -- if necessary -- pull it into an array list.
The procedure for passing the data is here:
Passing String array between two class in android application
To convert it to a List - simply do:
List<String> = Arrays.asList(stringArray);
So I've been googling most of yesterday and last nite and just can't seem to wrap my head around how to pass an arraylist to a subactivity. There are tons of examples and snippets passing primitive data types, but what I have is an arraylist of type address (address.java below).
I've found a lot of stuff on stackoverflow and around the web on this, but nothing that got a lot of attention except for one with a GeoPoint example. Again, it looked to me like they just flattened the GeoPoint object into two integers and passed it in. I can't do that because my address class may expand to include integers, floats, whatever. Right now, the test app below is only two strings for simplicity. I thought if I could get the parcelalbe stuff working with that, the rest could follow.
Can someone post a working example for an ArrayList of a non-primitive object, or perhaps add code below to make this work?
UPDATE: code below is now working after replies/editing. Thanks!
/* helloParcel.java */
public class helloParcel extends Activity
{
// holds objects of type 'address' == name and state
private ArrayList <address> myList;
#Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
setContentView (R.layout.main);
Button b1 = (Button) findViewById(R.id.button1);
b1.setOnClickListener(ocl);
myList = new ArrayList();
address frank = new address ("frank", "florida");
address mary = new address ("mary", "maryland");
address monty = new address ("monty", "montana");
myList.add (frank);
myList.add (mary);
myList.add (monty);
// add the myList ArrayList() the the extras for the intent
}
OnClickListener ocl = new OnClickListener()
{
#Override
public void onClick(View v)
{
// fill parceable and launch activity
Intent intent = new Intent().setClass(getBaseContext (), subActivity.class);
// for some reason, I remember a posting saying it's best to create a new
// object to pass. I have no idea why..
ArrayList <address> addyExtras = new ArrayList <address>();
for (int i = 0; i < myList.size(); i++)
addyExtras.add (myList.get(i));
intent.putParcelableArrayListExtra ("mylist", addyExtras);
startActivity(intent);
}
};
}
/* address.java */
public class address implements Parcelable
{
private String name;
private String state;
private static String TAG = "** address **";
public address (String n, String s)
{
name = n;
state = s;
Log.d (TAG, "new address");
}
public address (Parcel in)
{
Log.d (TAG, "parcel in");
name = in.readString ();
state = in.readString ();
}
public String getState ()
{
Log.d (TAG, "getState()");
return (state);
}
public String getName ()
{
Log.d (TAG, "getName()");
return (name);
}
public static final Parcelable.Creator<address> CREATOR
= new Parcelable.Creator<address>()
{
public address createFromParcel(Parcel in)
{
Log.d (TAG, "createFromParcel()");
return new address(in);
}
public address[] newArray (int size)
{
Log.d (TAG, "createFromParcel() newArray ");
return new address[size];
}
};
#Override
public int describeContents ()
{
Log.d (TAG, "describe()");
return 0;
}
#Override
public void writeToParcel (Parcel dest, int flags)
{
Log.d (TAG, "writeToParcel");
dest.writeString (name);
dest.writeString (state);
}
}
/* subActivity.java */
public class subActivity extends Activity
{
private final String TAG = "** subActivity **";
private ArrayList <address> myList;
#Override
protected void onCreate (Bundle savedInstanceState)
{
super.onCreate (savedInstanceState);
Log.d (TAG, "onCreate() in subActivity");
setContentView(R.layout.subactivity);
TextView tv1 = (TextView) findViewById(R.id.tv_sub);
myList = getIntent().getParcelableArrayListExtra ("mylist");
Log.d (TAG, "got myList");
for (int i = 0; i < myList.size (); i++)
{
address a = myList.get (i);
Log.d (TAG, "state:" + a.getState ());
tv1.setText (a.getName () + " is from " + a.getState ());
}
}
}
I can see a number of problems here:
Why use addressParcelable? Why not make address implement Parcelable, and then use:
intent.putParcelableArrayListExtra( "addresses", addyExtras );
Your parcelable object must include a static CREATOR. See the documentation for details.
You are not actually adding any extras to the intent before you call startActivity(). See point 1 for a suggestion here.
I think that you will need to address all of these issues in order to get it working.
It can be done MUCH simpler, without all the pain-in-the-ass of implementing Parcelable...
ArrayList (but NOT any List) is Serializable. So, you can put the entire list using putExtra() and retrieve it using getSerializableExtra(), as Sam said.
BUT, I want to add one more important thing: the object your array list stores has to also implement Serializable... and all other complex objects that the object may contain (in your case none) must also implement that (so it's recursive - in order to serialize an object, you must be able to serialize all of its fields).
Now, you might be asking yourself why implementing Serializable instead of Parcelable when there are already methods for reading and writing array lists of parcelables?
Well... the difference is simplicity - just add implements Serializable and optionally private static final long serialVersionUID = SOME_CONSTANT and you're DONE! That is the reason why I never use Parcelable - you can do all those things using Serializable with literally 2 lines of code - instead of many method inheritances and all that stuff...
You can pass Serializable objects via putExtra. ArrayList implements Serializable.
Mike dg is correct!
putExtra() and getSerializable() will store and retrieve an ArrayList<> of your custom objects, with no interface implementing required. Worked for me!