android onclick of button如何在sharedpreference中保存自定义复选框状态,并在app重启后获取它

时间:2016-07-04 11:31:12

标签: android checkbox

我有一个所有安装应用的列表视图。我现在使用自定义适配器问题是这样我想在用户退出应用程序时保存我的复选框状态(已选中或未选中),以便我可以在应用程序重新启动时重新加载此状态。这是我的代码

hover

这是我的主要活动代码

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

}

在我的主动活动中,当我选中复选框并单击按钮时,我有一个我想要的按钮,保存所有复选框状态并在活动打开时重新加载状态。

2 个答案:

答案 0 :(得分:0)

请尝试使用此

SharedPreferences preferences = getSharedPreferences(getString("YOUR_APP_NAME"), MODE_PRIVATE);
SharedPreferences.Editor edit = preferences.edit();
edit.putBoolean("checkbox_" + CHECKBOX_INDEX, CHECKBOX_IS_CHECKED);
edit.commit();

/* ----- after ----- */
SharedPreferences preferences = getSharedPreferences(getString("YOUR_APP_NAME"), MODE_PRIVATE);
preferences.getBoolean("checkbox_" + CHECKBOX_INDEX, false);

我认为tutorial会帮助你提高你的安卓技能

将方法添加到Listadapter

public void setItemChecked(boolean[] items) {
   itemChecked = items;
}

并使用此代码从您的MainActivity中调用此方法

boolean[] items = new boolean[Utilities.getInstalledApplication(this).size()];
for (int i = 0; i < Utilities.getInstalledApplication(this).size(); ++i) {
    items[i] = preferences.getBoolean("checkbox_" + i,false);
}

之后

Listadapter Adapter = new Listadapter(this, Utilities.getInstalledApplication(this), packageManager);

称之为

Adapter.setItemChecked(items);

答案 1 :(得分:0)

复选框的监听器是:

checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

        }
    });