繁体   English   中英

Android如何在sharedpreference中保存自定义复选框状态并在应用重启后获取

[英]android how to save custom checkbox state in sharedpreference and get it after app restart

我有所有安装应用程序的listview 我正在使用自定义适配器,现在的问题是,我要保存复选框状态,当我将复选框的状态保存在sharedpreferences中时,然后在应用程序重新启动时又打开我保存的listview复选框状态自动检查。 怎么做? 这是我的代码

public class Listadapter extends BaseAdapter {
    private Context mContext;
    private List<ApplicationInfo> mListAppInfo;
    private PackageManager mPackManager;
    private ArrayList<Boolean> checkList = new ArrayList<Boolean>();
    CheckBox checkBox;
    boolean index[];
    boolean[] itemChecked;
    public Listadapter(Context applicationContext, List<ApplicationInfo> installedApplication, PackageManager packageManager) {
        //super(applicationContext,textViewResourceId,installedApplication);
            super();
            this.mContext = applicationContext;
            this.mListAppInfo = installedApplication;
            index = new boolean[installedApplication.size()];
            this.mPackManager = packageManager;
            for (int i = 0; i < installedApplication.size(); i++) {
            checkList.add(false);
            itemChecked = new boolean[installedApplication.size()];
            }
    }
    private class ViewHolder {
        ImageView ivAppIcon;
        TextView tvAppName;
        TextView tvPkgName;
        CheckBox checkBox;
    }
    @Override
    public int getCount() {
        return mListAppInfo.size();
        //return ((null != mListAppInfo) ? mListAppInfo.size() : 0);
    }

    @Override
    public Object getItem(int position) {
       // index = new boolean[mListAppInfo.size()];
        return mListAppInfo.get(position);
    }

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

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        // get the selected entry

        final ViewHolder holder;

      //  LayoutInflater inflater = (LayoutInflater) mContext.getLayoutInflater();
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService( Context.LAYOUT_INFLATER_SERVICE );

        if (convertView == null) {
            convertView = inflater.inflate(R.layout.list_item, null);
            holder = new ViewHolder();
        // reference to convertView
            holder.tvAppName = (TextView) convertView
                    .findViewById(R.id.textView1);
            holder.tvPkgName = (TextView) convertView
                    .findViewById(R.id.textView);
            holder.checkBox = (CheckBox) convertView
                    .findViewById(R.id.checkBox1);
            holder.ivAppIcon = (ImageView) convertView
                    .findViewById(R.id.imageView);

            convertView.setTag(holder);
            // holder.ck1.setTag(packageList.get(position));

        }
             else {

                holder = (ViewHolder) convertView.getTag();
            }
        final ApplicationInfo entry = mListAppInfo.get(position);

        holder.ivAppIcon.setImageDrawable(entry.loadIcon(mPackManager));
        holder.tvAppName.setText(entry.loadLabel(mPackManager));
        holder.tvPkgName.setText(entry.packageName);
        holder.checkBox.setChecked(false);


        holder.checkBox.setChecked(false);

        if (itemChecked[position])
            holder.checkBox.setChecked(true);
        else
            holder.checkBox.setChecked(false);

        holder.checkBox.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                if (holder.checkBox.isChecked())
                    itemChecked[position] = true;
                else
                    itemChecked[position] = false;
            }
        });
        return convertView;
    }
}

我会在共享的首选项键中使用您的复选框“ position”:

    SharedPreferences sharedPreferences = getActivity().getSharedPreferences("yourSharedPref", 0);
    SharedPreferences.Editor editor = sharedPreferences.edit();

并像这样存储我的复选框的状态:

    if (holder.checkBox.isChecked())
        editor.putBoolean("CheckBoxState" + position, true);  
    else
        editor.putBoolean("CheckBoxState" + position, false); 

然后,您可以通过解析以下位置来恢复复选框状态:

    for(int i = 0 ; i < positionMax ; i++){
        sharedPreferences.getBoolean("CheckBoxState" + i);
        //Do what you want with this value.
    } 

holder.checkBox.setChecked()应该读取状态,例如。 来自开发人员文档:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
boolean defaultValue = getResources().getBoolean(R.string.default_value);
boolean isChecked = sharedPref.getBoolean(getString(R.string.saved_state), defaultValue);

在方法onClick()中,您可以编写状态:

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean(getString(R.string.saved_state), newState);
editor.commit();

检查文档

暂无
暂无

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

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