更新时间:2022-10-21 09:23:25
您可以尝试以下操作:
我希望这个步骤的帮助。
I have a list, in which each row contains a checkbox and a textview, also I have a button which should check/uncheck all checkboxes in the list, however, I have not been able to achieve this with the checkboxes OUTSIDE of the screen, only with the visibles one. And if I understood well, that happens because items outside of the screens dont exist, they are recycled to create the new rows while scrolling, causing null pointers/out of index errores while trying to check/access the checkboxes outside of the screen.
I Googled for solutions but nothing worked so far.
How can I solve this?
Here is the adapter:
public class AlumnoArrayAdapter<T> extends BaseAdapter{
Context mContext;
LayoutInflater mInflater;
ArrayList<T> mList;
ArrayList<Alumno> alumno;
boolean verEstadisticas;
SparseBooleanArray mSparseBooleanArray;
public AlumnoArrayAdapter(Context context, ArrayList<T> list, ArrayList<Alumno> alumno, boolean verEstadisticas) {
//TODO Auto-generated constructor stub
this.alumno = alumno;
this.verEstadisticas = verEstadisticas;
this.mContext = context;
mInflater = LayoutInflater.from(mContext);
mSparseBooleanArray = new SparseBooleanArray();
mList = new ArrayList<T>();
this.mList = list;
}
public ArrayList<T> getCheckedItems() {
ArrayList<T> mTempArry = new ArrayList<T>();
for(int i=0;i<mList.size();i++) {
if(mSparseBooleanArray.get(i)) {
mTempArry.add(mList.get(i));
}
}
return mTempArry;
}
public int getCount() {
return mList.size();
}
public Object getItem(int position) {
return mList.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
}
TextView tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
tvTitle.setText(mList.get(position).toString());
CheckBox mCheckBox = (CheckBox) convertView.findViewById(R.id.chkEnable);
mCheckBox.setTag((Integer) position);
mCheckBox.setChecked(mSparseBooleanArray.get(position));
mCheckBox.setOnCheckedChangeListener(mCheckedChangeListener);
if(verEstadisticas){
mCheckBox.setVisibility(View.GONE);
}
mCheckBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
Integer position = (Integer) cb.getTag();
alumno.get(position).setChecked(cb.isChecked());
}
});
return convertView;
}
OnCheckedChangeListener mCheckedChangeListener = new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mSparseBooleanArray.put((Integer) buttonView.getTag(), isChecked);
}
};
}
If more code is needed please let me know.
Would appreciate any help possible.
Thanks in advance!
EDIT: Got a solution!, the modified adapter:
public class AlumnoArrayAdapter<T> extends BaseAdapter{
Context mContext;
LayoutInflater mInflater;
ArrayList<T> mList;
ArrayList<Alumno> alumno;
boolean verEstadisticas;
SparseBooleanArray mSparseBooleanArray;
boolean checarTodo = false;
public AlumnoArrayAdapter(Context context, ArrayList<T> list, ArrayList<Alumno> alumno, boolean verEstadisticas) {
//TODO Auto-generated constructor stub
this.alumno = alumno;
this.verEstadisticas = verEstadisticas;
this.mContext = context;
mInflater = LayoutInflater.from(mContext);
mSparseBooleanArray = new SparseBooleanArray();
mList = new ArrayList<T>();
this.mList = list;
}
public ArrayList<T> getCheckedItems() {
ArrayList<T> mTempArry = new ArrayList<T>();
for(int i=0;i<mList.size();i++) {
if(mSparseBooleanArray.get(i)) {
mTempArry.add(mList.get(i));
}
}
return mTempArry;
}
public int getCount() {
return mList.size();
}
public Object getItem(int position) {
return mList.get(position);
}
public long getItemId(int position) {
return position;
}
public void checkAll(){
for(int x = 0; x < alumno.size(); x++){
alumno.get(x).setChecked(!alumno.get(x).isChecked());
}
}
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null) {
convertView = mInflater.inflate(R.layout.row, null);
}
TextView tvTitle = (TextView) convertView.findViewById(R.id.tvTitle);
tvTitle.setText(mList.get(position).toString());
CheckBox mCheckBox = (CheckBox) convertView.findViewById(R.id.chkEnable);
mCheckBox.setTag((Integer) position);
mCheckBox.setChecked(alumno.get(position).isChecked());
if(verEstadisticas){
mCheckBox.setVisibility(View.GONE);
}
mCheckBox.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
//Alumno Alumno = (Alumno) cb.getTag();
Integer position = (Integer) cb.getTag();
alumno.get(position).setChecked(cb.isChecked());
}
});
return convertView;
}
}
You can try following:
I hope this steps help.