ListView displaying only the final element of a ArrayList(HashMap)
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 Arr开发者_JAVA技巧ayList<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();
}
精彩评论