Java 解析相同JSON的不同结果
我很难弄清楚是什么导致了我的问题 因此,我试图从JSON中获取两组字符串,并在listview中显示它们。当我第一次测试这个应用程序时,它非常棒。一切正常 但是当我关闭并重新打开它时,我的listview只显示一组字符串(只有第二组,重复两次) 这是处理JSON解析的类:(它在异步任务中执行)Java 解析相同JSON的不同结果,java,android,json,listview,android-asynctask,Java,Android,Json,Listview,Android Asynctask,我很难弄清楚是什么导致了我的问题 因此,我试图从JSON中获取两组字符串,并在listview中显示它们。当我第一次测试这个应用程序时,它非常棒。一切正常 但是当我关闭并重新打开它时,我的listview只显示一组字符串(只有第二组,重复两次) 这是处理JSON解析的类:(它在异步任务中执行) 公共类ListarPosts扩展了SherlockActivity{ 字符串URL=null,Titulo=null; ArrayList posts=新的ArrayList(); 公共ArrayList
公共类ListarPosts扩展了SherlockActivity{
字符串URL=null,Titulo=null;
ArrayList posts=新的ArrayList();
公共ArrayList getPosts(字符串Json){
试一试{
JSONObject jObject=新的JSONObject(Json);
JSONArray jArray=jObject.getJSONArray(“Posts”);
for(int i=0;i
当我打印键和值时,它会显示正确的字符串,即使listview是错误的
我的猜测是,当我创建新的“Post”并将其添加到ArrayList时,出现了一些问题。这就解释了为什么它会打印正确的一个,但不会在ListView中显示它。可能是因为我在AsyncTask中创建了一个新实例而发生冲突
不管怎样,我有点迷路了。如果你有任何想法,我应该尝试或什么可能是错误的,请让我知道
谢谢你的帮助 在添加到新数据之前,只需清除每次Arraylist,如下所示。。。。 并删除Asynctask中的runonuithread。此处不需要
public ArrayList<Post> getPosts(String Json){
try {
JSONObject jObject = new JSONObject(Json);
JSONArray jArray = jObject.getJSONArray("Posts");
if(posts.size()>0){
posts.clear();
}
for (int i = 0; i < jArray.length(); i++) {
final int id = jArray.getJSONObject(i).getInt("ID");
final int tipo = verificaTipo(jArray.getJSONObject(i).getString("Tipo"));
final JSONArray jValues = jArray.getJSONObject(i).getJSONArray("Valores");
for (int a = 0; a < jValues.length(); a++) {
final String KEY = jValues.getJSONObject(a).getString("Key");
System.out.println(KEY);
final String Value = jValues.getJSONObject(a).getString("Value");
System.out.println(Value);
if (KEY.equals("URL"))
URL = Value;
else if (KEY.equals("TITULO"))
Titulo = Value;
}
posts.add(new Post(id, Titulo, tipo));
}
return posts;
} catch (ParseException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
public ArrayList getPosts(字符串Json){
试一试{
JSONObject jObject=新的JSONObject(Json);
JSONArray jArray=jObject.getJSONArray(“Posts”);
如果(posts.size()>0){
posts.clear();
}
for(int i=0;i
就是这样……问题出在runOnUiThread()中。我认为Tipo的值在线程中更新
public class ListarPosts extends Activity {
ArrayList<String> URL = null, Titulo = null;
ArrayList<Post> posts = new ArrayList<Post>();
int id;
int tipo;
public ArrayList<Post> getPosts(String Json) {
try {
posts = new ArrayList<Post>();
URL = new ArrayList<String>();
Titulo = new ArrayList<String>();
JSONObject jObject = new JSONObject(Json);
JSONArray jArray = jObject.getJSONArray("Posts");
for (int i = 0; i < jArray.length(); i++) {
id = jArray.getJSONObject(i).getInt("ID");
tipo = 1;
final JSONArray jValues = jArray.getJSONObject(i).getJSONArray(
"Valores");
for (int a = 0; a < jValues.length(); a++) {
final String KEY = jValues.getJSONObject(a)
.getString("Key");
System.out.println(KEY);
final String Value = jValues.getJSONObject(a).getString(
"Value");
System.out.println(Value);
if (KEY.equals("URL"))
URL.add(Value);
else if (KEY.equals("TITULO"))
Titulo.add(Value);
}
}
for (String value : Titulo) {
// If you need to use runOnUiThread, you can implement here
posts.add(new Post(id, value, tipo));
}
return posts;
} catch (ParseException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}
公共类ListarPosts扩展活动{
ArrayList URL=null,Titulo=null;
ArrayList posts=新的ArrayList();
int-id;
国际提波;
公共ArrayList getPosts(字符串Json){
试一试{
posts=newarraylist();
URL=新的ArrayList();
Titulo=newarraylist();
JSONObject jObject=新的JSONObject(Json);
JSONArray jArray=jObject.getJSONArray(“Posts”);
for(int i=0;i public class ListarPosts extends Activity {
ArrayList<String> URL = null, Titulo = null;
ArrayList<Post> posts = new ArrayList<Post>();
int id;
int tipo;
public ArrayList<Post> getPosts(String Json) {
try {
posts = new ArrayList<Post>();
URL = new ArrayList<String>();
Titulo = new ArrayList<String>();
JSONObject jObject = new JSONObject(Json);
JSONArray jArray = jObject.getJSONArray("Posts");
for (int i = 0; i < jArray.length(); i++) {
id = jArray.getJSONObject(i).getInt("ID");
tipo = 1;
final JSONArray jValues = jArray.getJSONObject(i).getJSONArray(
"Valores");
for (int a = 0; a < jValues.length(); a++) {
final String KEY = jValues.getJSONObject(a)
.getString("Key");
System.out.println(KEY);
final String Value = jValues.getJSONObject(a).getString(
"Value");
System.out.println(Value);
if (KEY.equals("URL"))
URL.add(Value);
else if (KEY.equals("TITULO"))
Titulo.add(Value);
}
}
for (String value : Titulo) {
// If you need to use runOnUiThread, you can implement here
posts.add(new Post(id, value, tipo));
}
return posts;
} catch (ParseException e) {
e.printStackTrace();
return null;
} catch (Exception e) {
e.printStackTrace();
return null;
}
}
}