Java 如何在我的RecyclerView中增加复选框的数量?
如果在加载活动时选中了all复选框,或者未选中all复选框,并且从那时起它们被选中或未选中,那么下面的代码工作得非常完美,但问题是,实际上,在加载活动时可能选中了一些复选框 我假设键是Java 如何在我的RecyclerView中增加复选框的数量?,java,android,checkbox,Java,Android,Checkbox,如果在加载活动时选中了all复选框,或者未选中all复选框,并且从那时起它们被选中或未选中,那么下面的代码工作得非常完美,但问题是,实际上,在加载活动时可能选中了一些复选框 我假设键是count=0如果我可以将count设置为表单加载时的复选框数 这是我在适配器的setOnClickListener(新视图)中的设置。OnClickListener(){,适用于所有的复选框从选中或未选中的位置开始时: //we want to keep track of chec
count=0
如果我可以将count
设置为表单加载时的复选框数
这是我在适配器的setOnClickListener(新视图)中的设置。OnClickListener(){
,适用于所有的复选框从选中或未选中的位置开始时:
//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();
}
//我们要跟踪复选框
整数计数;
计数=0;
int size=matchingcontactsasaraylist.size()匹配;
对于(int i=0;i
试试这个..在您的recyclerView适配器中..使公共静态int计数变量..并在onBindViewHolder()中添加checkedChangeListener()
到您的复选框中
public static int count = 0; // field variable
@Override
public void onBindViewHolder(final ItemViewHolder holder, final int
position)
{
holder.cb.setOnCheckedChangeListener(new
CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
{
count ++;
Toast.makeText(context_type, "count is " + count,
Toast.LENGTH_SHORT).show();
}
else
{
count --;
Toast.makeText(context_type, "count is " + count,
Toast.LENGTH_SHORT).show();
}
}
});
}
您也可以检查所有已检查或未检查..像这样
在适配器中,不要忘记添加这些选项以记住选中的位置:-
使用以下选项获取所有视图并计数复选框:
int checked = 0;
for (int i = 0; i < rv.getChildCount(); i++) {
try {
CheckBox checkBox = rv.getChildAt(i).findViewById(R.id.checkbox);
if (checkBox.isChecked()) checked++;
} catch (NullPointerException e) {
e.printStackTrace();
}
}
Toast.makeText(this, "Checked: " + checked, Toast.LENGTH_SHORT).show();
int checked=0;
对于(int i=0;i
其中,rv
是您的回收视图,R.id.checkbox
是您的复选框id
它适合我,但请确保适配器已为recyclerview创建了视图。感谢上面的帮助,不过我找到了另一种方法来简化过程中的代码。这很有用,因为我正在许多活动中重复此过程
在我的活动中,我使用Gson
从服务器将已选中的复选框放入SharedReferences
:
//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
中,我从SharedReferences
获取这些值,创建一个名为CheckedContactsArrayList的arrayList
:
//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
中,不需要循环
或任何东西:
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();
}
谢谢!我已经更新了我的答案。请检查。你能用click listener发布你的全部代码吗。这将有助于理解问题。可能你没有减少计数器,这可能会导致问题。谢谢,但这不起作用。无论检查与否,继续获取“计数为0!”。此外,应用程序在滚动时崩溃,必须没有正确地回收复选框。添加这两种方法,以便recyclerview记住选中的位置。我假设你知道它…所以我不想提及,而且崩溃可能是其他问题。但是不管如何,添加这两种方法和测试…是的,getItemId丢失,稍后将尝试,谢谢。另外*进行检查ge listener位于onBindViewHolder()
//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);
@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());
}
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();
}