检查顶部框时,选中所有复选框

时间:2014-08-19 19:22:23

标签: android listview checkbox

在我的自定义列表视图中,我有多行,每行包含一个文本视图和一个复选框。最上面一行包含文本" All"和一个复选框。我需要的是,当选中该复选框时,列表视图中的所有其他复选框都会被选中/取消选中。

这是我的MyAdapter:

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

String[] values;
Boolean[] checkedStatus;

public MyAdapter(Context context, int resource, String[] values, Boolean[] checkedStatus) {
    super(context, resource, values);

    this.values = values;
    this.checkedStatus = checkedStatus;
}

@Override
public int getCount() {
    return values.length;
}

@Override
public String getItem(int position) {
    return values[position];
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = convertView;
    if(view == null){
        LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        view = inflater.inflate(R.layout.row_layout, null);
    }

    TextView textView = (TextView) view.findViewById(R.id.title);
    textView.setText(values[position]);

    final CheckBox box = (CheckBox) view.findViewById(R.id.chk);
    box.setTag(position);
    box.setOnCheckedChangeListener(this);
    box.setChecked(checkedStatus[position]);

    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            // on each click change the state of checkbox
            box.setChecked(!box.isChecked());
        }
    });

    return view;

}

@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    Integer index = (Integer) buttonView.getTag();
    checkedStatus[index] = isChecked;
    String key = buttonView.getTag().toString();

    SharedPreferences sharedPreferences=getContext().getSharedPreferences("status", Context.MODE_PRIVATE);
    SharedPreferences.Editor editor=sharedPreferences.edit();
    editor.putBoolean(key,isChecked);
    editor.apply();
    }
}

这是MyActivity3:

public class MyActivity3 extends Activity {
private TextView tv;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_my3);
    Button m = (Button) findViewById(R.id.button3);
    tv = (TextView) findViewById(R.id.textViewcat);
    Typeface typeface = Typeface.createFromAsset(getAssets(), "BebasNeue Bold.ttf");
    tv.setTypeface(typeface);

    String[] listArray = new String[] { "All", "Friends & Family", "Sports", "Outside", "At School", "Fitness", "Photography", "Food", "Beach", "Money" };
    SharedPreferences sharedPreferences = getSharedPreferences("status", MODE_PRIVATE);

    Boolean[] checkedStatus = new Boolean[listArray.length];
    for ( int index = 0; index < checkedStatus.length; index++)
        checkedStatus[index] = sharedPreferences.getBoolean(Integer.toString(index), false);

    ListView listView = (ListView) findViewById(R.id.listView);
    MyAdapter adapter = new MyAdapter(this, R.layout.row_layout, listArray, checkedStatus);
    listView.setAdapter(adapter);

}

1 个答案:

答案 0 :(得分:1)

这可以使用视图持有者进行优化

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

    String[] values;
    Boolean[] checkedStatus;

    public MyAdapter(Context context, int resource, String[] values, Boolean[] checkedStatus) {
        super(context, resource, values);

        this.values = values;
        this.checkedStatus = checkedStatus;
    }

    @Override
    public int getCount() {
        return values.length;
    }

    @Override
    public String getItem(int position) {
        return values[position];
    }

    @Override
    public long getItemId(int position) {
        return 0;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view = convertView;
        if(view == null){
            LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            view = inflater.inflate(R.layout.row_layout, null);
        }

        TextView textView = (TextView) view.findViewById(R.id.title);
        textView.setText(values[position]);

        final CheckBox box = (CheckBox) view.findViewById(R.id.chk);
        box.setTag(position);
        box.setOnCheckedChangeListener(null);
        box.setChecked(checkedStatus[position]);
        box.setOnCheckedChangeListener(this);

        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // on each click change the state of checkbox
                box.toggle();
            }
        });

        return view;

    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Integer index = (Integer) buttonView.getTag();
        String key = buttonView.getTag().toString();
        SharedPreferences sharedPreferences=getContext().getSharedPreferences("status", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor=sharedPreferences.edit();

        if(index == 0){//index of all
            for(int i = 0; i < checkedStatus.length; i++){
                checkedStatus[i] = isChecked;
                editor.putBoolean(String.valueOf(i),isChecked);
            }
            notifyDataSetChanged();
        } else {
            checkedStatus[index] = isChecked;
            editor.putBoolean(key,isChecked);
        }
        editor.apply();
    }
}