我有一个listView,其中包含一个复选框和两个textViews,它们显示两个名为stName和admissionNumber的arraylists。更多Arraylist我有smsMobileNum。一些职位没有smsMobileNum所以我想禁用它们。它正在工作,但问题是当我滚动listView然后所有checkBoxes被禁用。这是我的代码
public class SmsStudentNameListAdapter extends ArrayAdapter{
ArrayList<CheckModelClass> smsMobileNum,userIdList;
ArrayList<CheckModelClass> stName,admissionNumber;
ArrayList<String> updatedSmsNumber = new ArrayList<>(), updatedUserId = new ArrayList<>();
Context context;
BaseActivity activity;
LayoutInflater inflater;
public SmsStudentNameListAdapter(Context context, BaseActivity activity, ArrayList<CheckModelClass> stName, ArrayList<CheckModelClass> admissionNumber, ArrayList<CheckModelClass> smsMobileNum, ArrayList<CheckModelClass> userIdList, ArrayList<String> updatedSmsNumber, ArrayList<String> updatedUserId) {
super(context, R.layout.sms_studentlist,stName);
this.stName = stName;
this.admissionNumber = admissionNumber;
this.context = context;
this.activity = activity;
this.smsMobileNum = smsMobileNum;
this.userIdList = userIdList;
this.updatedSmsNumber = updatedSmsNumber;
this.updatedUserId = updatedUserId;
}
private static class ViewHolder {
CheckBox checkBox;
TextView studentName, admissionNum;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder = null;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.sms_studentlist, parent, false);
viewHolder = new ViewHolder();
viewHolder.studentName = (TextView) convertView.findViewById(R.id.text1);
viewHolder.admissionNum = (TextView) convertView.findViewById(R.id.text2);
viewHolder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
viewHolder.checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int getPosition = (Integer) buttonView.getTag();
stName.get(getPosition).setSelected(buttonView.isChecked());
if (isChecked) {
if (updatedUserId.contains(userIdList.get(getPosition).getName()) && updatedSmsNumber.contains(smsMobileNum.get(getPosition).getName())) {
} else {
updatedUserId.add(userIdList.get(getPosition).getName());
updatedSmsNumber.add(smsMobileNum.get(getPosition).getName());
}
} else {
updatedUserId.remove(userIdList.get(getPosition).getName());
updatedSmsNumber.remove(smsMobileNum.get(getPosition).getName());
}
}
});
convertView.setTag(viewHolder);
convertView.setTag(R.id.text1, viewHolder.studentName);
convertView.setTag(R.id.text2, viewHolder.admissionNum);
convertView.setTag(R.id.checkbox, viewHolder.checkBox);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.studentName.setText(stName.get(position).getName());
viewHolder.admissionNum.setText(admissionNumber.get(position).getName());
viewHolder.checkBox.setTag(position);
viewHolder.checkBox.setChecked(stName.get(position).isSelected());
if(!smsMobileNum.get(position).getName().equals("null")) {
viewHolder.checkBox.setChecked(stName.get(position).isSelected());
}
else{
viewHolder.checkBox.setEnabled(false);
}
return convertView;
}
}
答案 0 :(得分:0)
我想你可以试试这个:
// no relevant code
viewHolder.studentName.setText(stName.get(position).getName());
viewHolder.admissionNum.setText(admissionNumber.get(position).getName());
viewHolder.checkBox.setTag(position);
boolean isSelected = stName.get(position).isSelected();
String mobileNum = smsMobileNum.get(position).getName();
if(mobileNum.equals("null")) {
viewHolder.checkBox.setEnabled(false);
}
else{
viewHolder.checkBox.setEnabled(true);
viewHolder.checkBox.setChecked(isSelected);
}