I have made four check boxes. when I check them true it should save in ArrayList
. Can I save them to the array list
? and also when I deselect them they should be removed
from the list
or ArrayList
.i have implemented the checkboxes
and also one select all button to select all checkboxes
and one deselect
button on deselect all the values of checkboxes
should be removed from it.
My code is
public class SearchFragment extends BaseFragment {
private static final String TAG = null;
String[] name={ "Chicago", "Los Angeles", "Japan", "Europe"};
Object arr;
ArrayList<String> arrayList = new ArrayList<>();
TextView tv2
CheckBox abc,abc1,a,b,c;
public static SearchFragment newInstance () {
return new SearchFragment();
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search, container, false);
tv2=(TextView) view.findViewById(R.id.tv2);
abc=(CheckBox) view.findViewById(R.id.abc);
a=(CheckBox) view.findViewById(R.id.a);
b=(CheckBox) view.findViewById(R.id.b);
c=(CheckBox) view.findViewById(R.id.c);
return view;
}
@Override
public void onViewCreated (View view, @Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ButterKnife.bind(this, view);
tv2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String value=tv2.getText().toString().trim();
if (value.equals("Select all")){
abc.setChecked(true);
a.setChecked(true);
b.setChecked(true);
c.setChecked(true);
tv2.setText("UnSelect all");
arrayList.addAll(Arrays.asList(name));
Log.e("SELECTED","SELECTED "+arrayList.size());
}
else {
abc.setChecked(false);
b.setChecked(false);
a.setChecked(false);
c.setChecked(false);
tv2.setText("Select all");
if (arrayList.size() != 0) {
arrayList.clear();
}
Log.e("NOT_SELECTED","NOT_SELECTED");
}
}
});
abc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked){
arrayList.addAll(Arrays.asList("Chicago"));
}
}
});