我无法将我的数据库数据显示到tablelayout中

时间:2014-03-26 12:35:20

标签: android

我尝试将我的quizapp的高分显示到表格布局值从数据库中获取 所以PLZ帮我...告诉我任何代码行中的问题 "这是我的DBHandler类"

public class Database extends SQLiteOpenHelper{
        private static final int DATABASE_VERSION = 1;
        private static final String DATABASE_NAME = "QuestData";
        private static final String DATABASE_SCORE = "HScore";
        private static final String TABLE_QUEST = "quest";
        private static final String KEY_ID = "id";
        private static final String KEY_QUEST = "quest";
        private static final String KEY_ANS = "answer";
        private static final String KEY_OPTION1= "option1"; 
        private static final String KEY_OPTION2= "option2";
        private static final String KEY_OPTION3= "option3";

    static final String scoreTable="ScoreTab";
    static final String score="Score";
    static final String pName="PlayerName";

    private SQLiteDatabase database;
    public Database(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
}


    @Override
    public void onCreate(SQLiteDatabase db) {

        database=db;
        db.execSQL("CREATE TABLE "+scoreTable+" ("+score+ " INTEGER PRIMARY KEY , "+
                pName+ " TEXT)");
        addScore(null);
        String sql = "CREATE TABLE IF NOT EXISTS" + TABLE_QUEST + "("
                + KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT," + KEY_QUEST
                + " TEXT, " + KEY_OPTION1 +" TEXT,"
                + KEY_OPTION2 +" TEXT, " + KEY_OPTION3 + " TEXT, " + KEY_ANS + " TEXT)";
        db.execSQL(sql);        
        addQuest();
        }

    private void addQuest() {
        Quest q1=new Quest("(1).Which company is the largest manufacturer" +
                " of network equipment?","HP","IBM","CISCO","CISCO");
        this.addQuest(q1);
        Quest q2=new Quest("(2).Which of the following is NOT " +
                "an operating system?","SuSe","BIOS","DOS","BIOS");
        this.addQuest(q2);
        Quest q3=new Quest("(3).Which of the following is the fastest" +
                " writable memory?","RAM","FLASH","Register","Register");
        this.addQuest(q3);
        Quest q4=new Quest("(4).Which of the following device" +
                " regulates internet traffic?","Router","Bridge","PenDrive","Router");
        this.addQuest(q4);
        Quest q5=new Quest("(5).Which of the following is NOT an" +
                " interpreted language?","Ruby","Python","BASIC","BASIC");
        this.addQuest(q5);
        Quest q6=new Quest("(6).Which Cricket player scores fastest hundred in ODI" +
                " interpreted language?","Afridi","Sachin","Ponting","Afridi");
        this.addQuest(q6);
        Quest q7=new Quest("(7).Who is the richest Person in the world" +
                " ?","BillGates","Ashish","Ambani","Billgates");
        this.addQuest(q7);
        Quest q8=new Quest("(8).What is the full form of ORM" +
                " ?","Object Relation Module","Object Ralation Map","Operation Resource Map","Object Ralation Map");
        this.addQuest(q8);
        Quest q9=new Quest("(9).Full form of OODP" +
                " ?","Object Operating Design Pattern","Object Of Design Pattern","Object Oriented Design Pattern","Object Oriented Design Pattern");
        this.addQuest(q9);
        Quest q10=new Quest("(10).Who is the fastest baller in the world" +
                " ?","Shoib","Munaf","BretLee","BretLee");
        this.addQuest(q10);

        }


    private void addQuest(Quest quest) {

        ContentValues values = new ContentValues();
        values.put(KEY_QUEST,quest.getQUEST()); 
        values.put(KEY_ANS,quest.getANSWER());
        values.put(KEY_OPTION1,quest.getOPTION1());
        values.put(KEY_OPTION2,quest.getOPTION2());
        values.put(KEY_OPTION3,quest.getOPTION3());
    }
    public void addScore(HScore hScore)
    {
        //SQLiteDatabase db=this.getWritableDatabase();
        ContentValues values=new ContentValues();
        values.put(score, hScore.getNAME());
        values.put(pName, hScore.getSCORE());
        database.insert(scoreTable, null, values);
    }
    public int rowcount()
    {
        int row=0;
        String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
        SQLiteDatabase database = this.getWritableDatabase();
        Cursor cursor = database.rawQuery(selectQuery,null);
        row=cursor.getCount();
        return row;
    }

    public List<Quest>getAllQuestions() {
        List<Quest>queList = new ArrayList<Quest>();

        String selectQuery = "SELECT  * FROM " + TABLE_QUEST;
        database=this.getReadableDatabase();
        Cursor cursor = database.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                Quest question = new Quest();
                question.setID(cursor.getInt(0));
                question.setQUEST(cursor.getString(1));
                question.setANSWER(cursor.getString(2));
                question.setOPTION1(cursor.getString(3));
                question.setOPTION2(cursor.getString(4));
                question.setOPTION3(cursor.getString(5));
                queList.add(question);
            } while (cursor.moveToNext());
        }

        return queList;
    }
    public List<HScore>getScore() {
        List<HScore>scoreList = new ArrayList<HScore>();

        String selectQuery = "SELECT  * FROM " + scoreTable;
        database=this.getReadableDatabase();
        Cursor cursor = database.rawQuery(selectQuery, null);

        if (cursor.moveToFirst()) {
            do {
                HScore score = new HScore();
                score.setSID(cursor.getInt(0));
                score.setNAME(cursor.getString(1));
                score.setSCORE(cursor.getString(2));
                scoreList.add(score);
            } while (cursor.moveToNext());
        }

        return scoreList;
    }

    @Override
    public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
        database.execSQL("DROP TABLE IF EXISTS " + TABLE_QUEST);
        database.execSQL("DROP TABLE IF EXISTS " + scoreTable);
        onCreate(database);
    }

}

&#34;在这里我的活动&#34;

public class High_ScoreActivity extends Activity {
    List<HScore> scoreList;
    TextView txtName,txtScore;
    int sid=0;
    HScore hs;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_high__score);
        Database  database = new Database(this);
        scoreList = database.getScore();
        hs = scoreList.get(sid);
        txtName=(TextView)findViewById(R.id.tvName1);
        txtScore=(TextView)findViewById(R.id.tvScore1);

        setScoreView();
    }
    private void setScoreView() {
        txtScore.setText(hs.getNAME());
        txtScore.setText(hs.getSCORE());
    }
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.high__score, menu);
        return true;
    }

}

&#34; Here Activty_Layout&#34;

<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=".High_ScoreActivity" >

    <Button
        android:id="@+id/btn3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="45dp"
        android:text="@string/btn" />

    <TableLayout
        android:id="@+id/tlHighScore"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="28dp"
        android:layout_marginTop="30dp" >

        <TableRow
            android:id="@+id/tableRow1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/tvName1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <TextView
            android:id="@+id/tvScore1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/tvName2"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <TextView
            android:id="@+id/tvScore2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/tvName3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <TextView
            android:id="@+id/tvScore3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/tvName4"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <TextView
            android:id="@+id/tvScore4"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
        </TableRow>

        <TableRow
            android:id="@+id/tableRow5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" >

            <TextView
                android:id="@+id/tvName5"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="TextView" />

            <TextView
            android:id="@+id/tvScore5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="TextView" />
        </TableRow>
    </TableLayout>
</RelativeLayout>

0 个答案:

没有答案