I have two Activity A and B.I call API in Activity A and get response in JsonArray. Now I need to get Whole JsonArray in Activity B from Activity A.how can I do this?
Asked
Active
Viewed 1,776 times
0
-
what you tried so far ? – Ganesh Gudghe Feb 07 '17 at 07:47
-
well I get no of column and row in responce jsonArray.from that no of column and rown I have display button horizontally and vertically in Activity B.@GaneshPatil – mohsinmemon Feb 07 '17 at 08:08
-
pass JsonArray through intent from activity A to B – Gautam Feb 07 '17 at 08:08
4 Answers
1
@mohsinmemon
Convert JsonArray to String then attach it to Intent ans send it.
JSONObject jObject = new JSONObject("Json Response");
Intent obj_intent = new Intent(ActivityA.this, ActivityB.class);
Bundle b = new Bundle();
b.putString("Array",jObject.toString());
obj_intent.putExtras(b);
In ActivityB
Bundle b = getIntent().getExtras();
String Array=b.getString("Array");

eLemEnt
- 1,741
- 14
- 21
1
try this,
In Activity A:
Intent intent = new Intent(ActivityA.this, ActivityB.class);
intent.putExtra("jsonArray", jsonArray.toString());
startActivity(intent);
In Activity B:
Intent intent = getIntent();
String jsonArray = intent.getStringExtra("jsonArray");
try {
JSONArray array = new JSONArray(jsonArray);
} catch (JSONException e) {
e.printStackTrace();
}

Komal12
- 3,340
- 4
- 16
- 25
0
If you are not starting the second activity from first; declare that Json as public static else just pass it as bundle in intent.

nullvoid
- 121
- 8
-
no without using static I need from Activity A to B via any method,putExtra,getExtra() but Whole Array? – mohsinmemon Feb 07 '17 at 08:04
-
then just use the toString(), it will convert it into a string which you can pass as extras and then you just have to use new JsonArray("string here"). – nullvoid Feb 07 '17 at 08:35
0
first you should convert your data as an ArrayList in activity A like below :
ArrayList<YourCustomDataModel> dataArrayList= null;
String jsonInternets = //"Your Json Response"
if (jsonInternets != null && !jsonInternets.isEmpty()) {
Gson gson = new Gson();
YourCustomDataModel[] dataModels= gson.fromJson(jsonInternets,
YourCustomDataModel[].class);
dataArrayList= new ArrayList<YourCustomDataModel>(Arrays.asList(dataModels));
}
after this step you can put this Arraylist in your intent like below :
Intent intent=new Intent(ActivityA.this,ActiviyB.class);
intent.putExtra("data",dataArrayList);
startActivity(intent);
now after this steps in Activity B you can get your data as an ArrayList like below:
if (getIntent().getExtras()!=null&&getIntent().getExtras().getSerializable("data")!=null)
{
ArrayList<YourCustomDataModel> dataArrayList=getIntent().getExtras().getSerializable("data");
}

Morteza
- 59
- 2
- 10