1

スクロール時にcheckBoxのステータスを維持するために、このimageCheckBoxAdapterコードを変更する方法(つまり、チェックされているすべてのチェックボックスは、スクロール後もチェックされたままにする必要があります。また、チェックされた変数を配列に格納する必要があります)?

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;
}
}

行_チェックボックス.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

View.setTagメソッドを使用して、リスト内のチェックボックスの位置を保存できると思います。次のように、getViewでチェックボックスを参照します。

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

CheckBoxは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..
}

リストビューを上または下にスクロールすると、表示されていない行アイテムに対してgetViewが呼び出されるため、スニペットを作成しました。また、すでにチェックされているかどうかにかかわらず、行アイテムを更新します。

于 2012-04-18T17:52:17.797 に答える
0

値を配列に格納する必要があると思います。各 checkBox に対して、関連付けられた checkBox を格納するための changeListener を設定します。

于 2012-04-18T17:05:08.897 に答える
0

私は最近、誰かが尋ねた同様のケースを読みました。

私が正しく理解している場合、これは、CheckBoxコントロールが でデータバインドされておらずDataRepeaterItem、スクロールするとチェックボックスの状態が失われることが原因である可能性があります。

Viewstateチェックされた値を維持するために使用してみることができます。その投稿を見つけてみますが、参考になりました。

于 2012-04-18T17:27:21.710 に答える