1

I have this JSON result below from an Android request

[{"j_id":"1","j_title":"Online Content Management"},{"j_id":"2","j_title":"Graduate developer"}]

I am trying to display both the j_id and j_title columns values on each Listview but I realized that the Listview is only displaying the last record on every list.

This is the screenshot of my output

enter image description here

This is my Activity Code

txt1 = (TextView) findViewById(R.id.textView1);
txt2 = (TextView) findViewById(R.id.textView2);
listView = (ListView) findViewById(R.id.list);


list = new ArrayList<HashMap<String, String>>();

    String[] from={"j_id","j_title"};//string array
    int[]    to={R.id.textView3,R.id.textView4}; //int array of views id's

dataAdapter = new SimpleAdapter(this,list,R.layout.list_view_items,from,to);//Create object and set the parameters for simpleAdapter

// Assign adapter to ListView
listView.setAdapter(dataAdapter);

My doInBackground code in the AsyncTask class

@SuppressLint("NewApi")
protected String doInBackground(String... params) {

    HashMap<String, String> data = new HashMap<String, String>();

    // parse json data
        data.put("jobsbycategory", params[0]);
        String result = ruc.sendPostRequest(URL2, data);

         Log.v("Result value", result);
        // parse json data
        try {
            JSONArray jArray = new JSONArray(result);
            for (int i = 0; i < jArray.length(); i++) {
                JSONObject jsonObject = jArray.getJSONObject(i);
                    hashMap.put("j_id",jsonObject.getString("j_id"));
                    hashMap.put("j_title",jsonObject.getString("j_title")+"");
                    list.add(hashMap);//add the hashmap into arrayList

                // add interviewee name to arraylist
                //list2.add(jsonObject.getString("j_title"));
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }
        return result;
}
protected void onPostExecute(String s) {
    loading.dismiss();
    //list.add(hashMap);
    dataAdapter.notifyDataSetChanged();
    intent = getIntent();
    jobsbycategory = intent.getStringExtra("jobsbycategory");
    txt2.setText("Job Sector: " + " " + jobsbycategory );
    Toast.makeText(getApplicationContext(), jobsbycategory, Toast.LENGTH_SHORT).show();

}

Every support is appreciated.

Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68
Edwinfad
  • 515
  • 6
  • 14

1 Answers1

2

You are using same HashMap object every time and over-writing the new values upon old values so you need to create new HashMap in every iteration .

Your list is holding a single reference variable at every position so in last iteration , if you change the value in that hashmap then every element in the list will have same value since every element in the list pointing to a single hashmap reference

        // parse json data
            data.put("jobsbycategory", params[0]);
            String result = ruc.sendPostRequest(URL2, data);

             Log.v("Result value", result);
            // parse json data
            try {
                JSONArray jArray = new JSONArray(result);
                for (int i = 0; i < jArray.length(); i++) {

                        hashMap = new HashMap<String, String>>();
                      //^^^^^^^^^^ add this line 
                    JSONObject jsonObject = jArray.getJSONObject(i);
                        hashMap.put("j_id",jsonObject.getString("j_id"));
                        hashMap.put("j_title",jsonObject.getString("j_title")+"");
                        list.add(hashMap);//add the hashmap into arrayList

                    // add interviewee name to arraylist
                    //list2.add(jsonObject.getString("j_title"));
                }

For further details

Is Java “pass-by-reference” or “pass-by-value”?

Community
  • 1
  • 1
Pavneet_Singh
  • 36,884
  • 5
  • 53
  • 68