如何根据数据库状态启用和禁用switchCompat?

时间:2017-07-25 10:17:55

标签: android

我有一个应用程序,其中我有一个listview,其中一些数据存储在sqlite数据库中。 Listview还包含switchCompat,当我在listview的索引处启用开关然后将状态设置为" 0"当我禁用项目然后设置为" 1"。但是当状态设置为" 0"它启用swicth并在状态设置为toi" 1"它禁用开关。请帮助。

代码: -

  listViewHolder.switchCompat.setOnCheckedChangeListener(null);
    boolean isCheck;
    isCheck = !listStorage.get(position).getStatus().equalsIgnoreCase("0");

    listViewHolder.switchCompat.setChecked(isCheck);

    listViewHolder.switchCompat.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
        @Override
        public void onCheckedChanged(final CompoundButton buttonView, final boolean isChecked) {
            if (isChecked) {
                new AlertDialog.Builder(mContext, R.style.AppCompatAlertDialogStyle).setTitle("Warning").setMessage("You want to whiteList this application?").setPositiveButton("YES", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int pos) {
                        //Adding items in Dataset
                        CBlackListModel appList = listStorage.get(position);
                        String appName = appList.getName();
                        String appPackage = appList.getPackName();
                        byte[] appIcon = appList.getImage();
                        String appStatus = "1";

                        appListDataBase.updateApp(appName,appPackage,appIcon,appStatus);

                        notifyDataSetChanged();
                        listViewHolder.switchCompat.setChecked(isChecked);

                    }
                }).setNegativeButton("No", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        notifyDataSetChanged();
                        listViewHolder.switchCompat.setChecked(isChecked);
                    }
                }).show();

            } else {
                CBlackListModel model = listStorage.get(position);
                String appName = model.getName();
                String appPackage = model.getPackName();
                byte[] appIcon = model.getImage();
                String appStatus = "1";

                appListDataBase.updateApp(appName,appPackage,appIcon,appStatus);
                notifyDataSetChanged();
                listViewHolder.switchCompat.setChecked(false);

                Toast.makeText(mContext, "Removed", Toast.LENGTH_LONG).show();

            }
        }


    });

1 个答案:

答案 0 :(得分:0)

如果(isCheck.equals( “0”)){

listViewHolder.switchCompat.setChecked(真); }

与此相关...默认情况下,在XML中设置为false。