【问题标题】:Switches in listview automatically gets check when scrolled滚动时自动检查列表视图中的开关
【发布时间】:2018-06-18 08:23:50
【问题描述】:

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

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

当我滚动通过 listview 开关自动检查时,我遇到了同样的问题。 如果数据等于从数据库中检索的 '1' 并且它工作正常但是当我滚动列表时所有开关都会被检查,我的逻辑设置为 on。

这是我的代码

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

【问题讨论】:

    标签: android listview switchcompat


    【解决方案1】:

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

    【讨论】:

    • 如果将if(null==view) 替换为if(true) 是否有效? -- 仅供测试,请勿永久使用
    • 你的意思是使用if(true)后它可以工作了吗?如果是这样,请不要使用它。这是一种不好的做法,可以通过其他方式解决。
    • 用另一种方式告诉我
    【解决方案2】:

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

    并将其移至public void onBindViewHolder

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-01-21
      • 2017-01-11
      • 1970-01-01
      • 2023-03-31
      • 1970-01-01
      相关资源
      最近更新 更多