我下面的作品码完善活动时负载与所有的复选框选中,或所有的复选框选中,并检查它们是否还是从这一点上不加以控制,但问题是,在现实中一些复选框检查活动可能加载。
我想关键是count = 0;
如果我可以count
在表单加载时设置复选框的数量......
这就是setOnClickListener(new View.OnClickListener() {
我的adapter
, 当所有复选框从选中或未选中的位置开始时的作用:
//we want to keep track of checked boxes
int count;
count = 0;
int size = MatchingContactsAsArrayList.size();
for (int i = 0; i < size; i++) {
if (theContactsList.get(i).isSelected) {
count++;
}
}
if (count == 0) {
Toast.makeText(context_type, "count is 0!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context_type, "The count is " + count, Toast.LENGTH_SHORT).show();
}
感谢上面的帮助,但是我找到了另一种简化过程中代码的方法。有用,因为我在许多活动中重复这个过程。
在我的活动中,我将已经选中的框sharedpreferences
从服务器中放入Gson
:
//Here, we PUT the arraylist into the sharedPreferences/*
SharedPreferences sharedPreferencescheckedContactsAsArrayList = PreferenceManager.getDefaultSharedPreferences(getApplication());
SharedPreferences.Editor editorcheckedContactsAsArrayList = sharedPreferencescheckedContactsAsArrayList.edit();
Gson gsoncheckedContactsAsArrayList = new Gson();
String jsoncheckedContactsAsArrayList = gsoncheckedContactsAsArrayList.toJson(checkedContactsAsArrayList);
editorcheckedContactsAsArrayList.putString("checkedContactsAsArrayList", jsoncheckedContactsAsArrayList);
editorcheckedContactsAsArrayList.commit();
在我的adapter
I GET 这些值中sharedpreferences
,创建一个名为的 arrayList checkedContactsAsArrayList
:
//we are fetching the array list checkedContactsAsArrayList, created in the activity. with this we will put a tick in the checkboxes of the selected numbers
SharedPreferences sharedPreferencescheckedContactsAsArrayList = PreferenceManager.getDefaultSharedPreferences(context_type);
Gson gsoncheckedContactsAsArrayList = new Gson();
String jsoncheckedContactsAsArrayList = sharedPreferencescheckedContactsAsArrayList.getString("checkedContactsAsArrayList", "");
Type type2 = new TypeToken<ArrayList<String>>() {
}.getType();
checkedContactsAsArrayList = gsoncheckedContactsAsArrayList.fromJson(jsoncheckedContactsAsArrayList, type2);
然后我的 onBindViewHolder
@Override
public void onBindViewHolder(final RecyclerView.ViewHolder viewHolder, final int position) {
etc...
holder.check.setOnClickListener(new View.OnClickListener() {
@Override
//on checkbox click
public void onClick(View v) {
//pos is the row number that the clicked checkbox exists in
Integer pos = (Integer) holder.check.getTag();
//if the checkbox is checked
if (holder.check.isChecked())
{
//we want to add the phone number of the checked row into our arraylist.
//add the checked number into the arraylist
checkedContactsAsArrayList.add(theContactsList.get(pos).getPhone());
} else {
//remove the checked number from the arraylist
checkedContactsAsArrayList.remove(theContactsList.get(pos).getPhone());
}
而且在 my 中onBindViewHolder
,不需要loops
或任何东西:
if (checkedContactsAsArrayList.size() == 0) {
Toast.makeText(context_type, "count is 0!", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context_type, "The count is " + checkedContactsAsArrayList.size(), Toast.LENGTH_SHORT).show();
}
谢谢!
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句