Android:多次获取listview中的最后一项

时间:2012-07-17 05:13:50

标签: android json checkbox android-listview httpclient

我正在尝试从服务器使用listview获取json中的项目,因为我已经制作了自定义适配器。但是如果服务器上有6个项目,它会在列表视图中显示最后一个项目6次,并且我已经在列表中的每个项目前面checkbox获取了已检查项目的ID。这是我的代码:

btnclub.setOnClickListener(new OnClickListener() {

        ArrayList<Integer> checkedList = new ArrayList<Integer>();

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            Dialog d = new Dialog(TennerTextActivity.this);
            d.setContentView(R.layout.slctevnt);

            ListView list = (ListView) d.findViewById(R.id.list_mulitple);
            HashMap<String, String> list_map = new HashMap<String, String>();

            // Initialize CATEGORY_LIST HERE
            try {
                client = new DefaultHttpClient();
                HttpGet get = new HttpGet(
                        "http://dhklashfgsdhgsdg");
                HttpResponse rp = client.execute(get);
                String result = EntityUtils.toString(rp.getEntity());
                System.out.println("----------------------- result: "
                        + result);

                result = "{\"root\": " + result + "}";
                JSONObject root = new JSONObject(result);
                JSONArray sessions = root.getJSONArray("root");

                for (int i = 0; i < sessions.length(); i++) {
                    HashMap<String, String> map2 = new HashMap<String, String>();
                    JSONObject e = sessions.getJSONObject(i);
                    list_map.put("category_name", e.getString("name"));
                    list_map.put("category_id", e.getString("id"));
                    list_category.add(list_map);

                }
            } catch (Exception e) {
                e.printStackTrace();
            }

            list.setAdapter(new MyAdapter());
            d.show();
            Button btndone = (Button) d.findViewById(R.id.button_multiple);
            btndone.setOnClickListener(new View.OnClickListener() {

                @Override
                public void onClick(View v) {
                    // TODO Auto-generated method stub
                    for(int i = 0 ; i < checkedList.size() ; i++) {
                        clubs = "," + String.valueOf(checkedList.get(i));
                    }
                    clubs = clubs.substring(1);
                    System.out.print(clubs);
                    Log.e("club", clubs);
                }
            });

我的适配器是:

class MyAdapter extends BaseAdapter {



            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return list_category.size();
            }

            @Override
            public Object getItem(int position) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public long getItemId(int position) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public View getView(final int position, View convertView,
                    ViewGroup parent) {

                if (convertView == null) {
                    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
                    convertView = inflater.inflate(R.layout.dialoglist,
                            null);
                    TextView item = (TextView) convertView
                            .findViewById(R.id.dialogitem);
                    item.setText(list_category.get(position).get(
                            "category_name"));


                }
                CheckBox check = (CheckBox)convertView.findViewById(R.id.checkBox_list);
                check.setOnCheckedChangeListener(new OnCheckedChangeListener() {

                    @Override
                    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                        // TODO Auto-generated method stub
                        if(isChecked) {
                            int temp = Integer.parseInt(list_category.get(position).get("category_id"));
                            checkedList.add(temp);
                            Object[] tempArray = checkedList.toArray();
                            Arrays.sort(tempArray);
                            checkedList.clear();
                            for(int i = 0 ; i < tempArray.length ; i++) {
                                checkedList.add((Integer) tempArray[i]);
                            }
                        }
                    }
                });

                return convertView;
            }

        }

请告诉我哪里做错了......

3 个答案:

答案 0 :(得分:1)

我认为你必须像这样改变循环

 for (int i = 0; i < sessions.length(); i++) {
                        HashMap<String, String> map2 = new HashMap<String, String>();
 HashMap<String, String> list_map = new HashMap<String, String>();

                        JSONObject e = sessions.getJSONObject(i);
                        list_map.put("category_name", e.getString("name"));
                        list_map.put("category_id", e.getString("id"));
                        list_category.add(list_map);

                    }

答案 1 :(得分:1)

问题位于以下行

                   list_map.put("category_name", e.getString("name"));
                    list_map.put("category_id", e.getString("id"));
                    list_category.add(list_map);

您将category_name和ID放入list_map并将其添加到list_category。在这里,您将在list_category中添加list_map实例。并且因为map的键(category_name和id)是相同的,

它只能存储一个对应一个键的值,因此它存储了这些键的最后插入值。

当您使用新数据更新list_map时,实例本身会发生变化),这就是为什么它显示最后一次list_map数据的6倍。

解决方案:您可以在此处执行操作,仅使用new关键字在循环中初始化list_map,因此每次插入list_category时都会插入新实例。或者你可以使用两个d数组。而不是hashmap。

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

P.S:我在您的代码中指出了一个问题,即导致数据重复,其他SO成员也会提出其他问题。

修改 替换以下代码

for(int i = 0 ; i < checkedList.size() ; i++) {
                        clubs = "," + String.valueOf(checkedList.get(i));
                    }
                    clubs = clubs.substring(1);

如下所示

   StringBuilder sb = new StringBuilder();
for(int i = 0 ; i < checkedList.size() ; i++) {
                       sb.add(String.valueOf(checkedList.get(i))+",");
                    }
                 sb.deleteCharAt(sb.lastIndexOf(","));

答案 2 :(得分:0)

像这样改变getView方法

if (convertView == null) 
{
    LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    convertView = inflater.inflate(R.layout.dialoglist,null);
}

TextView item = (TextView) convertView.findViewById(R.id.dialogitem);
item.setText(list_category.get(position).get("category_name"));