我正在尝试从服务器获取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;
}
}
请告诉我哪里做错了......