Android 如何将所有复选框添加到数据库?

Android 如何将所有复选框添加到数据库?,android,sqlite,checkbox,android-sqlite,Android,Sqlite,Checkbox,Android Sqlite,我正在通过数据库中的记录动态创建复选框。我可以成功地为数据库中的每条记录生成checkboxech 在我点击按钮之后,我想在不同的表中添加一条基于复选框的记录。我需要通过所有复选框,看看它是否被选中,如果是,我需要将id传递给id_参与者以创建记录。但我不知道怎么做 你能帮忙吗 @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bund

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

在我点击按钮之后,我想在不同的表中添加一条基于复选框的记录。我需要通过所有复选框,看看它是否被选中,如果是,我需要将id传递给id_参与者以创建记录。但我不知道怎么做

你能帮忙吗

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

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

如果该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?布局是线性布局,只有这样才能得到孩子们。如果这不起作用,我知道另一种可能的方法,当我尝试创建临时复选框并使其相等时:复选框临时;temp=layout.getChildAti;我得到错误不兼容的类型。必需的widget.checkbox,找到view.view。尝试向下转换它,尽管这样做并不总是安全的。由于所有复选框都继承自视图,所以从技术上讲,复选框是一个视图,这意味着您可以执行以下操作:temp=checkbox layout.getChildAti;这意味着您可以在temp上调用与checkbox相关的方法,并希望找到是否选中了它