Не получается написать запрос для вызова 2го вопроса из базы
Добавлено: 10 сен 2015, 20:06
Всем привет. Хочу разработать маленький тест приложения. Есть 3 категории, в каждом категории по 3 вопроса (в общем 9 вопросов пока). Сейчас выходят все первые вопросы категорий. Пользуюсь GridView, Adapter, LoadManager. В адапторе есть TextView для вопроса, 4-RadioButton для выбора ответов и кнопка Next для перехода к следующему вопросу. Здесь у меня начались проблемы. Во первых не как не получается по нажатию кнопки Next, getCheckedRadioButtonId для RadioButton чтобы определить правильный или неправильный ответ выбран. Просмотрел много примеров по RadioButton и RadioGroup что то я точно не правильно делаю. И самое главное по нажатию кнопки должна выйти второй вопрос. Делать запрос или цикл вообще голова не работает до этого место дошел. Пожалуйста подскажите коды, поправте мои ошибки. Спасибо большое.
Спасибо большое
Код: Выделить всё
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;
}
Код: Выделить всё
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;
}