gpt4 book ai didi

java - ListView 内的复选框状态在 Android 中不能按相反顺序工作

转载 作者:行者123 更新时间:2023-12-01 13:39:31 25 4
gpt4 key购买 nike

Romain Guy's answer 的帮助下,我准备了 ListView ,其中每一行包含一个复选框和一个文本框和 Raghunandan's answer

如果我从上到下顺序检查每个复选框(即显示每一行数据),它工作正常,但如果我检查最后一个及其前一个,它只显示空格。

我的两门课:

显示当前状态.java

private AlertDialog leaveDialog(View view, int icon, String title,
final String[] data) {
final FilterAdapter mAdapter = new FilterAdapter(ShowCurrentStatus.this,R.layout.customleavetypefilter, data);
AlertDialog dialog;
AlertDialog.Builder builder = new AlertDialog.Builder(
ShowCurrentStatus.this);
builder.setIcon(icon);
builder.setTitle(title);
builder.setView(view);
ListView mListView = (ListView) view
.findViewById(R.id.listViewleavetypefilter);
mListView.setCacheColorHint(Color.TRANSPARENT);
mListView.setAdapter(mAdapter);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {
StringBuilder result = new StringBuilder();
for (int i = 0; i < mAdapter.mCheckStates.size(); i++) {
if (mAdapter.mCheckStates.get(i) == true) {

result.append(data[i]);
result.append("\n");
} else {
result.append("hi");
}

}
Toast.makeText(ShowCurrentStatus.this, result, Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {

@Override
public void onClick(DialogInterface dialog, int which) {

}
});

dialog = builder.create();
return dialog;
}

FilterAdapter.java

public class FilterAdapter extends ArrayAdapter<String> implements  CompoundButton.OnCheckedChangeListener {

SparseBooleanArray mCheckStates;
private String[] mData;
private Context mContext;
int layoutResourceId;

public FilterAdapter(Context mContext,int layoutResourceId, String[] mData) {
super(mContext, layoutResourceId, mData);
this.layoutResourceId = layoutResourceId;
this.mContext = mContext;
this.mData = mData;
mCheckStates = new SparseBooleanArray(mData.length);

}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
MyViewHolder mMyViewHolder;

if (view == null) {
LayoutInflater inflater = ((Activity)mContext).getLayoutInflater();
view = inflater.inflate(layoutResourceId, parent, false);
mMyViewHolder = new MyViewHolder(view);
view.setTag(mMyViewHolder);
} else {

mMyViewHolder = (MyViewHolder) view.getTag();
}
mMyViewHolder.name.setText(mData[position]);
mMyViewHolder.name.setTextColor(Color.CYAN);

mMyViewHolder.check.setTag(position);
mMyViewHolder.check.setChecked(mCheckStates.get(position, false));

mMyViewHolder.check.setOnCheckedChangeListener(this);
return view;
}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mCheckStates.put((Integer) buttonView.getTag(), isChecked);
}

private class MyViewHolder {
TextView name;
CheckBox check;

MyViewHolder(View mView) {
name = (TextView) mView
.findViewById(R.id.textViewAddDataSelectMultiple);
check = (CheckBox) mView.findViewById(R.id.checkBoxSelectMultiple);
}
}

public boolean isChecked(int position) {
return mCheckStates.get(position, false);
}

public void setChecked(int position, boolean isChecked) {
mCheckStates.put(position, isChecked);
notifyDataSetChanged();

}

public void toggle(int position) {
setChecked(position, !isChecked(position));

}

如果我从下到上顺序检查,mAdapter.mCheckStates.get(i) == true 中的 else 部分(即“hi”)总是执行。

有人可以告诉我我哪里做错了吗?

谢谢

最佳答案

改变你的

for (int i = 0; i < mAdapter.mCheckStates.size(); i++) {

for (int i = 0; i < data.length; i++) {

关于java - ListView 内的复选框状态在 Android 中不能按相反顺序工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20948025/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com