Easy to maintain. Get your pick from the model.
class PropertyModel{ String VAL= "SOME VALUE" boolean selected = false; public String getVAL() {return VAL;} public void setVAL(String VAL) {this.VAL = VAL;} public boolean isSelected() {return selected;} public void setSelected(boolean selected) {this.selected = selected;} }
Prepare your opinion as follows:
public class CheckBoxAdptr extends BaseAdapter{ public View getView(int position, View view, ViewGroup parent) { PropertyModel items = list.get(position); View row; if (view == null) { LayoutInflater inflater = (LayoutInflater) context .getSystemService(Context.LAYOUT_INFLATER_SERVICE); row = inflater.inflate(R.layout.list_item_checkbox, null); } else{row = view;} TextView T1 = (TextView) row.findViewById(R.id.list_item_1); CheckBox checkBox = (CheckBox) row.findViewById(R.id.list_item_check_box); T1.setText(""+items.getVAL()); if(items.isSelected()){ checkBox.setChecked(true); } else {checkBox.setChecked(false);} return row; } }
Now in your activity or fragment install the adapter and do something like this:
final ArrayList<PropertyModel> list = getList(); CheckBoxAdptr adpt = new CheckBoxAdptr(getActivity(), list); listview.setAdapter(adpt); listview.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) { for(int i = 0; i<list.size(); i++){ if(i!=position){ list.get(i).setSelected(false); } else{ list.get(i).setSelected(true); } } adpt.notifyDataSetChanged(); } });
Please note that all your work is done with adpt.notifyDataSetChanged() . Hope this helps.
Pranjal choladhara
source share