i am trying to get the items in the listview using json from server, for this i have made my custom adapter. but if there are 6 items on the server it displays the last item 6 times in the list view and i have given a checkbox infront of every item in the list to get the id of the checked item. here is my code:
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);
}
});
and my adapter is:
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;
}
}
please tell where i am doing wrong …
problem is in below line
you are putting
category_nameand id intolist_mapand adding same intolist_category. here you are adding list_map instance in list_category. and becuse of the key for map(which is category_name and id ) is same,it can store only one value corresponding to one key, hence it is storing last inserting value of these keys.
when you are updating list_map with new data the instance itself getting change), That is why it is showing 6 times of last list_map data.
Solution : what you can do here, initialize list_map in loop only with new keyword, so it will insert new instance every-time you insert into list_category. or you can use two d array. instead of hashmap.
P.S: I pointed out one problem in your code, which is causing duplicacy of data, there can be other problem also as suggested by other SO members.
Edit:
replace your below code
with something like below