从JSON结果中选择每条记录,然后将其添加到listview,Android

时间:2016-12-05 18:23:33

标签: android mysql json listview

我在Android请求中有以下JSON结果

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

我尝试在每个j_id上显示j_titleListview列值,但我意识到Listview仅显示每个列表中的最后一条记录。

这是我输出的屏幕截图

enter image description here

这是我的活动代码

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

我在AsyncTask类中的doInBackground代码

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

}

感谢每一位支持者。

1 个答案:

答案 0 :(得分:2)

您每次使用相同的HashMap对象,并在旧值上覆盖新值,因此您需要在每次迭代中创建新的HashMap

您的列表在每个位置都持有一个引用变量,所以在最后一次迭代中,如果您更改该hashmap中的值,那么列表中的每个元素都将具有相同的值,因为列表中的每个元素都指向一个hashmap引用< / p>

        // 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"));
                }

了解更多详情

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