Android自定义ListView复选框保存状态

时间:2017-07-14 16:31:27

标签: android

我需要能够保存复选框的状态。当应用程序关闭时,它应该保存选中的复选框,当应用程序重新启动时,应该在启动时检查已选中的复选框。它使用自定义适配器列表视图。谢谢你的时间。

public class OrderActivity extends AppCompatActivity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_order);

        getSupportActionBar().setTitle("Order");
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        ListView listview;
        CustomChoiceListViewAdapter adapter;
        adapter = new CustomChoiceListViewAdapter();
        listview = (ListView) findViewById(R.id.listview1);
        listview.setAdapter(adapter);

        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.a1), "1");
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.a2), "2");
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.a3), "3");
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.a4), "4");
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.a5), "5");
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.a6), "6");
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.a7), "7");
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.a8), "8");
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.a9), "9");
        adapter.addItem(ContextCompat.getDrawable(this, R.drawable.a10), "10");
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case android.R.id.home:
                finish();
                return true;
        }
        return super.onOptionsItemSelected(item);
    }
}

public class ListViewItem {
    private Drawable icon;
    private String text;

    public void setIcon(Drawable icon) {
        this.icon = icon;
    }

    public void setText(String text) {
        this.text = text;
    }

    public Drawable getIcon() {
        return this.icon;
    }

    public String getText() {
        return this.text;
    }
}

public class CustomChoiceListViewAdapter extends BaseAdapter{

    private ArrayList<ListViewItem> listViewItemList = new ArrayList<ListViewItem>() ;
    public CustomChoiceListViewAdapter() { }

    @Override
    public int getCount() {
        return listViewItemList.size() ;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        final int pos = position;
        final Context context = parent.getContext();

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.item_order, parent, false);
        }

        ImageView iconImageView = (ImageView) convertView.findViewById(R.id.imageview1) ;
        TextView textTextView = (TextView) convertView.findViewById(R.id.text_name) ;

        ListViewItem listViewItem = listViewItemList.get(position);

        iconImageView.setImageDrawable(listViewItem.getIcon());
        textTextView.setText(listViewItem.getText());

        return convertView;
    }

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


    @Override
    public Object getItem(int position) {
        return listViewItemList.get(position) ;
    }

    public void addItem(Drawable icon, String text) {
        ListViewItem item = new ListViewItem();

        item.setIcon(icon);
        item.setText(text);

        listViewItemList.add(item);
    }
}

public class CheckableLinearLayout extends LinearLayout implements Checkable {

    public CheckableLinearLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        // mIsChecked = false ;
    }

    @Override
    public boolean isChecked() {
        CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
        return cb.isChecked();
        // return mIsChecked ;
    }

    @Override
    public void toggle() {
        CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
        setChecked(cb.isChecked() ? false : true);
        // setChecked(mIsChecked ? false : true) ;
    }

    @Override
    public void setChecked(boolean checked) {
        CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);

        if (cb.isChecked() != checked) {
            cb.setChecked(checked);
        }


    }
}

1 个答案:

答案 0 :(得分:0)

您可以使用SharedPreferences存储复选框的状态以供日后使用。但是,您需要为每个复选框指定标识号才能执行此操作。以这种形式存储:&#34; .12.32.54.62。&#34;其中12,32,54,62是复选框的标识号。读取和写入SharedPreferences文件可能需要一些时间,但我认为它不会对您的性能产​​生太大影响。另一方面,我认为以下代码还有改进的余地:

public class CheckableLinearLayout extends LinearLayout implements Checkable {

String mId;
SharedPreferences mShPreferences;

public CheckableLinearLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    mShPreferences = context.getSharedPreferences(
            "com.yourpackagename.PREFERENCE_FILE_KEY", Context.MODE_PRIVATE);
}

@Override
public boolean isChecked() {
    CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
    String key = "." + mId + ".";
    return mShPreferences.getString("checkboxes", cb.isChecked() ? key : "").contains(key);
}

@Override
public void toggle() {
    CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);
    setChecked(!cb.isChecked());
}

@Override
public void setChecked(boolean checked) {
    CheckBox cb = (CheckBox) findViewById(R.id.checkBox1);

    if (cb.isChecked() != checked) {

        cb.setChecked(checked);

        String checkboxes = mShPreferences.getString("checkboxes", ".");

        SharedPreferences.Editor editor = mShPreferences.edit();

        String key = "." + mId + ".";

        if (checked && !checkboxes.contains(key))
            editor.putString("checkboxes", checkboxes + mId + ".");
        else if (!checked && checkboxes.contains(key))
            editor.putString("checkboxes", checkboxes.replace(key, "."));

        editor.apply();
    }
}

public void setId(String id) {
    this.mId = id;
}
}