
- Forum posts: 1
Sep 10, 2015, 12:52:58 PM via Website
Sep 10, 2015 12:52:58 PM via Website
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
public List<HashMap<String, Category>> allCat(Context context){
SQLiteDatabase db = this.getReadableDatabase();
List> list = new ArrayList>();
String s = "select * from category";
Cursor cursor = db.rawQuery(s, null);
int count = 0;
if (cursor.moveToFirst()){
do {
Resources resources = context.getResources();
HashMap hm = new HashMap();
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 allQuestions(int id){
SQLiteDatabase db = this.getReadableDatabase();
List questionses = new ArrayList();
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
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
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
// 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);