• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Can't write query to call second question from DB. Couldn't solve myself.

Akramjon

Lurker
I am making a small quiz app. There are 3 categories and every category has 3 questions (9 questions). I use
GridView, Adapters and Loaders. Now on Activity I call first question of every category. On adapter There are
TextView for quesion, 4 Radiobuttons for answers and a Button for Next question. I read many topics and
tutorials about quiz, query, RadioGroup and RadioButtons. When i press Button question must change to another question but I even can't solve RadioButon's getCheckedRadioButtonId. It's always throwing error. Please help me, show me where is my mistake. I stopped here. At least 7 days passed when i come face to face with this problem. I myself can't solve this problem. Dear professionals please help me, show me some codes and point me to right way. Thanks very much.
DB
Code:
public List<HashMap<String, Category>> allCat(Context context){

    SQLiteDatabase db = this.getReadableDatabase();
   List<HashMap<String, Category>> list = new ArrayList<HashMap<String, Category>>();
   String s = "select * from category";
   Cursor cursor = db.rawQuery(s, null);
   int count = 0;
   if (cursor.moveToFirst()){
       do {
           Resources resources = context.getResources();
           HashMap<String, Category> hm = new HashMap<String, Category>();
           hm.put("category", new Category(cursor.getInt(1), resources.getIdentifier(context.getPackageName()

+ ":drawable/" +
           cursor.getString(cursor.getColumnIndex("image")), null, null), cursor.getString(2)));
           list.add(hm);
           count ++;
       }
       while (cursor.moveToNext());
   }
   db.close();
   return list;
}

public List<Questions> allQuestions(int id){
   SQLiteDatabase db = this.getReadableDatabase();
   List<Questions> questionses = new ArrayList<Questions>();
   String st = "select * from matem where category_id=" + id;

    Cursor cursor = db.rawQuery(st, null);

   if (cursor.moveToFirst()){
       do {       Questions questions = new Questions(cursor.getInt(0),
                   cursor.getString(1), cursor.getString(2),
                   cursor.getString(3), cursor.getString(4),
                   cursor.getString(5), cursor.getString(6),
                   cursor.getString(7), cursor.getInt(8), cursor.getInt(9));
                   questionses.add(questions);
       }while (cursor.moveToNext());
  }
   db.close();
   return questionses;
}

QuestionLoader
Code:
public class QuestionsLoaders extends AsyncTaskLoader<List<Questions>>{
    public int id;
    public QuestionsLoaders(Context context, int id) {
        super(context);
        this.id=id;
    }
    @Override
    public List<Questions> loadInBackground() {
        return new DbHelper(getContext()).allQuestions(id);
    }

QuestionAdapter
Code:
public class QuestionsAdapter extends BaseAdapter{
    Context context;
    LayoutInflater inflater;
    DbHelper dbHelper;
    Button btn_next;
    RadioGroup radioGroup;
    RadioButton rb,radAns1,radAns2,radAns3,radAns4;;
    List<Questions> questionsList;
    int nextQ = 0;
    int score = 0;
    int qid = 0;

    public QuestionsAdapter() {
    }

    public QuestionsAdapter(Context context, List<Questions> questionsList) {
        if (questionsList != null) {
            this.questionsList = questionsList;
            this.context = context;
            inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            dbHelper = new DbHelper(this.context);
        }
    }

    @Override
    public int getCount() {
        return questionsList.size();
    }

