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

标签 android mysql json listview

我从 Android 请求中得到了下面的 JSON 结果

[{"j_id":"1","j_title":"在线内容管理"},{"j_id":"2","j_title":"研究生开发人员"}]

我试图在每个 Listview 上同时显示 j_idj_title 列值,但我意识到 Listview 仅显示每个列表中的最后一条记录。

这是我输出的截图

enter image description here

这是我的 Activity 代码

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

}

感谢您的支持。

最佳答案

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

您的列表在每个位置都持有一个引用变量,因此在上一次迭代中,如果您更改该 HashMap 中的值,则列表中的每个元素都将具有相同的值,因为列表中的每个元素都指向单个 HashMap 引用

        // 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”?

关于android - 从 JSON 结果中选择每条记录,然后将其添加到 listview,Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40980823/

相关文章:

SQL Join 从左表和右表返回空行

android - 以编程方式创建具有轮廓样式的 MaterialButton

android - 只听到音轨播放时的咔哒声

android - android studio 1.4中content_main.xml的作用是什么?

mysql - GROUP和ORDER Mysql在列中获取不同的数据

ios - 尝试使用 swiftJson 解析 json 数组

Android ObjectAnimation 只启动了一次

php - 使用mysql数据库以php形式自动选择下拉列表

java - 具有任意 JSON 键的 Jackson ObjectMapper

javascript - Node 重新验证 : how to indent JSON output?