列表视图中的开关在滚动时自动得到检查
Switches in listview automatically gets check when scrolled
我知道这个问题与这个问题重复
Switches in listview automatically gets check android when scrolled 但答案对我不起作用
当我滚动浏览列表视图开关时,我遇到了同样的问题,自动获取检查。
如果数据等于 '1',我的逻辑设置为打开,这是从数据库中检索到的并且它工作正常但是当我滚动列表时所有开关都被选中。
这是我的代码
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if(null==view){
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.list_item,null);
}
final ApplicationInfo data = appList.get(position);
if(null !=data){
final SwitchCompat appName = (SwitchCompat)view.findViewById(R.id.appName);
ImageView iconView = (ImageView)view.findViewById(R.id.app_icon);
//Read Data
dbHelper db = new dbHelper(context);
SQLiteDatabase database1 = db.getReadableDatabase();
Cursor cursor = database1.rawQuery("select isLocked from apps where package='"+appList.get(position).packageName+"'",null);
if(cursor !=null){
cursor.moveToFirst();
if(cursor.getInt(0) == 1){
appName.setChecked(true);
}
}
appName.setText(data.loadLabel(packageManager));
iconView.setImageDrawable(data.loadIcon(packageManager));
appName.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
//appList.get(position).packageName
dbHelper dbHelper = new dbHelper(context);
SQLiteDatabase database = dbHelper.getWritableDatabase();
database.execSQL("update apps set 'isLocked'=1 where package='"+appList.get(position).packageName+"'");
}else {
dbHelper dbHelper = new dbHelper(context);
SQLiteDatabase database = dbHelper.getWritableDatabase();
database.execSQL("update apps set 'isLocked'=0 where package='"+appList.get(position).packageName+"'");
}
}
});
}
return view;
}
在ListView
中,滚动时重复使用项目。如果该复选框已被选中,则需要在使用前取消选中它。
使用:
if(cursor.getInt(0) == 1){
appName.setChecked(true);
} else {
appName.setChecked(false);
}
代替
if(cursor.getInt(0) == 1){
appName.setChecked(true);
}
或reset/uncheck执行其他任务前的复选框:
View view = convertView;
if(null==view){
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.list_item,null);
}
final SwitchCompat appName = (SwitchCompat)view.findViewById(R.id.appName);
appName.setOnCheckedChangeListener(null);
appName.setChecked(false);
final ApplicationInfo data = appList.get(position);
if(null !=data){
// your code.
}
请从 public View getView
中删除 assigns/binds 数据的代码
并将其移动到 public void onBindViewHolder
我知道这个问题与这个问题重复
Switches in listview automatically gets check android when scrolled 但答案对我不起作用
当我滚动浏览列表视图开关时,我遇到了同样的问题,自动获取检查。 如果数据等于 '1',我的逻辑设置为打开,这是从数据库中检索到的并且它工作正常但是当我滚动列表时所有开关都被选中。
这是我的代码
public View getView(final int position, View convertView, ViewGroup parent) {
View view = convertView;
if(null==view){
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.list_item,null);
}
final ApplicationInfo data = appList.get(position);
if(null !=data){
final SwitchCompat appName = (SwitchCompat)view.findViewById(R.id.appName);
ImageView iconView = (ImageView)view.findViewById(R.id.app_icon);
//Read Data
dbHelper db = new dbHelper(context);
SQLiteDatabase database1 = db.getReadableDatabase();
Cursor cursor = database1.rawQuery("select isLocked from apps where package='"+appList.get(position).packageName+"'",null);
if(cursor !=null){
cursor.moveToFirst();
if(cursor.getInt(0) == 1){
appName.setChecked(true);
}
}
appName.setText(data.loadLabel(packageManager));
iconView.setImageDrawable(data.loadIcon(packageManager));
appName.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
if(b){
//appList.get(position).packageName
dbHelper dbHelper = new dbHelper(context);
SQLiteDatabase database = dbHelper.getWritableDatabase();
database.execSQL("update apps set 'isLocked'=1 where package='"+appList.get(position).packageName+"'");
}else {
dbHelper dbHelper = new dbHelper(context);
SQLiteDatabase database = dbHelper.getWritableDatabase();
database.execSQL("update apps set 'isLocked'=0 where package='"+appList.get(position).packageName+"'");
}
}
});
}
return view;
}
在ListView
中,滚动时重复使用项目。如果该复选框已被选中,则需要在使用前取消选中它。
使用:
if(cursor.getInt(0) == 1){
appName.setChecked(true);
} else {
appName.setChecked(false);
}
代替
if(cursor.getInt(0) == 1){
appName.setChecked(true);
}
或reset/uncheck执行其他任务前的复选框:
View view = convertView;
if(null==view){
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = layoutInflater.inflate(R.layout.list_item,null);
}
final SwitchCompat appName = (SwitchCompat)view.findViewById(R.id.appName);
appName.setOnCheckedChangeListener(null);
appName.setChecked(false);
final ApplicationInfo data = appList.get(position);
if(null !=data){
// your code.
}
请从 public View getView
并将其移动到 public void onBindViewHolder