    @Override
    public Object getItem(int position) {
        return questionsList.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        final View view = inflater.inflate(R.layout.question_list, null);
        final Questions questions = questionsList.get(position);

        btn_next = (Button) view.findViewById(R.id.btn_next);

        final TextView question = (TextView) view.findViewById(R.id.textQuestion);
        question.setText(questions.getVopros());
        final RadioButton radAns1 = (RadioButton) view.findViewById(R.id.radAns1);
        radAns1.setText(questions.getAns_1());
        final RadioButton radAns2 = (RadioButton) view.findViewById(R.id.radAns2);
        radAns2.setText(questions.getAns_2());
        final RadioButton radAns3 = (RadioButton) view.findViewById(R.id.radAns3);
        radAns3.setText(questions.getAns_3());
        final RadioButton radAns4 = (RadioButton) view.findViewById(R.id.radAns4);
        radAns4.setText(questions.getAns_4());
        final TextView textAns = (TextView) view.findViewById(R.id.textAns);
        textAns.setText("Right answer:" + questions.getAns_r());

        radioGroup = (RadioGroup)view.findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                if (checkedId == radAns1.getId()){
                    Toast.makeText(view.getContext().getApplicationContext(), "Checked id: " + radAns1.getId(), 

Toast.LENGTH_SHORT).show();
                }else if (checkedId == radAns2.getId()){
                    Toast.makeText(view.getContext().getApplicationContext(), "Checked id: " + radAns2.getId(), 

Toast.LENGTH_SHORT).show();
                }else if (checkedId == radAns3.getId()){
                    Toast.makeText(view.getContext().getApplicationContext(), "Checked id: " + radAns3.getId(), 

Toast.LENGTH_SHORT).show();
                }else if (checkedId == radAns4.getId()){
                    Toast.makeText(view.getContext().getApplicationContext(), "Checked id: " + radAns4.getId(), 

Toast.LENGTH_SHORT).show();
                }
            }
        });

        btn_next.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                radioGroup = (RadioGroup)v.findViewById(R.id.radioGroup);
                    int selectedId = radioGroup.getCheckedRadioButtonId();
                    rb = (RadioButton)v.findViewById(selectedId);

//                radioGroup.setOnCheckedChangeListener();
            }

        });

        return view;
    }

and my tries to write getCheckedRadioButtonId and query

Code:
//        btn_next.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {
//                RadioGroup radioGroup = (RadioGroup) v.findViewById(R.id.radioGroup);
//                int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
//
//                String radioButtonSelected = "";
//
//                switch (checkedRadioButton) {
//                    case R.id.radAns1:
//                        radioButtonSelected = "radiobutton1";
//                        break;
//                    case R.id.radAns2:
//                        radioButtonSelected = "radiobutton2";
//                        break;
//                    case R.id.radAns3:
//                        radioButtonSelected = "radiobutton3";
//                        break;
//                    case R.id.radAns4:
//                        radioButtonSelected = "radiobutton4";
//                }
//            }
//        });


//        btn_next = (Button) view.findViewById(R.id.btn_next);
//        btn_next.setOnClickListener(new View.OnClickListener() {
//            @Override
//            public void onClick(View v) {

//                RadioGroup radioGroup = (RadioGroup) v.findViewById(R.id.radioGroup);
//                int checkedRadioButton = radioGroup.getCheckedRadioButtonId();
//
//                String radioButtonSelected = "";
//
//                switch (checkedRadioButton) {
//                    case R.id.radAns1 : radioButtonSelected = "radiobutton1";
//                        break;
//                    case R.id.radAns2 : radioButtonSelected = "radiobutton2";
//                        break;
//                    case R.id.radAns3 : radioButtonSelected = "radiobutton3";
//                        break;
//                    case R.id.radAns4 : radioButtonSelected = "radiobutton4";
//                }

//                    radioGroup = (RadioGroup)v.findViewById(R.id.radioGroup);
//                    int selectedId = radioGroup.getCheckedRadioButtonId();
//                    rb = (RadioButton)v.findViewById(selectedId);


//        Toast.makeText(v.getContext(), "Checked id: " + rb.getId(), Toast.LENGTH_SHORT).show();
//                RadioButton rb = (RadioButton) v.findViewById(radioGroup.getCheckedRadioButtonId());
//
//                int selectId=radioGroup.getCheckedRadioButtonId();
//                RadioButton selected = (RadioButton)v.findViewById(selectId);
//                String selected_user = selected.getText().toString();


//                final RadioGroup  radioGroup = (RadioGroup)v.findViewById(R.id.radioGroup);
//                int id=radioGroup.getCheckedRadioButtonId();
//                RadioButton selectedRedioButton = (RadioButton) v.findViewById(id);
//                Toast.makeText(v.getContext(), "You have selected "+selected_user.toString(), 


