android答題系統(二):實現主介面入口和查詢資料

來源:互聯網
上載者:User

標籤:

 

main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"    xmlns:tools="http://schemas.android.com/tools"    android:layout_width="match_parent"    android:layout_height="match_parent"    android:paddingBottom="@dimen/activity_vertical_margin"    android:paddingLeft="@dimen/activity_horizontal_margin"    android:paddingRight="@dimen/activity_horizontal_margin"    android:paddingTop="@dimen/activity_vertical_margin"    tools:context="com.mytest.exam.MainActivity" >    <Button        android:id="@+id/btn_exam"        style="?android:attr/buttonStyleSmall"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_marginTop="52dp"        android:text="開始答題" /></RelativeLayout>
View Code

 

MainActivity

package com.mytest.exam;import java.io.File;import java.io.FileOutputStream;import java.io.IOException;import java.io.InputStream;import java.io.OutputStream;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.widget.Button;public class MainActivity extends Activity {    private Button btnExam;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        loadDB();        btnExam = (Button) this.findViewById(R.id.btn_exam);        btnExam.setOnClickListener(new View.OnClickListener() {            @Override            public void onClick(View v) {                Intent intent = new Intent(MainActivity.this, ExamActivity.class);                startActivity(intent);            }        });    }    /**     * 載入資料庫     */    private void loadDB() {        File file = new File(DBService.DB_PATH + DBService.DB_NAME);        if (file.exists() == false) {            File dir = new File(DBService.DB_PATH);            dir.mkdir();        }        try {            InputStream is = getBaseContext().getAssets().open(DBService.DB_NAME);            OutputStream os = new FileOutputStream(file);            byte[] buff = new byte[1024];            int len = 0;            while ((len = is.read(buff)) > 0) {                os.write(buff, 0, len);            }            os.flush();// 必須調用            os.close();            is.close();        } catch (IOException e) {            // TODO Auto-generated catch block            e.printStackTrace();        }    }}
View Code

 

DBService

package com.mytest.exam;import java.util.ArrayList;import java.util.List;import android.database.Cursor;import android.database.sqlite.SQLiteDatabase;public class DBService {    public static final String DB_PATH = "/data/data/com.mytest.exam/databases/";    public static final String DB_NAME = "question.db";    private SQLiteDatabase db;    public DBService() {        this.db = SQLiteDatabase.openDatabase(DB_PATH + DB_NAME, null, SQLiteDatabase.OPEN_READWRITE);    }    public List<Question> getQuestions() {        List<Question> list = new ArrayList<Question>();        Cursor cursor = db.rawQuery("select * from question ", null);        if (cursor != null && cursor.getCount() > 0) {            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.explain = cursor.getString(cursor.getColumnIndex("explaination"));                question.selectedAnswer = -1;                list.add(question);            }        }        return list;    }}
View Code

 

Question

package com.mytest.exam;public class Question {    public String question;    public String answerA;    public String answerB;    public String answerC;    public String answerD;    public int answer;    public String explain;    public int ID;    public int selectedAnswer;        public String getQuestion() {        return question;    }    public void setQuestion(String question) {        this.question = question;    }    public String getAnswerA() {        return answerA;    }    public void setAnswerA(String answerA) {        this.answerA = answerA;    }    public String getAnswerB() {        return answerB;    }    public void setAnswerB(String answerB) {        this.answerB = answerB;    }    public String getAnswerC() {        return answerC;    }    public void setAnswerC(String answerC) {        this.answerC = answerC;    }    public String getAnswerD() {        return answerD;    }    public void setAnswerD(String answerD) {        this.answerD = answerD;    }    public int getAnswer() {        return answer;    }    public void setAnswer(int answer) {        this.answer = answer;    }    public String getExplain() {        return explain;    }    public void setExplain(String explain) {        this.explain = explain;    }    public int getID() {        return ID;    }    public void setID(int iD) {        ID = iD;    }    public int getSelectedAnswer() {        return selectedAnswer;    }    public void setSelectedAnswer(int selectedAnswer) {        this.selectedAnswer = selectedAnswer;    }}
View Code

 

android答題系統(二):實現主介面入口和查詢資料

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.