我知道有多个问题与我的问题类似,但没有答案解决了我的特殊问题。
我有一个BaseAdapter,它准备一个带复选框的列表。
以下是适配器的相关代码 -
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
selectedFolderIds = new ArrayList<>();
selectedFolderNames = new ArrayList<>();
selectedFolderParentIds = new ArrayList<>();
deselectedFolderIds = new ArrayList<>();
final FolderStructureDataModel folderDetails = (FolderStructureDataModel) getItem(position);
final ViewHolder holder;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(convertView == null) {
convertView = inflater.inflate(R.layout.folder_structure_list_item_template,
parent, false);
holder = new ViewHolder();
holder.folderListLayout = (RelativeLayout) convertView.findViewById(R.id.folder_list_layout);
holder.childFoldersIndicator = (ImageView)
convertView.findViewById(R.id.child_folders_indicator);
holder.selectionCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
holder.selectionCheckBox.setOnCheckedChangeListener(null);
holder.selectionCheckBox.setOnClickListener(null);
}
/**
* Attach the listeners to the checkboxes.
*/
holder.selectionCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
CheckBox cb = (CheckBox) view;
if (cb.isChecked()) {
selectedFolderIds.add(HelperFunctions.EncodeString(folderDetails.ID));
selectedFolderNames.add(HelperFunctions.EncodeString(folderDetails.DisplayName));
selectedFolderParentIds.add(HelperFunctions.EncodeString(folderDetails.ParentFolderID));
SelectedFolderListener selectedFolderListener =
(SelectedFolderListener) context;
selectedFolderListener.getSelectedFoldersArrayList(selectedFolderIds,
selectedFolderNames, selectedFolderParentIds);
Log.i("Test : " + folderDetails.DisplayName + " at position " +
String.valueOf(position), "Set to " +
String.valueOf(checkboxStatus.get(position))
+ " From onClickListener");
} else {
selectedFolderIds.remove(HelperFunctions.EncodeString(folderDetails.ID));
selectedFolderNames.remove(HelperFunctions
.EncodeString(folderDetails.DisplayName));
selectedFolderParentIds.remove(HelperFunctions
.EncodeString(folderDetails.ParentFolderID));
deselectedFolderIds.add(HelperFunctions.EncodeString(folderDetails.ID));
UnselectedFolderListener unselectedFolderListener =
(UnselectedFolderListener) context;
unselectedFolderListener.getDeselectedFolderArrayList(deselectedFolderIds);
Log.i("Test : " + folderDetails.DisplayName + " at position " +
String.valueOf(position), "Set to " +
String.valueOf(checkboxStatus.get(position))
+ " From onClickListener");
}
}
});
holder.selectionCheckBox.setOnCheckedChangeListener
(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (((buttonView).isChecked()) && isChecked) {
checkboxStatus.set(position, true);
Log.i("Test : " + folderDetails.DisplayName + " at position " +
String.valueOf(position), "Set to " +
String.valueOf(checkboxStatus.get(position))
+ " From onCheckChangedListener");
} else {
checkboxStatus.set(position, false);
Log.i("Test : " + folderDetails.DisplayName + " at position " +
String.valueOf(position), "Set to " +
String.valueOf(checkboxStatus.get(position))
+ " From onCheckChangedListener");
}
}
});
//Populate the data
holder.selectionCheckBox.setText(String.valueOf(folderDetails.DisplayName));
if(Integer.valueOf(folderDetails.ChildFolderCount) == 0) {
holder.childFoldersIndicator.setVisibility(View.GONE);
holder.folderListLayout.setFocusable(true);
holder.folderListLayout.setClickable(true);
} else {
holder.childFoldersIndicator.setVisibility(View.VISIBLE);
holder.folderListLayout.setFocusable(false);
holder.folderListLayout.setClickable(false);
}
/**
* Set the checkbox as per the service reply (status in the server)
*/
if(Boolean.valueOf(folderDetails.IsAlreadySelected)) {
holder.selectionCheckBox.setChecked(true);
Log.i("Test : " + folderDetails.DisplayName + " at position " +
String.valueOf(position), "Set to " +
String.valueOf(holder.selectionCheckBox.isChecked()) + " From folderDetails");
} else {
holder.selectionCheckBox.setChecked(false);
Log.i("Test : " + folderDetails.DisplayName + " at position " +
String.valueOf(position), "Set to " +
String.valueOf(holder.selectionCheckBox.isChecked()) + " From folderDetails");
}
/**
* Set the checkbox as per the values maintained in the checkboxStatus ArrayList
*/
if(checkboxStatus.get(position)) {
holder.selectionCheckBox.setChecked(true);
Log.i("Test : " + folderDetails.DisplayName + " at position " +
String.valueOf(position), "Set to " +
String.valueOf(holder.selectionCheckBox.isChecked()) + " From checkBoxStatus");
} else {
holder.selectionCheckBox.setChecked(false);
Log.i("Test : " + folderDetails.DisplayName + " at position " +
String.valueOf(position), "Set to " +
String.valueOf(holder.selectionCheckBox.isChecked()) + " From checkBoxStatus");
}
return convertView;
}
我有一个ArrayList
checkboxStatus
,用于维护每个位置的复选框状态。
我面临的问题是,如果已经选中了一个复选框(根据'folderDetails.IsAlreadySelected'的值),我取消选中它,然后向下滚动,然后向上滚动,再次选中复选框。特殊的部分是在相反的情况下不会发生这种情况 - 如果未选中该复选框,我会检查它并向下滚动然后再向上滚动;状态仍为已检查状态。
适配器可能出现什么问题?
答案 0 :(得分:2)
要解决您的问题,请执行以下操作:
if(convertView == null) {
convertView = inflater.inflate(R.layout.folder_structure_list_item_template,
parent, false);
holder = new ViewHolder();
holder.folderListLayout = (RelativeLayout) convertView.findViewById(R.id.folder_list_layout);
holder.childFoldersIndicator = (ImageView)
convertView.findViewById(R.id.child_folders_indicator);
holder.selectionCheckBox = (CheckBox) convertView.findViewById(R.id.checkbox);
// Add this line, set tag on your checkbox.
holder.selectionCheckBox .setTag(holder);
convertView.setTag(holder);
}
现在点击列表,
holder.selectionCheckBox.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
// add this line,declare holder_new as an instance of ViewHolder
holder_new= (ViewHolder) view.getTag();
// rest is your code what you have already written
});
现在,在onclick之后,无论您使用holder访问selectionCheckBox,都将其更改为holder_new。
holder.selectionCheckBox.setChecked(真); 将其更改为 holder_new.selectionCheckBox.setChecked(true);