代码之家  ›  专栏  ›  技术社区  ›  Rohit Chauhan

当滚动时,ListView中的开关将自动进行检查。

  •  0
  • Rohit Chauhan  · 技术社区  · 6 年前

    我知道这个问题和这个问题重复

    Switches in listview automatically gets check android when scrolled 但答案对我不起作用

    当我滚动浏览listview开关get check时,我也遇到了同样的问题。 如果数据等于 “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;
    }
    
    2 回复  |  直到 6 年前
        1
  •  2
  •   Nabin Bhandari    6 年前

    ListView ,滚动时重复使用这些项。如果复选框已被选中,则在使用前需要取消选中它。

    用途:

    if(cursor.getInt(0) == 1){
        appName.setChecked(true);
    } else {
        appName.setChecked(false);
    }
    

    代替

    if(cursor.getInt(0) == 1){
        appName.setChecked(true);
    }
    

    或在执行其他任务之前重置/取消选中复选框:

    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.
    }
    
        2
  •  0
  •   smoothBlue    6 年前

    请删除从中分配/绑定数据的代码 public View getView

    把它移到 public void onBindViewHolder