Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> android 項目 local_Test_exam 代碼分享

android 項目 local_Test_exam 代碼分享

編輯:關於Android編程

本android項目是致力於建立一個本地的sqlite數據庫,然後存儲考試的題目,android應用調用sqlite數據庫中的數據,生成相應的答題界面。

下面是代碼解析

\

 

項目主要是有界面是activity_exam.xml 和 activity_index.xml組成

(1)activity_exam.xml 展示界面

\

 

代碼如下:

 




 

 

 

 

 

 

(2)activity_question.xml界面和代碼如下

\

 

 



    
    
        
        
            
            
            
            
        
        
    
    
    


 

(3)邏輯處理部分

接下來就是4個邏輯處理部分了

DBservice 負責數據庫數據的存取

Question 為自定義數據類型,存儲信息

在indexActivity類中創建sqlite數據庫,並設置startExam按鈕的相應事件,跳轉到答題界面

examActivity類為題目展示階段,將數據庫中的問題取出展示到android應用上

 

DBService.java 代碼

 

package com.exam.administrator.exam;

import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by Administrator on 2016/8/15.
 */
public class DBService {
    private SQLiteDatabase db;
    public DBService(){
        db = SQLiteDatabase.openDatabase("/data/data/com.exam.administrator.exam/databases/question.db",null, SQLiteDatabase.OPEN_READWRITE);
    }
    public List getQuestion(){
        List list = new ArrayList();
        Cursor cursor = db.rawQuery("Select * from question", null);
        if(cursor.getCount() > 0){
            cursor.moveToFirst();
            int count = cursor.getCount();
            for(int i = 0; i < count; i++){
                cursor.moveToPosition(i);
                Question question = new Question();
                question.question = cursor.getString(cursor.getColumnIndex("question"));
                question.answerA = cursor.getString(cursor.getColumnIndex("answerA"));
                question.answerB = cursor.getString(cursor.getColumnIndex("answerB"));
                question.answerC = cursor.getString(cursor.getColumnIndex("answerC"));
                question.answerD = cursor.getString(cursor.getColumnIndex("answerD"));
                question.answer = cursor.getInt(cursor.getColumnIndex("answer"));
                question.ID = cursor.getInt(cursor.getColumnIndex("ID"));
                question.explanation = cursor.getString(cursor.getColumnIndex("explanation"));
                question.selectedAnswer = -1;
                list.add(question);
            }
        }
        return list;

    }
}


















examActivity.java代碼
package com.exam.administrator.exam;

import android.content.DialogInterface;
import android.preference.DialogPreference;
import android.provider.MediaStore;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.List;

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);
        DBService  dbService = new DBService();
        final List list = dbService.getQuestion();
        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.next);
        Button btn_previous =(Button)findViewById(R.id.previous);
        final TextView tv_explanation = (TextView)findViewById(R.id.explanation);
        final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radioGroup);

        Question q = list.get(0);
        tv_question.setText(q.question);
        tv_explanation.setText(q.explanation);
        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 view) {
                if(current < count -1){
                    current++;
                    String string = "" + count;
                    //Toast.makeText(ExamActivity.this, string,Toast.LENGTH_SHORT).show();
                    Question q = list.get(current);
                    tv_question.setText(q.question);
                    radioGroup.clearCheck();
                    radioButtons[0].setText(q.answerA);
                    radioButtons[1].setText(q.answerB);
                    radioButtons[2].setText(q.answerC);
                    radioButtons[3].setText(q.answerD);
                    tv_explanation.setText(q.explanation);
                    if(q.selectedAnswer != -1){
                        radioButtons[q.selectedAnswer].setChecked(true);
                    }
                }else if(current == count-1 && wrongMode == true){
                    new AlertDialog.Builder(ExamActivity.this)
                            .setTitle("Remider")
                            .setMessage("End of the question, quit or not?")
                            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialog, int which){
                                    ExamActivity.this.finish();
                                }
                            }).setNegativeButton("No", null)
                            .show();
                }
                else{
                   // Toast.makeText(ExamActivity.this, "Yes",Toast.LENGTH_SHORT).show();
                    final List wrongList = checkAnswer(list);
                    if(wrongList.size() == 0){
                        new AlertDialog.Builder(ExamActivity.this)
                                .setTitle("reminder")
                                .setMessage("All are Right")
                                .setPositiveButton("Ok", new DialogInterface.OnClickListener(){
                                    @Override
                                    public void onClick(DialogInterface dialog, int which){
                                        ExamActivity.this.finish();
                                    }
                                }).show();
                    }else{
                    new AlertDialog.Builder(ExamActivity.this)
                            .setTitle("reminder")
                            .setMessage((list.size()-wrongList.size()) + " right answers. " +
                                    wrongList.size() + " wrong answers. Do you want to review?")
                            .setPositiveButton("YES", new DialogInterface.OnClickListener() {
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i) {
                                    wrongMode = true;
                                    List newList = new ArrayList();
                                    for(int j = 0; j < wrongList.size(); j++){
                                        newList.add(list.get(wrongList.get(j)));
                                    }
                                    list.clear();
                                    for(int j = 0; j < newList.size(); j++){
                                        list.add(newList.get(j));
                                    }
                                    current = 0;
                                    count = list.size();

                                    Question q = list.get(current);
                                    tv_question.setText(q.question);
                                    radioGroup.clearCheck();
                                    radioButtons[0].setText(q.answerA);
                                    radioButtons[1].setText(q.answerB);
                                    radioButtons[2].setText(q.answerC);
                                    radioButtons[3].setText(q.answerD);
                                    tv_explanation.setText(q.explanation);
                                    tv_explanation.setVisibility(View.VISIBLE);
                                    if(q.selectedAnswer != -1){
                                        radioButtons[q.selectedAnswer].setChecked(true);
                                    }
                                }
                            }).setNegativeButton("NO", new DialogInterface.OnClickListener(){
                                @Override
                                public void onClick(DialogInterface dialogInterface, int i){
                                    ExamActivity.this.finish();
                                }
                            }).show();
                }}
            }
        });
        btn_previous.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if(current > 0){
                    current--;
                    Question q = list.get(current);
                    radioGroup.clearCheck();
                    tv_question.setText(q.question);
                    radioButtons[0].setText(q.answerA);
                    radioButtons[1].setText(q.answerB);
                    radioButtons[2].setText(q.answerC);
                    radioButtons[3].setText(q.answerD);
                    tv_explanation.setText(q.explanation);
                    if(q.selectedAnswer != -1){
                        radioButtons[q.selectedAnswer].setChecked(true);
                    }
                }
            }
        });
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int i) {
                for(int j = 0; j < 4; j++){
                    if(radioButtons[j].isChecked() == true){
                        list.get(current).selectedAnswer = j+1;
                        break;
                    }
                }
            }
        });
    }
    private List checkAnswer(List list){
        List wrongList = new ArrayList();
        for(int i = 0; i < list.size(); i++){
            if(list.get(i).answer != list.get(i).selectedAnswer){
                wrongList.add(i);
            }
        }
        return wrongList;
    }
}
























