android - ListView 仅显示 ArrayList(HashMap) 的最后一个元素

标签 android json listview arraylist hashmap

我有一个 Activity 类,它通过查询获取 JSON 字符串。我将字符串解析为 ArrayList(HashMap(String, String))。我将其传递给 ListView。我面临的问题是在显示 ArrayList 的元素时,UI 中仅显示列表中的最后一个元素。即如果列表中有 3 个元素,则第三个元素显示三次

这是我的功能。 “错误列表” View 中有一个 ListView,而 R.id.text1/2/3 基本上是 TextView。

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 变量设置正确,所以我认为问题可能在于它在 View 中访问变量的方式。

这些是观点

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>

谢谢 斯里纳特

最佳答案

因为你添加了同一个对象三次。 你应该马上创建一个新的 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();
}

关于android - ListView 仅显示 ArrayList(HashMap) 的最后一个元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6935350/

相关文章:

javascript - 在动态 AJAX 负载上存储支持 JSON 的最佳位置在哪里?

xcode - cocoa - 应用程序架构

listview - Flutter - 与子小部件交互时停止 ListView 滚动

android listview OpenGLRenderer 和纹理问题?

java - 如何在recyclerview android的 Intent 字符串数组中显示收入?

java - HashMap 中的 ArrayIndexOutOfBoundsException

javascript - jquery中如何获取json数据数组?

android - CustomListAdapter 出现错误 "Cannot resolve symbol ' listView'"

android - fragment 布局接管工具栏和 float 按钮

Android - 使用 Fragment 的多级 ListView