Android Wierd..对象在';跨过';调试工具剪辑
这很难解释,但是。。这段代码应该从JSON数组中添加项目并显示在ListView上。使用我已覆盖的onResume方法刷新页面Android Wierd..对象在';跨过';调试工具剪辑,android,json,arraylist,Android,Json,Arraylist,这很难解释,但是。。这段代码应该从JSON数组中添加项目并显示在ListView上。使用我已覆盖的onResume方法刷新页面 public void onResume() { super.onResume(); if (adapter != null) { newItemlist.clear(); newItemlist.addAll(getJsonData()); adapter.notifyDataSetChange
public void onResume() {
super.onResume();
if (adapter != null) {
newItemlist.clear();
newItemlist.addAll(getJsonData());
adapter.notifyDataSetChanged();
//newItemlist.clear();
}
}
所有这些都应该显示JSON对象的内容。下面是全班同学
public class LogFragment extends Fragment {
Cursor cursor;
ListView listV;
String json;
SimpleAdapter adapter;
SharedPreferences exmaplePrefs;
public static final String PREFS = "examplePrefs";
ArrayList<HashMap<String, String>> newItemlist = new ArrayList<HashMap<String, String>>();
public LogFragment() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// getJsonData();
String from[] = { "time", "event", "player" };
int to[] = { R.id.time, R.id.event, R.id.player };
adapter = new SimpleAdapter(getActivity(), newItemlist,
R.layout.custom_list_row, from, to);
}
public ArrayList<HashMap<String, String>> getJsonData() {
exmaplePrefs = this.getActivity().getSharedPreferences(PREFS, 0);
json = exmaplePrefs.getString("jsonString", "cant find json");
JSONObject MainObj = null;
JSONArray JsonArray = null;
JSONObject temp = null;
try {
MainObj = new JSONObject(json);
JsonArray = MainObj.optJSONArray("Events");
if (JsonArray == null) {
temp = MainObj.getJSONObject("Events");
String time = temp.getString("time");
String event = temp.getString("event");
String player = temp.getString("player");
HashMap<String, String> map = new HashMap<String, String>();
map.put("time", time);
map.put("event", event);
map.put("player", player);
newItemlist.add(map);
} else {
for (int i = 0; i < JsonArray.length(); i++) {
JSONObject c = JsonArray.getJSONObject(i);
// Storing JSON item in a Variable
String time = c.getString("time");
String event = c.getString("event");
String player = c.getString("player");
// Adding value HashMap key => value
HashMap<String, String> map = new HashMap<String, String>();
map.put("time", time);
map.put("event", event);
map.put("player", player);
newItemlist.add(map);
}
}
} catch (Exception e) {
// TODO: handle exception
System.out.print("ERROR");
}
return newItemlist;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_log, container,
false);
listV = (ListView) rootView.findViewById(android.R.id.list);
listV.setAdapter(adapter);
return rootView;
}
@Override
public void onResume() {
super.onResume();
if (adapter != null) {
newItemlist.clear();
newItemlist.addAll(getJsonData());
adapter.notifyDataSetChanged();
//newItemlist.clear();
}
}
public类LogFragment扩展片段{
光标;
ListView-listV;
字符串json;
SimpleAdapter适配器;
共享引用exmaplePrefs;
公共静态最终字符串PREFS=“examplePrefs”;
ArrayList newItemlist=新ArrayList();
公共日志片段(){
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//getJsonData();
[]中的字符串={“时间”、“事件”、“玩家”};
int到[]={R.id.time,R.id.event,R.id.player};
adapter=new simpledapter(getActivity(),newItemlist,
R.layout.custom_list_行,from,to);
}
公共数组列表getJsonData(){
exmaplePrefs=this.getActivity().GetSharedReferences(PREFS,0);
json=exmaplePrefs.getString(“jsonString”,“找不到json”);
JSONObject MainObj=null;
JSONArray JSONArray=null;
JSONObject temp=null;
试一试{
MainObj=新的JSONObject(json);
JsonArray=MainObj.optJSONArray(“事件”);
if(JsonArray==null){
temp=MainObj.getJSONObject(“事件”);
字符串时间=临时获取字符串(“时间”);
字符串事件=临时获取字符串(“事件”);
字符串播放器=临时获取字符串(“播放器”);
HashMap=newHashMap();
地图。放置(“时间”,时间);
地图放置(“事件”,事件);
地图放置(“玩家”,玩家);
添加(映射);
}否则{
for(int i=0;ivalue
HashMap=newHashMap();
地图。放置(“时间”,时间);
地图放置(“事件”,事件);
地图放置(“玩家”,玩家);
添加(映射);
}
}
}捕获(例外e){
//TODO:处理异常
系统输出打印(“错误”);
}
返回newItemlist;
}
@凌驾
创建视图上的公共视图(布局、充气机、视图组容器、,
Bundle savedInstanceState){
视图根视图=充气机。充气(R.layout.fragment\u日志,容器,
假);
listV=(ListView)rootView.findviewbyd(android.R.id.list);
listV.setAdapter(适配器);
返回rootView;
}
@凌驾
恢复时公开作废(){
super.onResume();
if(适配器!=null){
newItemlist.clear();
addAll(getJsonData());
adapter.notifyDataSetChanged();
//newItemlist.clear();
}
}
我遇到的问题是项目被复制,例如,如果我单击item1,则显示会显示item1 item1。如果我单击item2,则显示会是item1 item2 item1 item2
这就引出了一个奇怪的问题。在调试代码时,我越是一步一步地检查代码……重复的代码就越多!例如,超过15行,下面就是结果
[{item1=ONE}、{item1=ONE}、{item1=ONE}、{item1=ONE}、{item1=ONE}、{item1=ONE}、{item1=ONE}、{item1=ONE}、{item1=ONE}、{item1=ONE}、{item1=ONE}、{item1=ONE}、{item1 item1=ONE}、{item1 item1=ONE}、{item1=ONE}、这非常简单:
- 步骤A,清除
newItemlist
- 步骤B,在
newItemlist
- 步骤C,从
getJsonData
- 步骤D,在
newItemList
你的项目是重复的。
getJsonData
返回newItemlist
。我不确定在这种情况下newItemlist.addAll
会做什么。(但它肯定会重复项目。)是的,我不敢相信我没有看到。我调用getJsonData(),而不是newItemlist.addAll(getJsonData());仍然看不出这对调试器有什么影响”/