我在列表视图中有一个对话框中的复选框。现在我只需要从对话框中获取更新的复选框项,然后点击“确认”。例如,如果用户只检查行中的一个复选框,我只需要获取该状态,但如果用户选中并取消选中该复选框,则我不想要该状态。我尝试使用哈希表来获取和删除状态,但是在列表视图滚动时,复选框事件被执行并且哈希表填充了错误的值。
public class SelectGroupAdaptor extends BaseAdapter {
Activity activity;
ArrayList<SelectGroupItems> data = new ArrayList<>();
Hashtable<Integer,Boolean> status;
public SelectGroupAdaptor(Activity activity) {
this.activity = activity;
}
public void add(SelectGroupItems selectGroupItems) {
this.data.add(selectGroupItems);
notifyDataSetChanged();
status=new Hashtable<>();
}
public ArrayList<SelectGroupItems> getCheckedData () {
return data;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
TextView textView;
CheckBox checkBox;
if (convertView == null) {
convertView = LayoutInflater.from(activity).inflate(R.layout.layout_select_group_row, null);
}
textView = (TextView) convertView.findViewById(R.id.label);
textView.setTypeface(Util.setCustomFont(activity));
checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
textView.setText(data.get(position).getGroupName());
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
data.get(position).setChecked(isChecked);
if(status!=null)
{
if(status.get(position)!=null)
{
status.remove(position);
}else
{
status.put(position,isChecked);
}
}
}
});
checkBox.setChecked((data.get(position).isChecked()));
return convertView;
}
public Hashtable<Integer,Boolean> getStatus()
{
return status;
}
}
任何人都可以帮助我
答案 0 :(得分:1)
您不需要HashMap,一个带状态属性的简单项目持有者类可以帮助您。查看修改后的代码:
编辑:如果我理解正确,您希望在检查状态发生变化时获取SelectGroupItems
对象?如果是这样,请参阅下面的编辑代码:
public class SelectGroupAdaptor extends BaseAdapter {
Activity activity;
ArrayList<SelectGroupItemHolder> data = new ArrayList<SelectGroupItemHolder>();
OnItemCheckStatusChangedListener onItemCheckStatusChangedListener;
// a item holder class, not a view holder
private static class SelectGroupItemHolder {
SelectGroupItems selectGroupItems;
boolean status;
}
public interface OnItemCheckStatusChangedListener {
public void onItemCheckStatusChanged(SelectGroupItems selectGroupItems, boolean checkStatus, int position);
}
public void setOnItemCheckStatusChangedListener(OnItemCheckStatusChangedListener onItemCheckStatusChangedListener) {
this.onItemCheckStatusChangedListener = onItemCheckStatusChangedListener;
}
public SelectGroupAdaptor(Activity activity) {
this.activity = activity;
}
public void add(SelectGroupItems selectGroupItems) {
SelectGroupItemHolder selectGroupItemHolder = new SelectGroupItemHolder();
selectGroupItemHolder.selectGroupItems = selectGroupItems;
this.data.add(selectGroupItemHolder);
notifyDataSetChanged();
}
public ArrayList<SelectGroupItems> getCheckedData() {
ArrayList<SelectGroupItems> checkedItemList = new ArrayList<SelectGroupItems>();
for (SelectGroupItemHolder selectGroupItemHolder : data) {
if (selectGroupItemHolder.status) {
checkedItemList.add(selectGroupItemHolder.selectGroupItems);
}
}
return checkedItemList;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
TextView textView;
CheckBox checkBox;
if (convertView == null) {
convertView = LayoutInflater.from(activity).inflate(R.layout.layout_select_group_row, null);
}
textView = (TextView) convertView.findViewById(R.id.label);
textView.setTypeface(Util.setCustomFont(activity));
checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);
textView.setText(data.get(position).selectGroupItems.getGroupName());
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
data.get(position).status = isChecked;
if(onItemCheckStatusChangedListener != null) {
onItemCheckStatusChangedListener.onItemCheckStatusChanged(data.get(position).selectGroupItems, data.get(position).status, position);
}
}
});
checkBox.setChecked((data.get(position).status));
return convertView;
}
@Override
public int getCount() {
return data.size();
}
@Override
public Object getItem(int position) {
return null;
}
@Override
public long getItemId(int position) {
return 0;
}
}
设置selectGroupAdapter实例的检查状态更改侦听器。即:
selectGroupAdaptor.setOnItemCheckStatusChangedListener(new SelectGroupAdaptor.OnItemCheckStatusChangedListener() {
@Override
public void onItemCheckStatusChanged(SelectGroupItems selectGroupItems, boolean checkStatus, int position) {
// handle check status change
}
});