Android checkbox.isChecked()无法正常运行

莫诺

ListView在其中checkbox每行显示一个。
每次checkbox触摸时,我都会检查是否被检查。
但是每次,第一项总是返回false但是,如果选中第二项,.ischecked()则第一项复选框方法始终会返回true
这是我的代码

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

}

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

伊曼纽尔S

您应该使用setOnCheckedChangeListener。

样本:

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

        }
      });

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章

来自分类Dev

Android bindString无法正常运行

来自分类Dev

MediaPlayer无法正常运行(Android)?

来自分类Dev

CheckBox 在新的 Android 材料库中无法正常工作,无法看到实际的复选标记

来自分类Dev

附加视频无法正常运行-Android

来自分类Dev

android:windowSoftInputMode =“ adjustResize”无法正常运行

来自分类Dev

YouTube Android API seekToMillis()无法正常运行

来自分类Dev

强标签无法正常运行Android Webview

来自分类Dev

drawCircle方法无法正常运行(Android)

来自分类Dev

Android if-else语句无法正常运行

来自分类Dev

多个警报无法正常运行Android

来自分类Dev

android 9补丁无法正常运行

来自分类Dev

Android Eclipse调试无法正常运行

来自分类Dev

Android每日通知无法正常运行

来自分类Dev

离子运行android无法正常工作

来自分类Dev

Android RecyclerView OnItemTouchListener无法正常运行

来自分类Dev

onPostExecute无法运行,@ override无法正常运行android开发

来自分类Dev

isChecked()无法正常工作

来自分类Dev

CheckBox onCheckedListener无法正常工作DataBinding

来自分类Dev

Android Rating Bar布局权重属性无法正常运行

来自分类Dev

Android Studio 2.0即时运行无法正常工作

来自分类Dev

Google+登录无法在Android片段上正常运行

来自分类Dev

全局搜索无法在Android 4.4中正常运行

来自分类Dev

Android Desc命令按顺序无法正常运行

来自分类Dev

Android:autoLink无法在我的4.4设备上完全正常运行

来自分类Dev

如果我的android活动中的控件无法正常运行

来自分类Dev

从android studio运行应用程序无法正常工作

来自分类Dev

GCM推送通知无法在Android上正常运行

来自分类Dev

tns运行android --emulator无法正常工作-NativeScript

来自分类Dev

须藤离子运行Android无法正常工作

Related 相关文章

  1. 1

    Android bindString无法正常运行

  2. 2

    MediaPlayer无法正常运行(Android)?

  3. 3

    CheckBox 在新的 Android 材料库中无法正常工作,无法看到实际的复选标记

  4. 4

    附加视频无法正常运行-Android

  5. 5

    android:windowSoftInputMode =“ adjustResize”无法正常运行

  6. 6

    YouTube Android API seekToMillis()无法正常运行

  7. 7

    强标签无法正常运行Android Webview

  8. 8

    drawCircle方法无法正常运行(Android)

  9. 9

    Android if-else语句无法正常运行

  10. 10

    多个警报无法正常运行Android

  11. 11

    android 9补丁无法正常运行

  12. 12

    Android Eclipse调试无法正常运行

  13. 13

    Android每日通知无法正常运行

  14. 14

    离子运行android无法正常工作

  15. 15

    Android RecyclerView OnItemTouchListener无法正常运行

  16. 16

    onPostExecute无法运行,@ override无法正常运行android开发

  17. 17

    isChecked()无法正常工作

  18. 18

    CheckBox onCheckedListener无法正常工作DataBinding

  19. 19

    Android Rating Bar布局权重属性无法正常运行

  20. 20

    Android Studio 2.0即时运行无法正常工作

  21. 21

    Google+登录无法在Android片段上正常运行

  22. 22

    全局搜索无法在Android 4.4中正常运行

  23. 23

    Android Desc命令按顺序无法正常运行

  24. 24

    Android:autoLink无法在我的4.4设备上完全正常运行

  25. 25

    如果我的android活动中的控件无法正常运行

  26. 26

    从android studio运行应用程序无法正常工作

  27. 27

    GCM推送通知无法在Android上正常运行

  28. 28

    tns运行android --emulator无法正常工作-NativeScript

  29. 29

    须藤离子运行Android无法正常工作

热门标签

归档