简体   繁体   中英

J2ME , Quizz using choiceGroups

I am working on a driving licence project on j2Me wich is including Tests like quizz , well and i am having a problem after parsing the questions and moving them into choiceGroups just like that :

 if (questions.length > 0) { for (int i = 0; i < questions.length; i++) { ChoiceGroup reponses = new ChoiceGroup("Reponses" + i, Choice.EXCLUSIVE); reponses.append(questions[i].getReponse1(), null); reponses.append(questions[i].getReponse2(), null); reponses.append(questions[i].getReponse3(), null); pass.append(questions[i].getContenu()); pass.append(reponses); } } } catch (Exception e) { System.out.println("Exception:" + e.toString()); } disp.setCurrent(pass); 

and the next step is the command who's controlling the choiceGroups to test them if they are like the true answer or not . so i am blocked here .

  if (c == valider) { int result = 0; for (int i = 0; i < pass.size(); i++) { String ch = pass.get(i).getLabel(); System.out.println(ch); } } 

I don't know how to get the choice from the choicegroup any help

Actually, I am not sure what totally you want for:

This code will help you get selected items from choicegroup that i did long time before:

//get a selected array in choicegroup
    private String[] choiceGroupSelected(ChoiceGroup cg) {
        String selectedArray[] = new String[cg.size()];
        int k = 0;
        for (int i = 0; i < cg.size(); i++) {
            if (cg.isSelected(i)) {
                selectedArray[k] = cg.getString(i);
                k++;
            }
        }
        return selectedArray;
    }

That function will help me get all selected items for deleting action below:

private void deleteSpecificItem() {
        try {
            String temp = null;
            int index;
            //get ChoiceGroup size
            int numbers = cgTrip.size();
            String selectedItems[] = choiceGroupSelected(cgTrip);
            //
            rs = services.RecordStoreManager.openRecordStoreByName("TripRS");
            re = rs.enumerateRecords(null, null, true);
            String[] tripList = new String[2];
            for (int i = 0; i < numbers; i++) {
                temp = selectedItems[i];
                if (temp != null) {
                    while (re.hasNextElement()) {
                        try {
                            index = re.nextRecordId();
                            System.out.println("RecordID: " + index);
                            byte[] byteBuff = rs.getRecord(index);
                            String source = new String(byteBuff);
                            tripList = services.StringManager.getItems(source, ";", 2);
                            String strProcess = tripList[0] + "-" + tripList[1];
                            //inspect all of items in choicegroup and if they are selecting then compare with record
                            //If comparison is true then delete this record
                            if (temp.equals(strProcess)) {
                                System.out.println("Delete RecordID: " + index);
                                rs.deleteRecord(index);
                                re.keepUpdated(true);
                                break;
                            }
                        } catch (RecordStoreException ex) {
                            ex.printStackTrace();
                        }
                    }
                }
            }
            try {
                rs.closeRecordStore();
            } catch (RecordStoreException ex) {
                ex.printStackTrace();
            }
            rs = null;
            re.destroy();
            this.LoadTripItem();
        } catch (RecordStoreNotOpenException ex) {
            ex.printStackTrace();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM