ListView仅显示ArrayList的最后一个元素(HashMap) [英] ListView displaying only the final element of a ArrayList(HashMap)

查看:176
本文介绍了ListView仅显示ArrayList的最后一个元素(HashMap)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

这是我的函数。 buglist视图中有一个ListView,而R.id.text1 / 2/3基本上是TextViews。

  public class GetBugs扩展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;


/ **当活动首次创建时调用。 * /
@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();

//发送一个HTTP GET请求来获取bug详细信息
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,HashMap Array is populated);

catch(JSONException e){
// TODO自动生成的catch块
e.printStackTrace();
}
}

如果需要,我也可以提供视图。当我调试代码时,我能够看到bugslist变量设置正确,所以我假设问题可能与它在视图中访问变量的方式有关。



这些是视图

1.bugslist.xml

  <?xml version =1.0encoding =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 =没有数据>
< / TextView>

< / LinearLayout>



2.bugrow.xml

 <?xml version =1.0encoding =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>

感谢
Sreenath

解决方案

因为你三次添加同一个对象。
你应该创建一个新的hashmap实例。

  public void parseBugs(String bugstring){
尝试{
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,HashMap Array is populated);

catch(JSONException e){
// TODO自动生成的catch块
e.printStackTrace();
}


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

解决方案

Because you add the same object three times. you should create a new hashmap instance very time.

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

这篇关于ListView仅显示ArrayList的最后一个元素(HashMap)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