lundi 13 novembre 2017

How can I add all checked checkboxes to database?

I'm creating checkboxes dynamically by records in the database. I can successfully generate checkboxech for each record in database.

Than after I click on button, I want to add a record in different table based of checked checkboxes. I need to go throuht all checkboxes, see if it's checked and if YES, I need to pass the id to id_participant to create the record. But I'm lost how to do it.

Can you help please?

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




Aucun commentaire:

Enregistrer un commentaire