1

如何修改此 imageCheckBoxAdapter 代码以在滚动时保持 checkBox 的状态(即所有选中的复选框即使在滚动后也应保持选中状态。此外,选中的变量需要存储在数组中)?

class imageCheckBoxAdapter extends ArrayAdapter<String>
{
private final Context context;
private final ArrayList<String> values;
private final Map< String, SmbFile> obj;
static ArrayList<Boolean> checks=new ArrayList<Boolean>();
public imageCheckBoxAdapter(Context context,ArrayList<String> values,Map< String, SmbFile>obj) 
{
    super(context, R.layout.row_checkbox, values);
    this.context = context;
    this.values = values;
    this.obj=obj;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = (LayoutInflater) context
        .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View rowView = inflater.inflate(R.layout.row_checkbox, parent, false);
    TextView textView = (TextView) rowView.findViewById(R.id.text1_check);
    textView.setText(values.get(position));
    ImageView imageView = (ImageView) rowView.findViewById(R.id.icon_image_check);
    try
    {
        if((obj.get(values.get(position)).isFile()))
        {
            imageView.setImageResource(R.drawable.view_file_icon);
        }
        else
        {
            imageView.setImageResource(R.drawable.view_folder_icon);
        }
    }
    catch (SmbException e) 
    {
        Toast.makeText(context,"Network error",Toast.LENGTH_SHORT).show();
        Log.d("id1", "error1");
        e.printStackTrace();
    }
    return rowView;
}
}

row_checkbox.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:padding="5dp" >
    <CheckBox
    android:id="@+id/checkBox1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:checked="false" />
 <ImageView
    android:id="@+id/icon_image_check"
    android:layout_width="50px"
    android:layout_height="50px"
    android:layout_marginLeft="5px"
    android:layout_marginRight="20px"
    android:layout_marginTop="5px"
    android:src="@drawable/view_file_icon" >
    </ImageView>
 <TextView
    android:id="@+id/text1_check"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@+id/label"
    android:textSize="30px" 
    android:typeface="sans">
    </TextView> 
</LinearLayout>
4

4 回答 4

5
class imageCheckBoxAdapter extends ArrayAdapter<String> implements View.onClickListener
{
    private final Context context;
    private final ArrayList<String> values;
    private final Map< String, SmbFile> obj;
    private ArrayList<Boolean> checks=new ArrayList<Boolean>();

    public imageCheckBoxAdapter(Context context,ArrayList<String> values,Map< String, SmbFile>obj) 
    {
        super(context, R.layout.row_checkbox, values);
        this.context = context;
        this.values = values;
        this.obj=obj;

        for (int i = 0; i < values.size(); i++) {
            checks.add(i, false);
        }
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View rowView = inflater.inflate(R.layout.row_checkbox, parent, false);
        TextView textView = (TextView) rowView.findViewById(R.id.text1_check);
        CheckBox chk = (CheckBox) rowView.findViewById(R.id.checkBox1);
        textView.setText(values.get(position));
        ImageView imageView = (ImageView) rowView.findViewById(R.id.icon_image_check);
        try
        {
            if((obj.get(values.get(position)).isFile()))
            {
                imageView.setImageResource(R.drawable.view_file_icon);
            }
            else
            {
                imageView.setImageResource(R.drawable.view_folder_icon);
            }
        }
        catch (SmbException e) 
        {
            Toast.makeText(context,"Network error",Toast.LENGTH_SHORT).show();
            Log.d("id1", "error1");
            e.printStackTrace();
        }

        chk.setTag(Integer.valueOf(position));

        // Set a listener for the checkbox
        chk.setOnClickListener(this);

        //Sets the state of CB, since we have the list of checked CB
        chk.setChecked(checks.get(position));

        return rowView;
    }

    @Override
    public void onClick(View view) {
        Integer index = (Integer)view.getTag();
        boolean state = checks.get(index.intValue());

        checks.set(index.intValue(), !state);
    }
}
于 2012-04-23T10:34:43.473 回答
0

我认为您需要将值存储在数组中。对于每个复选框,您设置一个 changeListener 来存储关联的复选框。

于 2012-04-18T17:05:08.897 回答
0

我最近读到,不知道在哪里,有人问过一个类似的案例。

如果我理解正确,这可能是因为CheckBox控件未在 中进行数据绑定DataRepeaterItem,并且在滚动复选框时会丢失其状态。

您可以尝试使用Viewstate来保持检查值。我会尝试找到那个帖子,它似乎信息丰富。

于 2012-04-18T17:27:21.710 回答
0

我认为您可以使用 View.setTag 方法在列表中保存复选框的位置,在 getView 中引用您的复选框,如下所示:

CheckBox chk = (CheckBox) rowView.findViewById(R.id.checkBox1);

因为 CheckBoxes 是 View 的间接子类,它也有一个属性 View.setTag 像这样:

chk.setTag(Integer.valueOf(position));

然后将 OnCheckedChangeListener 设置为您的 chk,例如:

chk.setOnCheckedChangeListener(new OnCheckedChangeListener {
    public void onCheckedChanged(CompoundButton buttonView, boolean state) {
           Integer int = (Integer)buttonView.getTag();

           checks.set(int.intValue(), !boolean)
    }
});

由于布尔数组列表大小与值大小相同,因此指定位置对应于复选框的确切位置

然后将此代码段添加到您的 getView 方法中:

if (checks.get(position)) {
  chk.setChecked(checks.get(position));
  // do some stuff if wanted..
} else {
  chk.setChecked(checks.get(position));
  // do some stuff if wanted..
}

我们做了这个片段,因为当 listview 向上或向下滚动时,会为看不见的行项目调用 getView。并更新行项目是否已检查。

于 2012-04-18T17:52:17.797 回答