Android checkbox.isChecked() 无法正常工作

2024-01-11

我有一个ListView其中一个checkbox显示在每行中。
每一次checkbox被触摸,我检查它是否被选中。
但每次,第一项总是返回false。但是,如果选中第 2 项,则.ischecked()第一项的复选框的方法总是返回true.
这是我的code:

public class CustomSpecificCar extends ArrayAdapter<JSONObject>{
    ListSpecificCars caller;
    private JSONObject currentJson;
    private CheckBox cbSelectedCar;
    private int position;

    public CustomSpecificCar(Context ctxt, List<JSONObject> list, ListSpecificCars caller){
        super(ctxt, R.layout.custom_specific_car_row, list);
        this.caller = caller;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        LayoutInflater inflater = LayoutInflater.from(getContext());
        View customView = inflater.inflate(R.layout.custom_specific_car_row, parent, false);
        this.position = position;

        // Set the reference of the layout
        currentJson                       = getItem(this.position);
        cbSelectedCar                     = (CheckBox)customView.findViewById(R.id.cbSelectedCar);
        TextView tvBrand                  = (TextView)customView.findViewById(R.id.tvBrand);
        TextView tvModel                  = (TextView)customView.findViewById(R.id.tvModel);
        TextView tvOwnerEditable          = (TextView)customView.findViewById(R.id.tvOwnerEditable);
        TextView tvPriceEditable          = (TextView)customView.findViewById(R.id.tvEstimatedPriceEditable);


        try {
            tvBrand.setText(currentJson.getString("brand"));
            tvModel.setText(currentJson.getString("model"));
            tvOwnerEditable.setText(currentJson.getString("owner"));
        } catch (JSONException e) {
            Log.e(e.getClass().getName(), "JSONException", e);
        }

        cbSelectedCar.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                    if (cbSelectedCar.isChecked()){
                        caller.updateClickedUsername(currentJson, true); // Add to the List
                        Log.d("Added click ", "ok");
                    }

                    else if (!cbSelectedCar.isChecked()) {
                        caller.updateClickedUsername(currentJson, false); // Delete from the List
                        Log.d("Deleted click ", "nok");
                    }
        }});

        return customView;
    }

}

我应该怎么做才能解决这个问题? 提前谢谢了!


您应该使用 setOnCheckedChangeListener。

Sample:

  checkbox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {

        }
      });

在你的代码中:

    cbSelectedCar.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {

        @Override
        public void onCheckedChanged(CompoundButton buttonView,
            boolean isChecked) {
              if (isChecked){
                    caller.updateClickedUsername(currentJson, true); // Add to the List
                    Log.d("Added click ", "ok");
                }

                else if (!isChecked) {
                    caller.updateClickedUsername(currentJson, false); // Delete from the List
                    Log.d("Deleted click ", "nok");
                }

        }
      });
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android checkbox.isChecked() 无法正常工作 的相关文章

