ListView 仅显示 ArrayList(HashMap)的最后一个元素
我有一个活动类,它通过查询获取 JSON 字符串。我将字符串解析为 ArrayList(HashMap(String, String))。我将其传递给 ListView。我面临的问题是,在显示 ArrayList 的元素时,UI 中仅显示列表中的最后一个元素。即如果列表中有 3 个元素,则第三个元素显示三次
这是我的函数。 “buglist”视图中有一个 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 变量设置正确,因此我认为问题可能在于其访问视图中变量的方式。
这些是视图
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>
谢谢 斯里纳斯
I have an activity class that gets a JSON string by making a query. I parsed the string into a ArrayList(HashMap(String, String)). I am passing this to a ListView. The problem I am facing is while displaying the elements of the ArrayList only the last element in the list is shown in the UI. i.e. if there are 3 elements in the list, the third element is shown thrice
This is my Function. The "buglist" view has a ListView in it and R.id.text1/2/3 are basically 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();
}
}
I can provide the views also if needed. When i debug the code, I was able to see that the bugslist variable is set correctly, so i assume the problem might be with the way its accessing the variable in the view.
These are the views
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>
Thanks
Sreenath
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
因为您将同一个对象添加了三次。
你应该随时创建一个新的 hashmap 实例。
Because you add the same object three times.
you should create a new hashmap instance very time.