我已经制作了四个复选框。当我检查它们时,它应保存在ArrayList
中。我可以将它们保存到array list
吗?当我取消选择它们时,它们应该来自removed
或list
ArrayList
。我已经实现了checkboxes
,还有一个全选按钮来选择所有checkboxes
并且取消选择deselect
的一个checkboxes
按钮应该从中删除。
我的代码是
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"));
}
}
});
答案 0 :(得分:1)
使用以下代码将您的所有
checkBox
数据添加到ArrayList
:
ArrayList<String> arrayList = new ArrayList<>();
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("Select all");
//Adding data to ArrayList
List arrList = Arrays.asList(name);
arrayList.addAll(arrList);
arrayList.addAll(Arrays.asList(arr));
//or, Add data into ArrayList Manually
arrayList.add("Chicago");
arrayList.add("Los Angeles");
arrayList.add("Japan");
arrayList.add("Europe");
}
else {
abc.setChecked(false);
b.setChecked(false);
a.setChecked(false);
c.setChecked(false);
tv2.setText("Select all");
//Clear Data from ArrayList
if(arrayList.size()!=0){
arrayList.clear
}
}
}
});
使用以下代码选择&amp;删除:
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("Remove 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.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//is chkIos checked?
if (((CheckBox) v).isChecked()) {
arrayList.add("Chicago");
}
else
// removes Chicago by arrayList Position
//to avoid runtime exception
if(arrayList.contain("Chicago")){
arrayList.remove(0);
}else{
// do your stuff
}
}
});
答案 1 :(得分:1)
您正在尝试访问arrayList而不初始化它。使用以下代码
public class SearchFragment extends BaseFragment {
String[] name={ "Chicago", "Los Angeles", "Japan", "Europe"};
Object arr;
ArrayList<String> arrayList = new ArrayList<>();
TextView tv2,tv3,tv4,tv5,tv6,tv7;
CheckBox
abc,abc1,a,b,c,abc2,abc3,abc4,a1,a2,a3,a4,b1,b2,b3,b4,c1,c2,c3,c4;
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("Clear All");
arrayList.addAll(Arrays.asList(name));
}
else {
abc.setChecked(false);
b.setChecked(false);
a.setChecked(false);
c.setChecked(false);
tv2.setText("Select all");
arrayList.clear();
}
}
});
}}