我有一個listView,它包含一個複選框和兩個textViews,它們顯示兩個名爲stName和admissionNumber.On的ArrayList。更多Arraylist我擁有smsMobileNum。一些職位沒有smsMobileNum,所以我想禁用它們。它正在工作,但問題是當我滾動listView然後所有複選框變爲禁用。這裏是我的代碼如果該位置的項目爲空,則禁用特定複選框
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;
}
}
使用boolean.if布爾值是真然後設置複選框真別的false.And查布爾值false時產品空。 – Rims