随机推荐

  • Oracle 云实例连接问题

    我对云缺乏经验 我已在 Oracle Cloud 中创建了一个计算实例 但是当我尝试使用公共 i p ssh 连接到它时 它显示 无法连接到主机端口 22 操作超时 我已经为实例创建了一个公共 i p 并提供了公钥 所有其他选项均设置为默认
  • 引用 NET Standard 1.6 项目时,ASP.NET Web API 2 无法返回流

    这是我见过的奇怪问题之一 以下是重现的步骤 1 gt 使用 Target Framework 4 6 2 在 VS 2017 中创建新的 Web API 项目 Create new ASP NET Web Application gt Se
  • Android 上的虚拟蓝牙设备?

    昨晚 我不断地被说外语的错误号码所困扰 我开始寻找一个应用程序来在电话连接中播放音频文件 带有多种语言的 黄号码 并发现了很多注释 说明由于硬件的工作方式 以及相反的情况 记录电话交谈 这是不可能的 不可能 总是让我想知道如何做某事 所以我
  • 在 Xamarin Forms 中实现自定义 Webview

    我是新来的Xamarin Forms我已经实现了一个自定义网页视图渲染器Droid项目 问题是在实现渲染器时iOS项目 就像Webview在不加载 CSS 和 Javascript 的情况下进行初始化 因为它只显示HTML页面没有任何功能
  • 使用 AWK 删除字段前的空格

    几乎完全重复通过 AWK 保持原始格式 POST 传递 https stackoverflow com questions 408469 keeping original format post passing through awk由同一
  • 类文件格式的最终​​变量

    Does class文件格式提供支持final关键字与变量一起使用吗 或者它只是从代码中推断出变量的有效最终性 然后 JIT 编译器基于它执行优化 Here https docs oracle com javase specs jvms s
  • 使用 powershell 正确格式化 JSON

    我有一个 JSON 格式的以下文件 Before ConvertTo JSON Yura Cashier branch release Retail v4 0 configuration RetailDemo Debug datetime
  • 如何在相机中添加边框?

    我需要在反应本机相机视图的取景器中间添加一个方框 存储库中也没有有关它的信息 那里的问题也没有得到解答 您正在使用哪个模块 react native camera or react native camera kit 如果您使用react
  • java正则表达式模式未封闭的字符类

    我需要一些帮助 我越来越 Caused by java util regex PatternSyntaxException Unclosed character class near index 24 a zA Z 0 9 at java
  • Scala:为什么隐式找不到隐式 ExecutionContext?

    我有一个特点 trait Crawler implicit def system ActorSystem implicit def executionContext ExecutionContext implicit def materia
  • 如何安装较新的 swift-tools-版本?

    我第一次尝试创建一个新的 Swift 包 根据这个苹果文档 https developer apple com documentation swift packages bundling resources with a swift pac
  • 应用程序在列表视图适配器第二次播放音频时崩溃

    我的课程由包含曲目列表的列表视图组成 每个列表项由 播放 和 暂停 按钮组成 它应该在单击 播放 时播放曲目 在单击 暂停 时停止曲目 我没有使用不同的按钮 我正在使用一个按钮仅在单击黑白播放和暂停时更改其文本并相应地起作用 我面临以下问题
  • 在 VSCode 中调试 FastAPI 应用程序

    我正在尝试调试使用 FastAPI uvicorn 的应用程序 web api 我也在使用诗歌并在 vscode 中设置 projev 虚拟环境 i read this https fastapi tiangolo com tutorial
  • 如何在CSS中从上到下堆叠div

    我有一个这样的列表 div class item 1 div div class item 2 div div class item 3 div div class item 4 div div class item 5 div 使用 cs
  • HttpClient 返回特殊字符但没有可读内容

    我正在尝试使用 async await 和 HttpClient 下载网页 但只得到一个充满特殊字符的字符串 代码就像 static async void DownloadPageAsync string url HttpClient cl
  • Flutter - 跨多个设备的端到端加密的密钥存储

    我正在编写一个 flutter 应用程序 用户基本上可以在其中创建注释 我编写了代码来为用户创建私钥 稍后用于加密他们的数据 加密数据存储在 Firebase Firestore 中 当检索他们的数据时 将使用他们的密钥对其进行解密 我看过
  • TensorFlow:“模块”对象没有属性“SessionRunHook”

    我在 Ubuntu 16 04 上运行 TensorFlow r0 10 版本 我已经能够运行一些基本教程 包括 MIST 字符识别教程 我正在努力完成CIFAR 10 https www tensorflow org versions r
  • 在线和离线用户实时使用 strope.js

    我在用strope js用于连接的 javascript 客户端库xmpp server openfire 使用下面的代码 var BOSH SERVICE http 127 0 0 1 7070 http bind connection
  • 将表单另存为图像(屏幕截图)

    我有两种形式 表格 1 包含我需要截图的内容 表格 2 包含图形绘制 该表格始终位于顶部但透明 我需要对第一个表单进行屏幕截图 而不将其放在表单 2 之上 也不包含表单 2 中的内容 这是我正在处理的一些问题 我正在努力修复 Private
  • Android checkbox.isChecked() 无法正常工作

    我有一个ListView其中一个checkbox显示在每行中 每一次checkbox被触摸 我检查它是否被选中 但每次 第一项总是返回false 但是 如果选中第 2 项 则 ischecked 第一项的复选框的方法总是返回true 这是我