我有关于在字符串数组中传递字符串数组的问题。当moduleType =“v”时,我编写了以下代码来存储标题,信息和发言人。代码使用bundle将字符串数组放入PopulateData活动中,并将其传递给下一个actvity,即DataList Activity。我的代码如下:
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
正如DDMS中出现的那样,我在第一个活动中作为bundle传递给数组的值正在正确显示,但是当它们转到下一个活动时,它们变为空。为什么会这样?我做错了什么?
答案 0 :(得分:2)
这段代码可能是您的问题:
bundle.putStringArray("titleArray", title);
bundle.putStringArray("infoArray", info);
bundle.putStringArray("speakerArray", speaker);
bundle.putString("date", strDate);
每次循环时,您都要替换以前的值,因为每个捆绑的值都会在迭代中键入相同的键。你可以通过几种方式解决这个问题:
答案 1 :(得分:1)
您还需要将信息发送到下一个活动视图包。数据仅发送给它发送的活动。
我认为没关系,因为它只是持久存在的键值对。 像这样......
// my list of names, icon locations
Map<String, String> info = new HashMap<String, String>();
nameIcons.put("titleArray", title);
nameIcons.put("infoArray", info);
然后将它们存储在共享首选项中。
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));
}