简体   繁体   English

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

[英]Android Custom ListView Checkbox Save State

I need to be able to save the state of the checkboxes. 我需要能够保存复选框的状态。 When the app the closes, it should save which checkboxes were checked and when the app comes back on, the checkboxes that were checked should be checked at startup. 当应用关闭时,它应保存选中了哪些复选框,当应用重新启动时,应在启动时选中已选中的复选框。 It uses a custom adapter listview. 它使用自定义适配器列表视图。 Thank You for your time. 感谢您的时间。

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


    }
}

You can use SharedPreferences to store states of your checkboxes for later use. 您可以使用SharedPreferences存储复选框的状态,以备后用。 However, you need to have identification number for each checkbox to do this. 但是,您需要为每个复选框都有标识号才能执行此操作。 Store it in this form: ".12.32.54.62." 以以下格式存储:“ .12.32.54.62”。 where 12, 32, 54, 62 are identification numbers of checked boxes. 其中12、32、54、62是复选框的标识号。 Reading and writing to SharedPreferences file may take some time, but I don't think it will affect your performance that much. 读取和写入SharedPreferences文件可能需要一些时间,但是我认为这不会对您的性能产​​生太大影响。 On the other hand, I think there is room for improvement in below code: 另一方面,我认为下面的代码还有改进的余地:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM