if (v.getId() == R.id.contact_btnNext) {
StringBuilder id = new StringBuilder();
String prefix = "";
for (Datamodel datamodel : mAdapter.arrSelectedModel ) {
id.append(prefix).append(datamodel.getId());
prefix = ",";
}
Using this code I am getting value of all ListView
it's which is checked and unchecked in id:
public class AdapterContactAddfriend extends BaseAdapter {
private Context context;
private ArrayList<Datamodel> arrModel;
public ArrayList<Datamodel> arrSelectedModel;
public ArrayList<Boolean> arrCheckboxState;
ViewHolder holder;
ImageLoader imageloader;
public AdapterContactAddfriend(Context context,
ArrayList<Datamodel> arrModel) {
this.context = context;
this.arrModel = arrModel;
arrCheckboxState = new ArrayList<Boolean>();
arrSelectedModel = new ArrayList<Datamodel>();
imageloader = new ImageLoader(context);
for (int i = 0; i < arrModel.size(); i++) {
arrCheckboxState.add(false);
}
}
@Override
public int getCount() {
return arrModel.size();
}
@Override
public Object getItem(int position) {
return arrModel.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView = mInflater
.inflate(R.layout.row_contactaddfriend, null);
holder.checkBox = (CheckBox) convertView
.findViewById(R.id.rowcontact_checkbox);
convertView.setTag(holder);
} else
holder = (ViewHolder) convertView.getTag();
holder.txtContactName.setText(arrModel.get(position).contactName);
holder.checkBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
int position = (Integer) buttonView.getTag();
if (isChecked) {
arrCheckboxState.set(position, true);
arrModel.get(position).setChecked(true);
arrSelectedModel.add(arrModel.get(position));
} else {
arrModel.get(position).setChecked(false);
arrCheckboxState.set(position, false);
arrSelectedModel.remove(arrModel.get(position));
}
}
});
return convertView;
}
}
This is my adapter and I have taken boolean
value public ArrayList<Boolean> arrCheckboxState
for check and unchecked value but I am unable to get value of id
which is checked where am doing mistake please tell me because I am getting value of id of all list-view item but I have to get only those id which is selected.
Remove this line..because your are not setting position as tag..as position is already there in parameters why are you getting it from tag ?
int position = (Integer) buttonView.getTag();
Use it like this
holder.checkBox
.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(position<arrCheckboxState.size()){
if (isChecked) {
arrCheckboxState.set(position, true);
arrModel.get(position).setChecked(true);
arrSelectedModel.add(arrModel.get(position));
} else {
arrModel.get(position).setChecked(false);
arrCheckboxState.set(position, false);
arrSelectedModel.remove(arrModel.get(position));
}
}
}
});
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.