Skip to content

Instantly share code, notes, and snippets.

@devrath
Created June 30, 2015 12:39
Show Gist options
  • Select an option

  • Save devrath/4e6aff582826d771b7b1 to your computer and use it in GitHub Desktop.

Select an option

Save devrath/4e6aff582826d771b7b1 to your computer and use it in GitHub Desktop.
OnClick of button in listview row check/uncheck the checkbox corrospondingly
public class AdptMyorderDetail extends BaseAdapter {
ArrayList<SerialNumbers> mSerialNumbers;
private Context mContext = null;
public AdptMyorderDetail(Context context, ArrayList<SerialNumbers> serialNo) {
super();
mContext = context;
mSerialNumbers = serialNo;
}
public int getCount() {
return mSerialNumbers.size();
}
public SerialNumbers getItem(int position) {
return mSerialNumbers.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
//Logic: Other subsequent rows will be a regular listview
final ViewHolder vHolder;
if (convertView == null) {
LayoutInflater layout = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layout.inflate(R.layout.adp_act_myorder_detail, null);
vHolder = new ViewHolder(view);
vHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag(); // Here we get the position that we have set for the checkbox using setTag.
mSerialNumbers.get(getPosition).setIsChecked(buttonView.isChecked()); // Set the value of checkbox to model to maintain its state.
}
});
view.setTag(R.string.checkBoxTag, vHolder.checkBox);
view.setTag(vHolder);// Set the tag to recycle each row of listview
} else {
vHolder = (ViewHolder) view.getTag();// Get the row based on the tag
}
vHolder.btnReportId.setTag(mSerialNumbers.get(position).getSerial_num() + "");//Done to submit the reasons
vHolder.checkBox.setTag(position); // This line is important.
vHolder.checkBox.setChecked(mSerialNumbers.get(position).isChecked());//Retrieve the Checked/Unchecked property from model and set it again
vHolder.txtNameId.setText(mSerialNumbers.get(position).getProduct().getName());
vHolder.txtSlnoId.setText(mContext.getResources().getString(R.string.myOrderDetailSlnoTag) + " " + mSerialNumbers.get(position).getSerial_num() + "");
vHolder.btnReportId.setTag(vHolder.checkBox);// For the button Checkbox reference is stored as tag
vHolder.btnReportId.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox checkBox = (CheckBox) view.getTag();// Using the reference set to the checkbox as tag get the proper checkbox
if(checkBox.isChecked()==true){
checkBox.setChecked(false);
}else{
checkBox.setChecked(true);
}
}
});
return view;
}
class ViewHolder {
private TextView txtNameId, txtSlnoId;
private CheckBox checkBox;
private ButtonRectangle btnReportId;
public ViewHolder(View base) {
txtNameId = (TextView) base.findViewById(R.id.txtNameId);
txtSlnoId = (TextView) base.findViewById(R.id.txtSlnoId);
checkBox = (CheckBox) base.findViewById(R.id.checkBox);
btnReportId = (ButtonRectangle) base.findViewById(R.id.btnReportId);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment