Created
December 16, 2014 14:01
-
-
Save devrath/59b7b87f6041b4351952 to your computer and use it in GitHub Desktop.
Checkbox adapter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package com.windhyaworks.adapters; | |
| import java.util.List; | |
| import android.app.Activity; | |
| import android.content.Context; | |
| import android.view.LayoutInflater; | |
| import android.view.View; | |
| import android.view.ViewGroup; | |
| import android.widget.ArrayAdapter; | |
| import android.widget.CheckBox; | |
| import android.widget.CompoundButton; | |
| import com.windhyaworks.R; | |
| import com.windhyaworks.models.ModelFilterCategories; | |
| public class AdptSearchFilterCategories extends ArrayAdapter<ModelFilterCategories> { | |
| private Context context; | |
| List<ModelFilterCategories> mdlLst; | |
| public AdptSearchFilterCategories(Activity context, List<ModelFilterCategories> mdlLst) { | |
| super(context, R.layout.adpt_searchfilter_category, mdlLst); | |
| this.context = context; | |
| this.mdlLst = mdlLst; | |
| } | |
| static class ViewHolder { | |
| protected CheckBox chkBxCatId; | |
| } | |
| @Override | |
| public View getView(int position, View convertView, ViewGroup parent) { | |
| ViewHolder viewHolder = null; | |
| if (convertView == null) { | |
| LayoutInflater mInflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE); | |
| convertView = mInflater.inflate(R.layout.adpt_searchfilter_category, null); | |
| viewHolder = new ViewHolder(); | |
| viewHolder.chkBxCatId = (CheckBox) convertView.findViewById(R.id.chkBxCatId); | |
| viewHolder.chkBxCatId.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. | |
| mdlLst.get(getPosition).setCheckboxState(buttonView.isChecked()); // Set the value of checkbox to maintain its state. | |
| } | |
| }); | |
| convertView.setTag(viewHolder); | |
| } else { | |
| viewHolder = (ViewHolder) convertView.getTag(); | |
| } | |
| viewHolder.chkBxCatId.setTag(position); // This line is important. | |
| viewHolder.chkBxCatId.setText(mdlLst.get(position).getCategoryName()); | |
| viewHolder.chkBxCatId.setChecked(mdlLst.get(position).isCheckboxState()); | |
| return convertView; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment