0
我有一个应用程序,我有一个列表视图与存储在sqlite数据库中的一些数据。 ListView还包含switchCompat当我启用开关索引的列表视图然后设置状态为“0”,当我禁用项目,然后设置为“1”。但是当状态设置为“0”时,启用开关,当状态设置为“1”时,禁用开关。请帮助。如何启用和禁用switchCompat accrodingly到数据库状态?
代码: -
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();
}
}
});
isCheck is boolean then if(isCheck.equal)not work – Raghav
你可以用这个检查isCheck = listStorage.get(position).getStatus()。equalsIgnoreCase(“0”); –
删除!从那一行开始。 –