我有一个奇怪的问题!我正在尝试使用复选框创建列表视图.在我的另一个线程中,我被告知我应该使用一个数组来跟踪被检查的行.我做到了,它工作得很好,但逻辑错了,我现在遇到另一个问题.
public View getView(final int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.row, null);
}
CheckBox checkbox = (CheckBox)v.findViewById(R.id.checkbox);
checkbox.setChecked(checked[position]);
final LinearLayout rowLayout = (LinearLayout) v.findViewById(R.id.individualRow);
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
rowLayout.setBackgroundColor(Color.GRAY);
checked[position] = false;
}
else
{
rowLayout.setBackgroundColor(Color.DKGRAY);
checked[position] = true;
}
}
});
}
Run Code Online (Sandbox Code Playgroud)
取消选中所有复选框后,它可以正常工作,即使我向下滚动并再次备份,但仍保留了我选中的复选框,但检查的数组未正确设置.基本上if测试应该是另一种方式!
if(isChecked)
{
rowLayout.setBackgroundColor(Color.GRAY);
checked[position] = true;
}
else
{
rowLayout.setBackgroundColor(Color.DKGRAY); …Run Code Online (Sandbox Code Playgroud)