indexActivity.java

 

 

package com.exam.administrator.exam;

import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;


public class IndexActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_index);
        // open or create question database
        SQLiteDatabase db = openOrCreateDatabase("question.db", Context.MODE_PRIVATE, null);
        db.execSQL("DROP TABLE IF EXISTS question");
        // create question table
        db.execSQL("CREATE TABLE `question` (`ID`	INTEGER, `question`	TEXT, `answerA`	TEXT, `answerB`	TEXT, `answerC`	TEXT, `answerD`	TEXT, `answer`	NUMERIC, `explanation`	TEXT, PRIMARY KEY(ID))");
        Question question1 = new Question();
        Question question2 = new Question();
        Question question3 = new Question();
        question1.question = "Oracle中VARCHAR2類型的最大長度是";
        question1.answerA = "4000";
        question1.answerB = "3000";
        question1.answerC = "1000";
        question1.answerD = "2000";
        question1.answer = 1;
        question1.explanation = "4000";
        question1.ID= 1;

        question2.question = "在Java中,負責對字節代碼解釋執行的是";
        question2.answerA = "應用服務器";
        question2.answerB = "虛擬機";
        question2.answerC = "垃圾回收器";
        question2.answerD = "編譯器";
        question2.answer = 2;
        question2.explanation = "虛擬機";
        question2.ID = 2;

        question3.question = "一個棧的輸入序列為1 2 3 4 5,則下列序列中不可能是棧得輸出序列的是";
        question3.answerA = "5 4 1 3 2";
        question3.answerB = "2 3 4 1 5";
        question3.answerC = "1 5 4 3 2";
        question3.answerD = "2 3 1 4 5";
        question3.answer = 1;
        question3.explanation = "5 4 1 3 2";
        question3.ID = 3;

        db.execSQL("INSERT INTO question VALUES(?,?,?,?,?,?,?,?)", new Object[]{question1.ID, question1.question,question1.answerA,question1.answerB,question1.answerC,question1.answerD, question1.answer, question1.explanation});
        db.execSQL("INSERT INTO question VALUES(?,?,?,?,?,?,?,?)", new Object[]{question2.ID, question2.question,question2.answerA,question2.answerB,question2.answerC,question2.answerD, question2.answer, question2.explanation});
        db.execSQL("INSERT INTO question VALUES(?,?,?,?,?,?,?,?)", new Object[]{question3.ID, question3.question,question3.answerA,question3.answerB,question3.answerC,question3.answerD, question3.answer, question3.explanation});


        /*Cursor c= db.rawQuery("select * from question",null);
        while(c.moveToNext()){
            int ID = c.getInt(c.getColumnIndex("ID"));
            String question = c.getString(c.getColumnIndex("question"));
            String answerA = c.getString(c.getColumnIndex("answerA"));
            String answerB = c.getString(c.getColumnIndex("answerB"));
            String answerC = c.getString(c.getColumnIndex("answerC"));
            String answerD = c.getString(c.getColumnIndex("answerD"));
            Log.i("db","ID=>" + ID + ", quesiton=>" + question + ", answerA=>" + answerA + ", answerB=>" + answerB + ", answerC=>" + answerC + ", answerD=>" + answerD );
            System.out.println(question);
        }*/
        //c.close();
        db.close();
        Button btn = (Button)findViewById(R.id.button);
        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(IndexActivity.this, ExamActivity.class);
                startActivity(intent);
            }
        });
    }
}

Question.java代碼

 

 

package com.exam.administrator.exam;

/**
 * Created by Administrator on 2016/8/15.
 */
public class Question {
    public String question;
    public String answerA;
    public String answerB;
    public String answerC;
    public String answerD;
    public int answer;
    public String explanation;
    public int ID;
    public int selectedAnswer;
}
  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved