【问题标题】:How can I add all checked checkboxes to database?如何将所有选中的复选框添加到数据库?
【发布时间】:2018-04-26 05:52:52
【问题描述】:

我正在通过数据库中的记录动态创建复选框。我可以成功地为数据库中的每条记录生成 checkboxech。

比单击按钮后,我想根据选中的复选框在不同的表中添加一条记录。我需要检查所有复选框,看看它是否被选中,如果是,我需要将 id 传递给 id_participant 以创建记录。但我不知道该怎么做。

你能帮忙吗?

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    myDB =  new DBAdapter(getContext());
    final View root = inflater.inflate(R.layout.fragment_add_from_db, container, false);
    final LinearLayout layout = root.findViewById(R.id.check_add_layout);
    btn_ad_group = root.findViewById(R.id.btn_add_group);
    Bundle bundle = getArguments();
    if (bundle != null) {
        id_eventu = bundle.getLong("idevent");
    }
    myDB.open();
    pocet = myDB.getParticipantsCount();
    Cursor cursor = myDB.getAllRowsParticipant();
    cursor.moveToFirst();
    for(int i = 0; i < pocet; i++) {
        CheckBox cb = new CheckBox(root.getContext());
        String name = cursor.getString(cursor.getColumnIndex("name"));
        String surname = cursor.getString(cursor.getColumnIndex("surname"));
        String text = name + " " + surname;
        cb.setText(text);
        cb.setHeight(90);
        cb.setId(cursor.getInt(cursor.getColumnIndex("_id")));
        layout.addView(cb);
        cursor.moveToNext();
    }
    myDB.close();

    btn_ad_group.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            myDB.open();
            // here I want to write into database by selected checkboxes
            // I want to read id of checked checkbox and pass it to id_participant
            myDB.insertRowRegistr(id_eventu, id_participant);
            myDB.close();

        }
    });

    return root;
}

【问题讨论】:

    标签: android sqlite checkbox android-sqlite


    【解决方案1】:

    你没有提供很多信息,所以我会假设一些事情。

    如果 LinearLayout“布局”是复选框的父级,您应该能够通过两个函数访问其子级:

    int numChildren = layout.getChildCount();
    for(int i=0;i<numChildren;i++){
        //create a temporary checkbox variable
        //make it equal to layout.getChildAt(i);
        //if temp is checked, add it to a list or hand it straight to database
    

    应该可以。从未在 android 中使用过复选框或数据库,但这应该可以让您访问每个复选框并检查它是否被单击。

    更新

    此代码可按我的需要工作。感谢您的帮助!

    btn_ad_group.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                myDB.open();
                int numChildren = layout.getChildCount();
                for(int i=0;i<numChildren;i++){
                    CheckBox temp;
                    temp = (CheckBox) layout.getChildAt(i);
                    if (temp.isChecked()) {
                        id_participant =  (long) temp.getId();
                        myDB.insertRowRegistr(id_eventu, id_participant);
                    }
                }
                myDB.close();
                fm.popBackStackImmediate();
            }
        });
    

    【讨论】:

    • 这是recyclerView还是listView?
    • 布局仅是线性布局
    • 那你应该得到孩子。如果这不起作用,我知道另一种方法可能
    • 当我尝试创建临时复选框并使其相等时:CheckBox temp; temp = layout.getChildAt(i);我收到错误不兼容的类型。必需的widget.checkbox,找到view.View。
    • 尝试向下转换它,尽管这样做并不总是安全的。由于所有 CheckBox 都继承自 View,因此从技术上讲,checkbox 是一个视图,这意味着您可以执行以下操作: temp = (CheckBox) layout.getChildAt(i);这意味着您可以在 temp 上调用与复选框相关的方法,并希望找出它是否被选中
    猜你喜欢
    • 2013-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-03-27
    • 1970-01-01
    相关资源
    最近更新 更多