Android小项目--答题系统

DBService.class

//此类用于连接数据库public class DBService {    
private SQLiteDatabase db;    
public DBService(){        
db=SQLiteDatabase.openDatabase("/data/data/test.ljh.com.answering/databases/question.db",null,SQLiteDatabase.OPEN_READWRITE);    }    
public List getQuestions(){        
List list=new ArrayList();        
Cursor cursor=db.rawQuery("select * from question",null);        
if(cursor.getCount()>0){            //大于0表示取得数据            
cursor.moveToFirst();            
int count=cursor.getCount();           
 for(int i=0;i

ExamActivity.class

public class ExamActivity extends AppCompatActivity {    
private int count;   
 private int current;    
private boolean wrongMode;   
 @Override    
protected void onCreate(Bundle savedInstanceState) {        
super.onCreate(savedInstanceState);        
setContentView(R.layout.activity_exam);        
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);        
setSupportActionBar(toolbar);       
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);        
fab.setOnClickListener(new View.OnClickListener() {            @Override            
public void onClick(View view) {                
Snackbar.make(view, "Replace with your own action", 
Snackbar.LENGTH_LONG)                        .setAction("Action", null).show();            }        });       
 
DBService dbService=new DBService();        
final List list=dbService.getQuestions();        
count=list.size();       
 current=0;        
wrongMode=false;       
 final TextView tv_question=(TextView)findViewById(R.id.question);        
final RadioButton[] radioButtons=new RadioButton[4];        
radioButtons[0]=(RadioButton)findViewById(R.id.answerA);        
radioButtons[1]=(RadioButton)findViewById(R.id.answerB);        
radioButtons[2]=(RadioButton)findViewById(R.id.answerC);        
radioButtons[3]=(RadioButton)findViewById(R.id.answerD);        
Button btn_next=(Button)findViewById(R.id.btn_next);        Button 
btn_previous=(Button)findViewById(R.id.btn_previous);        final 
TextView tv_explaination=(TextView)findViewById(R.id.explaination);        
final RadioGroup radioGroup=(RadioGroup)findViewById(R.id.radioGroup);        
Question q=list.get(0);        
tv_question.setText(q.question);        
tv_explaination.setText(q.explaination);        
radioButtons[0].setText(q.answerA);        
radioButtons[1].setText(q.answerB);        
radioButtons[2].setText(q.answerC);        
radioButtons[3].setText(q.answerD);        
btn_next.setOnClickListener(new View.OnClickListener() {            
@Override            
public void onClick(View v) {                
if (current < count - 1) {                   
 current++;                  
  Question q = list.get(current);                    
tv_question.setText(q.question);                    
tv_explaination.setText(q.explaination);                    
radioButtons[0].setText(q.answerA);                    
radioButtons[1].setText(q.answerB);                    
radioButtons[2].setText(q.answerC);                    
radioButtons[3].setText(q.answerD);                    
radioGroup.clearCheck();                    
if (q.selectedAnswer != -1) {                        
radioButtons[q.selectedAnswer].setChecked(true);                    }                
} 
else if (current == count - 1 && wrongMode == true) {                    
new AlertDialog.Builder(ExamActivity.this)                            .setTitle("提示")                           
 .setMessage("已经到达最后一题,是否退出?")                            
.setPositiveButton("确定", new DialogInterface.OnClickListener() {                                @Override                                
public void onClick(DialogInterface dialog, int which) {                                    
ExamActivity.this.finish();                               
 }                     
       })                          
  .setNegativeButton("取消",null)                       
     .show();              
  } else {           
         final List wrongList = checkAnswer(list);                    
new AlertDialog.Builder(ExamActivity.this)                            
.setTitle("提示")                            
.setMessage("你答对了" + (list.size() - wrongList.size()) +                                    "道题目,答错了" + wrongList.size() + "道题目,是否查看错题?")                            
.setPositiveButton("确定", new DialogInterface.OnClickListener() {                                
@Override                                
public void onClick(DialogInterface dialog, int which) {                                    
wrongMode = true;                                   
 List newList = new ArrayList();                                    
for (int i = 0; i < wrongList.size(); i++) {                                        
newList.add(list.get(wrongList.get(i)));                                    }                                    
list.clear();                                    
for (int i = 0; i < newList.size(); i++) {                                        
list.add(newList.get(i));                                    }                                    
current = 0;                                   
 count = list.size();                                   
 Question q = list.get(current);                                    
tv_question.setText(q.question);                                    
tv_explaination.setText(q.explaination);                                    
radioButtons[0].setText(q.answerA);                                    
radioButtons[1].setText(q.answerB);                                    
radioButtons[2].setText(q.answerC);                                    
radioButtons[3].setText(q.answerD);                                    
tv_explaination.setVisibility(View.VISIBLE);                                }                            
})                            
.setNegativeButton("取消", new DialogInterface.OnClickListener() {                                
@Override                               
 public void onClick(DialogInterface dialog, int which) {                                    
ExamActivity.this.finish();                                }                            
}).show();                }            }        });        
btn_previous.setOnClickListener(new View.OnClickListener() {            
@Override           
 public void onClick(View v) {               
 if (current > 0)//当前问题是否第一个问题,大于0表示不是             
   {                   
 current--;           
         Question q = list.get(current);                    
tv_question.setText(q.question);                    
tv_explaination.setText(q.explaination);                    
radioButtons[0].setText(q.answerA);                    
radioButtons[1].setText(q.answerB);                    
radioButtons[2].setText(q.answerC);                    
radioButtons[3].setText(q.answerD);                    
radioGroup.clearCheck();                  
  if (q.selectedAnswer != -1) {                        
radioButtons[q.selectedAnswer].setChecked(true);                    }                
}            }        });       
 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener(){            
@Override            
public void onCheckedChanged(RadioGroup group, int checkedId) {                
for(int i=0;i<4;i++){                    
if(radioButtons[i].isChecked()==true){                        
list.get(current).selectedAnswer=i;                        
break;                    
}                }            }        });    }   
 private List checkAnswer(List list){        
List wrongList=new ArrayList();        
for(int i=0;i

MainActivity

public class MainActivity extends AppCompatActivity {   
 @Override   
 protected void onCreate(Bundle savedInstanceState) {        
super.onCreate(savedInstanceState);        
setContentView(R.layout.activity_main);        String 
DB_PATH="/data/data/test.ljh.com.answering/databases/";        
String DB_NAME="question.db";       
 if((new File(DB_PATH+DB_NAME).exists())==false){           
 File dir=new File(DB_PATH);            
if(!dir.exists()){               
 dir.mkdir();          
  }           
 try{                    
InputStream is=getBaseContext().getAssets().open(DB_NAME);                
OutputStream os=new FileOutputStream(DB_PATH+DB_NAME);                
byte[] buffer=new byte[1024];               
 int length;                
while ((length=is.read(buffer))>0){                    
os.write(buffer,0,length);               
 }              
 os.flush();        
        os.close();     
           is.close();      
      } catch (IOException e) {               
 e.printStackTrace();       
     }    
    }      
  Button btn=(Button)findViewById(R.id.button);        
btn.setOnClickListener(new View.OnClickListener() {            
@Override           
 public void onClick(View v) {               
 Intent intent=new Intent(MainActivity.this,ExamActivity.class);                
startActivity(intent);         
   }      
  });   
 }}

Question.class

public class Question {    
public String question;    
public String answerA;    
public String answerB;  
  public String answerC;  
  public String answerD;    
public int answer;   
 public String explaination;   
 public int ID;   
 public int selectedAnswer;}

你可能感兴趣的:(Android小项目--答题系统)