2011-08-04 72 views
1

我有一個活動類,通過查詢獲取JSON字符串。我把這個字符串解析成一個ArrayList(HashMap(String,String))。我傳遞給ListView。我面臨的問題是顯示ArrayList的元素時,只有列表中的最後一個元素顯示在UI中。即如果列表中有三個元素,則第三個元素顯示三次ListView只顯示ArrayList的最後一個元素(HashMap)

這是我的函數。 「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

是否在列表中列出了同樣的東西或在每個列表項中textview(text1/2/3)?另外 - 你可以發表意見嗎? – Jack

+0

是的,它的名單是bugslist.xml。我爲textview元素獲得了不同的值。 – n3o

回答

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(); 
} 
+0

工作。謝謝。 StackOverflow的岩石! – n3o