Toast.LENGTH_LONG).show();
//                    int selectedId = radioGroup.getCheckedRadioButtonId();
//                    rb = (RadioButton)v.findViewById(selectedId);


//                if (selectedId == radAns1.getId()){
//                Toast.makeText(v.getContext().getApplicationContext(), "Checked id: " + radAns1.getId(), 


Toast.LENGTH_SHORT).show();
//            }else if (selectedId == radAns2.getId()){
//                Toast.makeText(v.getContext(), "Checked id: " + radAns2.getId(), Toast.LENGTH_SHORT).show();
//            }else if (selectedId == radAns3.getId()){
//                Toast.makeText(v.getContext(), "Checked id: " + radAns3.getId(), Toast.LENGTH_SHORT).show();
//            }else if (selectedId == radAns4.getId()){
//                Toast.makeText(v.getContext(), "Checked id: " + radAns4.getId(), Toast.LENGTH_SHORT).show();
//            }
//                Toast.makeText(v.getContext(), "Checked id: Salom", Toast.LENGTH_SHORT).show();


//            int selectedId = radioGroup.getCheckedRadioButtonId();
//            if (selectedId == radAns1.getId()){
//                Toast.makeText(v.getContext(), "Checked id: " + radAns1.getId(), Toast.LENGTH_SHORT).show();
//            }else if (selectedId == radAns2.getId()){
//                Toast.makeText(v.getContext(), "Checked id: " + radAns2.getId(), Toast.LENGTH_SHORT).show();
//            }else if (selectedId == radAns3.getId()){
//                Toast.makeText(v.getContext(), "Checked id: " + radAns3.getId(), Toast.LENGTH_SHORT).show();
//            }else if (selectedId == radAns4.getId()){
//                Toast.makeText(v.getContext(), "Checked id: " + radAns4.getId(), Toast.LENGTH_SHORT).show();
//            }
//            }
//        });


//    @Override
//    public void onClick(View v) {


//        int selectedId = radioGroup.getCheckedRadioButtonId();
//        if (selectedId == radAns1.getId()){
//            Toast.makeText(v.getContext(), "Checked id: " + radAns1.getId(), Toast.LENGTH_SHORT).show();
//        }else if (selectedId == radAns2.getId()){
//            Toast.makeText(v.getContext(), "Checked id: " + radAns2.getId(), Toast.LENGTH_SHORT).show();
//        }else if (selectedId == radAns3.getId()){
//            Toast.makeText(v.getContext(), "Checked id: " + radAns3.getId(), Toast.LENGTH_SHORT).show();
//        }else if (selectedId == radAns4.getId()){
//            Toast.makeText(v.getContext(), "Checked id: " + radAns4.getId(), Toast.LENGTH_SHORT).show();
//        }




//        Questions questions = new Questions();
//        RadioButton rb = (RadioButton) v.findViewById(radioGroup.getCheckedRadioButtonId());


//        int sav2 = 1;
//        Vars vars = new Vars();
//        dbHelper.allQuestions(sav2);
//        dbHelper.rowcount(sav2);


//        RadioGroup rg = (RadioGroup)v.findViewById(R.id.radioGroup);
//        String radiovalue = ((RadioButton)v.findViewById(rg.getCheckedRadioButtonId())).getText().toString();


//        int radioButtonID = radioButtonGroup.getCheckedRadioButtonId();
//        View radioButton = radioButtonGroup.findViewById(radioButtonID);
//        int idx = radioButtonGroup.indexOfChild(radioButton);


//        int id = radioGroup.getCheckedRadioButtonId();
//        if (id == -1){
//            //no item selected
//        }
//        else{
//            if (id == R.id.radio_button1){
//                //Do something with the button
//            }
//        }


//        if(rg1.getCheckedRadioButtonId()!=-1){
//            int id= rg1.getCheckedRadioButtonId();
//            View radioButton = rg1.findViewById(id);
//            int radioId = radioGroup.indexOfChild(radioButton);
//            RadioButton btn = (RadioButton) rg1.getChildAt(radioId);
//            String selection = (String) btn.getText();
//        }
question1.png

Please help me to write my code. Please point me to right way. Thanks very much.
 
Back
Top Bottom