我有一个通过进行查询来获取JSON字符串的活动类。我将字符串解析为ArrayList(HashMap(String,String))。我将它传递给ListView。我面临的问题是在显示ArrayList的元素时,只有列表中的最后一个元素显示在UI中。即如果列表中有3个元素,则第三个元素显示三次
这是我的功能。 “buglist”视图中有一个ListView,而R.id.text1 / 2/3基本上是TextViews。
public class GetBugs extends ListActivity {
public static final String BASE_URL = "http://172.19.194.89:3000";
public static String response_string="default message";
private static ArrayList<HashMap<String,String>> buglist;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
buglist = new ArrayList<HashMap<String,String>>();
setContentView(R.layout.bugslist);
}
@Override
public void onStart(){
super.onStart();
// Send a HTTP GET Request to get bug details
String bugs=RestClient.getData("http://172.19.194.89:3000/bug");
//String count=RestClient.getData("http://172.19.194.89:3000/count");
Log.i("DEBUG","BUGS LIST:"+bugs);
parseBugs(bugs);
ListAdapter adapter = new SimpleAdapter(this, buglist, R.layout.bugrow,
new String[] {"summary","platform","product"},
new int[] {R.id.text1,R.id.text2, R.id.text3});
setListAdapter(adapter);
}
public void parseBugs(String bugstring){
try {
JSONObject jobject = new JSONObject(bugstring);
JSONArray bugdetails = jobject.getJSONArray("bugs");
int i=0;
HashMap<String, String> item = new HashMap<String, String>();
String item_val;
JSONObject e = new JSONObject();
while(i<bugdetails.length()){
e=bugdetails.getJSONObject(i);
item_val = e.getString("summary");
item.put("summary", item_val);
item_val = e.getString("platform");
item.put("platform", item_val);
item_val = e.getString("product");
item.put("product", item_val);
buglist.add(i, item);
i++;
}
Log.i("INFO", "The HashMap Array is populated");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
如果需要,我也可以提供意见。当我调试代码时,我能够看到bugslist变量设置正确,所以我假设问题可能与它在视图中访问变量的方式有关。
这些是观点
1.bugslist.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:id="@id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:drawSelectorOnTop="false">
</ListView>
<TextView android:id="@id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No data">
</TextView>
</LinearLayout>
2.bugrow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="@+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</TextView>
<TextView android:id="@+id/text2"
android:textSize="12sp"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
</TextView>
<TextView android:id="@+id/text3"
android:typeface="sans"
android:textSize="14sp"
android:textStyle="italic"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</TextView>
</LinearLayout>
由于 斯里纳
答案 0 :(得分:2)
因为您添加了相同的对象三次。 你应该在很久的时候创建一个新的hashmap实例。
public void parseBugs(String bugstring){
try {
JSONObject jobject = new JSONObject(bugstring);
JSONArray bugdetails = jobject.getJSONArray("bugs");
int i=0;
String item_val;
JSONObject e = new JSONObject();
while(i<bugdetails.length()){
HashMap<String, String> item = new HashMap<String, String>();
e=bugdetails.getJSONObject(i);
item_val = e.getString("summary");
item.put("summary", item_val);
item_val = e.getString("platform");
item.put("platform", item_val);
item_val = e.getString("product");
item.put("product", item_val);
buglist.add(i, item);
i++;
}
Log.i("INFO", "The HashMap Array is populated");
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}