android - 动态选中列表中的复选框

标签 android android-checkbox

我正在处理复选框 ListView 。到目前为止,我所做的是从本地数据库填充列表,使用 setOnCheckedChangeListener 获取所有选中项目的数据库 ID,并将其存储在本地数据库的单独表中。这是我的代码的样子:

@Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        if(convertView == null){
            dbHelper = new DBHelper(con);
            database = dbHelper.getWritableDatabase();
            switch (getItemViewType(position)){
                case DYNAMIC_OBJECTIVE:
                    convertView = layoutInflater.inflate(R.layout.objective_list_view, null);
                    break;
                case STATIC_OBJECTIVE:
                    convertView = layoutInflater.inflate(R.layout.other_objective_list_view, null);
                    break;
            }

            switch (getItemViewType(position)){
                case DYNAMIC_OBJECTIVE:
                    CheckBox checkBox = (CheckBox) convertView.findViewById(R.id.objectiveCheckBox);
                    checkBox.setText(((Objectives)list.get(position)).getObjectiveTitle());

                    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                            if (isChecked) {
                                selectedObjectivesMap.put(((Objectives)list.get(position)).getObjectiveID(), "YES");
                            }else{
                                selectedObjectivesMap.put(((Objectives)list.get(position)).getObjectiveID(), "NO");
                            }
                        }
                    });
                    break;
                case STATIC_OBJECTIVE:
                    TextView checkBoxOther = (TextView) convertView.findViewById(R.id.objectiveOtherCheckBox);
                    final TextView objectiveOtherET = (TextView) convertView.findViewById(R.id.objectiveOtherEditText);
                    checkBoxOther.setText((String)list.get(position));
                    //ll = (LinearLayout)convertView.findViewById(R.id.linearLayoutOther);
                    //viewNew = layoutInflater.inflate(R.layout.other_objective_row, null);
                    ImageButton imageButton = (ImageButton) convertView.findViewById(R.id.addOtherObjectiveTextBox);
                    imageButton.setOnClickListener(new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            //Toast.makeText(v.getContext(), "Clicked123", Toast.LENGTH_SHORT).show();
                            final Dialog dialog = new Dialog(v.getContext());
                            dialog.setContentView(R.layout.otherobjectivedialog);

                            final EditText enterObjET = (EditText) dialog.findViewById(R.id.enterObjEditText);
                            Button closeEnterObjBtn = (Button) dialog.findViewById(R.id.closeEnterObjDialog);
                            Button objConfirmBtn = (Button) dialog.findViewById(R.id.enterObjConfirmBtn);

                            closeEnterObjBtn.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    dialog.dismiss();
                                }
                            });

                            objConfirmBtn.setOnClickListener(new View.OnClickListener() {
                                @Override
                                public void onClick(View v) {
                                    String enteredObj = enterObjET.getText().toString();
                                    if(enteredObj.equals("")){
                                        Toast.makeText(v.getContext(), "Please write a objective", Toast.LENGTH_LONG).show();
                                    }else {
                                        objectiveOtherET.setText(objectiveOtherET.getText().toString()+ " \n" + enteredObj);
                                        otherObjList.add(enteredObj);
                                        dialog.dismiss();
                                    }
                                }
                            });

                            dialog.show();

/*                            LinearLayout linearLayoutNew = new LinearLayout(con);
                            linearLayoutNew.setOrientation(LinearLayout.VERTICAL);
                            et = new EditText(con);
                            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
                            et.setLayoutParams(p);
                            et.setHint("Set some objective");
                            linearLayoutNew.addView(et);
                            ll.addView(linearLayoutNew);*/
                        }
                    });
                    break;
            }

        }
        return convertView;
    }

这就是我填充列表的方式:

ArrayList<Object> objectiveList = new ArrayList<>();
        Cursor crs = database.rawQuery("SELECT * FROM "+ ItemsTable.TABLE_OBJECTIVE +"", null);

        while(crs.moveToNext())
        {
            String title = crs.getString(crs.getColumnIndex("title"));
            int objectiveID = crs.getInt(crs.getColumnIndex("id"));
            objectiveList.add(new Objectives(objectiveID, title));
        }
        crs.close();
        objectiveList.add("Others");

        listView.setAdapter(new ObjectivesAdapter(getActivity(), objectiveList));
        viewPager = (ViewPager) getActivity().findViewById(R.id.pager);

问题: 这一切都很完美,但现在我想编辑相同的信息。我想要的只是当我返回此 View 时应检查现有的复选框 ID(已检查并存储在我的数据库中)。我已经查询了数据并且我有 ID,但我不知道如何根据 ID 来检查复选框。

            Cursor crsCheckObj = database.rawQuery("SELECT * FROM "+ ItemsTable.TABLE_PLAN_OBJECTIVE_MAP +" WHERE "
                    + ItemsTable.COLUMN_PLAN_OBJECTIVE_MAP_PLANID +"='"+ Info.getInstance().getPlanVisitID() +"'", null);
            if(crsCheckObj.getCount() > 0){
                while (crsCheckObj.moveToNext()){
                    //Here IDs should match in order to check the checkboxes
                }

            }
            crsCheckObj.close();

最佳答案

为此,您可以简单地使用 checkbox.setChecked(true)

如果它不起作用,请尝试以下操作:

checkbox.postDelayed(new Runnable() {
            @Override
            public void run() {
                checkbox.setChecked(true);
            }
        },100);

关于android - 动态选中列表中的复选框,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47030492/

相关文章:

android livedata进行顺序调用

java - 在微调器中选择项目后,它没有反射(reflect)给我(肉眼不可见)

android - 自定义复选框 starStyle 的外观

android - 最初在列表中不可见时未选中 MultiSelectListPreference 复选框(API 23)

java - 使用 setOnCheckedChangeListener 的复选框持有者

Android 菜单选项复选框未选中方向更改修复

android - 如何让 Android SafetyNetClient.Attest 使用仅限于 Android 应用程序的 API key ?

安卓应用程序: Preference Activity Crashing

java - 在 TravisCI 上测试

Android使用按钮和对话框复选框进行多项选择,并在 ListView 中显示所选项目而不是按钮