通过适配器在同一布局中将数据库行显示为ListView / Gridview,是否可以选择将它们显示在单独的页面(如相同布局)中。因此,我可以通过按钮切换到下一页。
目前,我只是使用sharedprefernce保存值并更改布局中的内容(在我的情况下,只需 TEXT )。我正在尝试使用一个问题和4个选项来创建用于考试的应用程序。当前,当用户单击“下一步”按钮时,正在保存用户分数并在共享首选项中选中了单选按钮ID。
我的代码:
package com.sap.quizmaster;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
private MyDatabase testdb;
private Cursor mycursor;
private TextView ques;
private RadioGroup options;
private RadioButton op1;
private RadioButton op2;
private RadioButton op3;
private RadioButton op4;
private RadioButton radioButton;
private LinearLayout default_lay;
private LinearLayout default_btn_lay;
private TextView final_text;
private Button final_btn;
private Button next_btn;
private Button prev_btn;
private int checkedid = -1;
private int q_no = 1;
private String answer;
private int score = 0;
private SharedPreferences sp;
private SharedPreferences.Editor sp_edit;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ques = findViewById(R.id.ques);
op1 = findViewById(R.id.op1);
op2 = findViewById(R.id.op2);
op3 = findViewById(R.id.op3);
op4 = findViewById(R.id.op4);
next_btn = findViewById(R.id.next_btn);
prev_btn = findViewById(R.id.prev_btn);
options = findViewById(R.id.options);
final_btn = findViewById(R.id.final_btn);
final_text = findViewById(R.id.final_text);
default_lay = findViewById(R.id.default_text);
default_btn_lay = findViewById(R.id.default_btn);
//Loading sharedpreferences
sp = getSharedPreferences("QuizMaster", MainActivity.MODE_PRIVATE);
//Clearing all values from sharedpreferences when app re-launching
sp_edit = sp.edit();
sp_edit.clear();
sp_edit.commit();
testdb = new MyDatabase(this);
mycursor = testdb.getQuestion(q_no);
setQuestion();
next_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mycursor.close();
checkedid = options.getCheckedRadioButtonId();
saveInteger(q_no, checkedid);
if (checkedid != -1) {
radioButton = findViewById(checkedid);
if (radioButton.getText().equals(answer)) {
score += 3;
} else {
score -= 1;
}
}
options.clearCheck();
q_no = q_no + 1;
if(q_no>3){
default_btn_lay.setVisibility(View.GONE);
default_lay.setVisibility(View.GONE);
final_btn.setVisibility(View.VISIBLE);
final_text.setVisibility(View.VISIBLE);
final_text.setText("Your Score is "+score);
q_no = 1;
return;
}
checkedid = sp.getInt(q_no + "", -1);
if (checkedid != -1) {
radioButton = findViewById(checkedid);
radioButton.setChecked(true);
}
mycursor = testdb.getQuestion(q_no);
setQuestion();
}
});
prev_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mycursor.close();
if (q_no <= 1) {
Toast.makeText(MainActivity.this, "END", Toast.LENGTH_SHORT).show();
return;
}
q_no = q_no - 1;
mycursor = testdb.getQuestion(q_no);
setQuestion();
checkedid = sp.getInt(q_no + "", -1);
if (checkedid != -1) {
radioButton = findViewById(checkedid);
radioButton.setChecked(true);
if (radioButton.getText().equals(answer)) {
score -= 3;
} else {
score += 1;
}
}
Toast.makeText(MainActivity.this, "Button PREVIOUS pressed", Toast.LENGTH_SHORT).show();
}
});
final_btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final_text.setVisibility(View.GONE);
final_btn.setVisibility(View.GONE);
default_lay.setVisibility(View.VISIBLE);
default_btn_lay.setVisibility(View.VISIBLE);
mycursor = testdb.getQuestion(q_no);
setQuestion();
sp_edit = sp.edit();
sp_edit.clear();
sp_edit.commit();
score = 0;
}
});
}
@Override
protected void onDestroy() {
super.onDestroy();
testdb.close();
mycursor.close();
}
public void setQuestion() {
answer = mycursor.getString(mycursor.getColumnIndex("ANS"));
ques.setText(mycursor.getString(mycursor.getColumnIndex("QUES")));
op1.setText(mycursor.getString(mycursor.getColumnIndex("OP1")));
op2.setText(mycursor.getString(mycursor.getColumnIndex("OP2")));
op3.setText(mycursor.getString(mycursor.getColumnIndex("OP3")));
op4.setText(mycursor.getString(mycursor.getColumnIndex("OP4")));
}
public void saveInteger(int ques, int value) {
sp_edit = sp.edit();
sp_edit.putInt(ques + "", value);
sp_edit.apply();
}
}
只要给我一个链接或想法即可以任何简单的方式进行思考。
答案 0 :(得分:2)
想要使用AdapterViewFlipper的声音 https://developer.android.com/reference/android/widget/AdapterViewFlipper 使用自定义适配器
这将使用适配器中的数据从模板生成视图 例如每个问题可以在单独的页面上,并带有按钮以移至下一个问题。
示例https://abhiandroid.com/ui/adapterviewflipper
但是您可能不想使用自动翻转选项,而是将下一个按钮链接到.
方法