Reputation: 183
I have problem regarding passing the string array as extra in the bundle. I have written the following code to store the title, info and speaker when the moduleType = "v". The code uses the bundle to put the array of strings in PopulateData activity and pass it to the next actvity i.e DataList Activity. My code is as follows:
PopulateData.java
for(int i = 0; i < moduleTypes.length; i++){
String moduleType = moduleTypes[i];
JSONObject jsonModule = new JSONObject(content[i]);
//Bundle[] bundle = new Bundle[jsonModule.length()];
Log.i(TAG, "ModuleType = v " + (moduleType == "v"));
Log.i(TAG, "ModuleType = v " + moduleType.equals("v"));
Log.i(TAG, "ModuleType = " + moduleType);
Log.i(TAG, "ModuleType Length = " + moduleTypes.length);
if(moduleType.equals("v")){
//String[] title = new String[jsonModule.length()];
//String[] info = new String[jsonModule.length()];
//String[] speaker = new String[jsonModule.length()];
String[] videoTypes = dbAdapter.collectVideoTypeFromListTable();
String[] title = new String[videoTypes.length];
String[] info = new String[videoTypes.length];
String[] speaker = new String[videoTypes.length];
title[i] = jsonModule.getString("title");
info[i] = jsonModule.getString("info");
speaker[i] = jsonModule.getString("speaker");
Log.i(TAG, "titleArray");
Log.i(TAG,title[i]);
Log.i(TAG, "infoArray");
Log.i(TAG,info[i]);
Log.i(TAG, "speakerArray");
Log.i(TAG,speaker[i]);
bundle.putStringArray("titleArray", title);
bundle.putStringArray("infoArray", info);
bundle.putStringArray("speakerArray", speaker);
bundle.putString("date", strDate);
}
intent.putExtras(bundle);
}
DataList.java
bundle = getIntent().getExtras();
title = bundle.getStringArray("titleArray");
Log.i(TAG, "Title Size="+String.valueOf(title.length));
info = bundle.getStringArray("infoArray");
Log.i(TAG, "Info Size="+String.valueOf(info.length));
speaker = bundle.getStringArray("speakerArray");
Log.i(TAG, "Speaker Size="+String.valueOf(speaker.length));
date = bundle.getString("date");
Log.i(TAG, "Bundle Size="+String.valueOf(bundle.size()));
Log.i(TAG, ""+title[0]);
//bundle.p
//title = bundle.getString("title");
//info = bundle.getString("info");
//speaker = bundle.getString("speaker");
for(int i = 0; i < title.length; i++){
Log.i(TAG, "title["+i+"]"+title[i]);
Log.i(TAG, "info["+i+"]"+info[i]);
Log.i(TAG, "speaker["+i+"]"+speaker[i]);
Log.i(TAG, date);
Content content = new Content(title[i], info[i], speaker[i], date);
contentList.add(content);
}
DDMS:
01-09 18:53:04.305: INFO/PopulateData(435): ModuleType = v false
01-09 18:53:04.305: INFO/PopulateData(435): ModuleType = v true
01-09 18:53:04.305: INFO/PopulateData(435): ModuleType = v
01-09 18:53:04.305: INFO/PopulateData(435): ModuleType Length = 9
01-09 18:53:04.315: INFO/DronaDBAdapter(435): Type: v
01-09 18:53:04.315: INFO/DronaDBAdapter(435): Type: v
01-09 18:53:04.315: INFO/DronaDBAdapter(435): Type: v
01-09 18:53:04.325: INFO/PopulateData(435): titleArray
01-09 18:53:04.325: INFO/PopulateData(435): Capital Budgeting
01-09 18:53:04.325: INFO/PopulateData(435): infoArray
01-09 18:53:04.325: INFO/PopulateData(435): Bhaavesh bhai will give full to training of Capital budgeting to team deltecs. All the best for him.
01-09 18:53:04.325: INFO/PopulateData(435): speakerArray
01-09 18:53:04.325: INFO/PopulateData(435): Bhaavesh Bhai
01-09 18:53:04.325: INFO/PopulateData(435): ModuleType = v false
01-09 18:53:04.325: INFO/PopulateData(435): ModuleType = v true
01-09 18:53:04.335: INFO/PopulateData(435): ModuleType = v
01-09 18:53:04.335: INFO/PopulateData(435): ModuleType Length = 9
01-09 18:53:04.345: INFO/DronaDBAdapter(435): Type: v
01-09 18:53:04.345: INFO/DronaDBAdapter(435): Type: v
01-09 18:53:04.345: INFO/DronaDBAdapter(435): Type: v
01-09 18:53:04.345: INFO/PopulateData(435): titleArray
01-09 18:53:04.345: INFO/PopulateData(435): Nixon Cameras
01-09 18:53:04.345: INFO/PopulateData(435): infoArray
01-09 18:53:04.355: INFO/PopulateData(435): Nixon India is a leading camera manufacters in India.
01-09 18:53:04.355: INFO/PopulateData(435): speakerArray
01-09 18:53:04.355: INFO/PopulateData(435): Nixon India
01-09 18:53:04.355: INFO/PopulateData(435): ModuleType = v false
01-09 18:53:04.365: INFO/PopulateData(435): ModuleType = v true
01-09 18:53:04.365: INFO/PopulateData(435): ModuleType = v
01-09 18:53:04.365: INFO/PopulateData(435): ModuleType Length = 9
01-09 18:53:04.375: INFO/DronaDBAdapter(435): Type: v
01-09 18:53:04.375: INFO/DronaDBAdapter(435): Type: v
01-09 18:53:04.375: INFO/DronaDBAdapter(435): Type: v
01-09 18:53:04.385: INFO/PopulateData(435): titleArray
01-09 18:53:04.385: INFO/PopulateData(435): Nixon Cameras
01-09 18:53:04.385: INFO/PopulateData(435): infoArray
01-09 18:53:04.385: INFO/PopulateData(435): Nixon India is a leading camera manufacters in India.
01-09 18:53:04.385: INFO/PopulateData(435): speakerArray
01-09 18:53:04.395: INFO/PopulateData(435): Nixon India
01-09 18:53:04.395: INFO/PopulateData(435): ModuleType = v false
01-09 18:53:04.395: INFO/PopulateData(435): ModuleType = v false
01-09 18:53:04.395: INFO/PopulateData(435): ModuleType = n
01-09 18:53:04.405: INFO/PopulateData(435): ModuleType Length = 9
01-09 18:53:04.405: INFO/PopulateData(435): ModuleType = v false
01-09 18:53:04.405: INFO/PopulateData(435): ModuleType = v false
01-09 18:53:04.415: INFO/PopulateData(435): ModuleType = n
01-09 18:53:04.415: INFO/PopulateData(435): ModuleType Length = 9
01-09 18:53:04.415: INFO/PopulateData(435): ModuleType = v false
01-09 18:53:04.415: INFO/PopulateData(435): ModuleType = v false
01-09 18:53:04.425: INFO/PopulateData(435): ModuleType = n
01-09 18:53:04.425: INFO/PopulateData(435): ModuleType Length = 9
01-09 18:53:04.425: INFO/PopulateData(435): ModuleType = v false
01-09 18:53:04.425: INFO/PopulateData(435): ModuleType = v false
01-09 18:53:04.435: INFO/PopulateData(435): ModuleType = e
01-09 18:53:04.435: INFO/PopulateData(435): ModuleType Length = 9
01-09 18:53:04.445: INFO/PopulateData(435): ModuleType = v false
01-09 18:53:04.445: INFO/PopulateData(435): ModuleType = v false
01-09 18:53:04.445: INFO/PopulateData(435): ModuleType = q
01-09 18:53:04.445: INFO/PopulateData(435): ModuleType Length = 9
01-09 18:53:04.455: INFO/PopulateData(435): ModuleType = v false
01-09 18:53:04.455: INFO/PopulateData(435): ModuleType = v false
01-09 18:53:04.455: INFO/PopulateData(435): ModuleType = s
01-09 18:53:04.465: INFO/PopulateData(435): ModuleType Length = 9
01-09 18:53:04.465: INFO/ActivityManager(59): Starting activity: Intent { cmp=com.drona.app/.DataList (has extras) }
01-09 18:53:04.545: INFO/DataList(435): Title Size=3
01-09 18:53:04.545: INFO/DataList(435): Info Size=3
01-09 18:53:04.545: INFO/DataList(435): Speaker Size=3
01-09 18:53:04.545: INFO/DataList(435): Bundle Size=4
01-09 18:53:04.555: INFO/DataList(435): null
01-09 18:53:04.555: INFO/DataList(435): title[0]null
01-09 18:53:04.555: INFO/DataList(435): info[0]null
01-09 18:53:04.555: INFO/DataList(435): speaker[0]null
01-09 18:53:04.565: INFO/DataList(435): Jan 09
01-09 18:53:04.565: INFO/DataList(435): title[1]null
01-09 18:53:04.565: INFO/DataList(435): info[1]null
01-09 18:53:04.565: INFO/DataList(435): speaker[1]null
01-09 18:53:04.565: INFO/DataList(435): Jan 09
01-09 18:53:04.565: INFO/DataList(435): title[2]Nixon Cameras
01-09 18:53:04.575: INFO/DataList(435): info[2]Nixon India is a leading camera manufacters in India.
01-09 18:53:04.575: INFO/DataList(435): speaker[2]Nixon India
01-09 18:53:04.575: INFO/DataList(435): Jan 09
As appearing in the DDMS, the values that I am passing in the array as bundle in the first activity is getting shown properly, but the when they go to the next activity, they become null. Why is it so? Is there anything wrong I had done?
Upvotes: 0
Views: 191
Reputation: 10974
This chunk of code is likely your problem:
bundle.putStringArray("titleArray", title);
bundle.putStringArray("infoArray", info);
bundle.putStringArray("speakerArray", speaker);
bundle.putString("date", strDate);
Each time you go through the loop, you are replacing the previous values because each of the bundled values are keyed to the same key over iterations. You could get around this a couple ways:
Upvotes: 2
Reputation: 26981
You also need to send the information to the next activity view bundle. The data only goes to the activity it is sent ot.
I think it is okay since it is simply just key-value pairs which are persisted. Something like this..
// my list of names, icon locations
Map<String, String> info = new HashMap<String, String>();
nameIcons.put("titleArray", title);
nameIcons.put("infoArray", info);
Then store them in shared preferences.
SharedPreferences keyValues = getContext().getSharedPreferences("info_preferences"), Context.MODE_PRIVATE);
SharedPreferences.Editor keyValuesEditor = keyValues.edit();
for (String s : info.keySet()) {
keyValuesEditor.pustString(s, info.get(s));
}
Upvotes: 1