2010-06-17 31 views
0

我的問題是,只有最後一個選項文本正在改變點擊下一步按鈕,其餘的保持不變。有一些問題與環路我think.Can有人排序了這一點please.Thank你安卓測驗應用程序需要的幫助

這裏的DB類:

package com.myapps.quiz; 

import java.io.File; 
import java.io.FileOutputStream; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.OutputStream; 

import android.content.ContentValues; 
import android.content.Context; 
import android.database.Cursor; 
import android.database.SQLException; 
import android.database.sqlite.SQLiteDatabase; 
import android.database.sqlite.SQLiteException; 
import android.database.sqlite.SQLiteOpenHelper; 

public class DataBaseHelper extends SQLiteOpenHelper{ 

    //The Android's default system path of your application database. 
    private static String DB_PATH = "/data/data/com.myapps.quiz/databases/"; 
    private static String DB_NAME = "quiz"; 
    private static String Table_name="Quiz"; 

    private SQLiteDatabase myDataBase; 
    private SQLiteDatabase myData; 
    private final Context myContext; 

    /** 
    * Constructor 
    * Takes and keeps a reference of the passed context in order to access to the application assets and resources. 
    * @param context 
    */ 
    public DataBaseHelper(Context context) { 
     super(context, DB_NAME, null, 1); 
     this.myContext = context; 
    } 



/** 
    * Creates a empty database on the system and rewrites it with your own database. 
    * */ 
    public void createDataBase() throws IOException{ 

     boolean dbExist = checkDataBase(); 
     if(dbExist){ 
      //do nothing - database already exist 
     }else{ 
      CopyFiles(); 
     } 
    } 

    private void CopyFiles() 
    { 
     try 
     { 
      InputStream is = myContext.getAssets().open(DB_NAME); 
      File outfile = new File(DB_PATH,DB_NAME); 
      outfile.getParentFile().mkdirs(); 
      outfile.createNewFile(); 

      if (is == null) 
      throw new RuntimeException("stream is null"); 
      else 
      { 
      FileOutputStream out = new FileOutputStream(outfile);  
      // BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(outfile)); 
       byte buf[] = new byte[128]; 
       do { 
       int numread = is.read(buf); 
        if (numread <= 0) 
         break; 
       out.write(buf, 0, numread); 
       } while (true); 

       is.close(); 
       out.close(); 
      } 
      //AssetFileDescriptor af = am.openFd("world_treasure_hunter_deluxe.apk"); 
     } 
     catch (IOException e) 
     { 
       throw new RuntimeException(e); 
     } 

    }  

    /** 
    * Check if the database already exist to avoid re-copying the file each time you open the application. 
    * @return true if it exists, false if it doesn't 
    */ 
    private boolean checkDataBase(){ 

     SQLiteDatabase checkDB = null; 

     try{ 
      String myPath = DB_PATH + DB_NAME; 
      checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

     }catch(SQLiteException e){ 

     } 

     if(checkDB != null){ 
      checkDB.close(); 
     } 

     return checkDB != null ? true : false; 
    } 

    /** 
    * Copies your database from your local assets-folder to the just created empty database in the 
    * system folder, from where it can be accessed and handled. 
    * This is done by transfering bytestream. 
    * */ 
    private void copyDataBase() throws IOException{ 

     //Open your local db as the input stream 
     InputStream myInput = myContext.getAssets().open(DB_NAME); 

     // Path to the just created empty db 
     String outFileName = DB_PATH + DB_NAME; 

     //Open the empty db as the output stream 
     OutputStream myOutput = new FileOutputStream(outFileName); 

     //transfer bytes from the inputfile to the outputfile 
     byte[] buffer = new byte[1024]; 
     int length; 
     while ((length = myInput.read(buffer))>0){ 
      myOutput.write(buffer, 0, length); 
     } 

     //Close the streams 
     myOutput.flush(); 
     myOutput.close(); 
     myInput.close(); 

    } 

    public void openDataBase() throws SQLException{ 

     //Open the database 
     String myPath = DB_PATH + DB_NAME; 
     myDataBase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

    } 

    @Override 
    public synchronized void close() { 

      if(myDataBase != null) 
       myDataBase.close(); 

      super.close(); 

    } 

    @Override 
    public void onCreate(SQLiteDatabase db) { 

    } 

    @Override 
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 

    } 



     /// Get Book content//////// 
     public Cursor getQuiz_Content(int bookId)  
     { 
     String myPath = DB_PATH + DB_NAME; 
     myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);  

     Cursor cur; 
     cur=myData.rawQuery("select quiz_text from Quiz where quiz_id='"+bookId+"'",null); 
     cur.moveToFirst(); 

     myData.close(); 
     return cur; 
     }; 
     ////////////////////////// 



     /// Get Book content//////// 
     public Cursor getQuiz_List()  
     { 
     String myPath = DB_PATH + DB_NAME; 
     myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY);  
     int i; 

     Cursor cur; 
     cur=myData.rawQuery("select quiz_id,quiz_text,correct_answer from quiz",null); 
     cur.moveToFirst(); 
     i = cur.getCount(); 
     myData.close(); 
     return cur; 
     }; 
     ////////////////////////// 


     public Cursor getAns(int quizid) 
     { 
      String myPath = DB_PATH + DB_NAME; 
      myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

      Cursor cur; 
      cur = myData.rawQuery("select answers from answer where quiz_id='"+quizid+"'", null); 
      cur.moveToFirst(); 
      myData.close(); 
      return cur; 
     } 

     public Cursor getAnsList() 
     { 
      String myPath = DB_PATH + DB_NAME; 
      myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 

      Cursor cur; 
      cur = myData.rawQuery("select answers from answer", null); 
      cur.moveToFirst(); 
      myData.close(); 

      return cur; 
     } 



     //---updates a title--- 
    /* public boolean UpdateFavourite_Individual(long rowid,String fav) 
     { 
     String myPath = DB_PATH + DB_NAME; 
     myData = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE); 

      ContentValues args = new ContentValues(); 
      args.put("bookmark", fav); 
      return myData.update("lyrics", args, 
          "rowid=" + rowid, null) > 0;      
     }*/ 
     ////////////////// 


} 

這裏的Quiz.java:

package com.myapps.quiz; 

import java.io.IOException; 



import android.app.Activity; 
import android.database.Cursor; 
import android.os.Bundle; 
import android.util.Log; 
import android.view.View; 
import android.widget.Button; 
import android.widget.LinearLayout; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 

public class Quiz extends Activity{ 
    /** Called when the activity is first created. */ 
    final DataBaseHelper db = new DataBaseHelper(this); 

    Cursor c3; 
    Cursor c4; 
    int counter=1; 
    String label; 


    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     String options[] = new String[19]; 
    // get reference to radio group in layout 
     RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1); 
     // layout params to use when adding each radio button 
     LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
       RadioGroup.LayoutParams.WRAP_CONTENT, 
       RadioGroup.LayoutParams.WRAP_CONTENT); 


     c4 = db.getAnsList(); 
     c4.moveToFirst(); 
     for (int k=0;k<19;k++) 
     {  
      options[k]=c4.getString(0); 
      c4.moveToNext(); 
      Log.e("value:", options[k]); 
     } 

     for (int i = 0; i < 4; i++){ 
      final RadioButton newRadioButton = new RadioButton(this); 
      c3 = db.getAns(3); 

     for (int j=0;j<i;j++)  
      c3.moveToNext(); 
      label = c3.getString(0); 

     newRadioButton.setText(label); 
     newRadioButton.setId(6); 
     radiogroup.addView(newRadioButton, layoutParams); 
     //getOptions(); 



     try { 
      db.createDataBase(); 
      db.openDataBase(); 
     } catch (IOException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     final Cursor c1; 

     c1= db.getQuiz_Content(counter); 






     //rdoAns1.setText("Lisbon"); 

     final TextView tvQuizText = (TextView)findViewById(R.id.TextView01); 
     tvQuizText.setText(c1.getString(0)); 

     Button btnNext = (Button)findViewById(R.id.btnNext); 
     btnNext.setOnClickListener(new View.OnClickListener() { 


      @Override 
      public void onClick(View v) { 
       // TODO Auto-generated method stub 

        Cursor c2 = null ; 
        Cursor c3 =null;  

        if (counter < 5) 
        { 
        counter +=1;  
        c2 = db.getQuiz_Content(counter); 
        c3 = db.getAns(counter); 
        //Log.e("Row Position after increment:",Integer.toString(c2.getPosition())); 
        tvQuizText.setText(c2.getString(0)); 
        } 



        for (int j=0;j<3;j++)  
        { 
         c3.moveToNext(); 
         label = c3.getString(0); 

        newRadioButton.setText(label); 

        } 

        //Log.e("value:",Integer.toString(counter)); 
        //Log.e("value",c3.getString(0)); 
      } 
     }); 
     //c3 = db.getAns(4); 
    // rdoAns1.setText(c3.getString(0)); 
     //rdoAns2.setText(c3.getString(1)); 
     //rdoAns3.setText(c3.getString(2)); 
     //rdoAns4.setText(c3.getString(3)); 
    } 
    /** add radio buttons to the group */ 
    // private void getOptions() 
    { 

     // get reference to radio group in layout 
     //RadioGroup radiogroup = (RadioGroup) findViewById(R.id.rdbGp1); 
     // layout params to use when adding each radio button 
     //LinearLayout.LayoutParams layoutParams = new RadioGroup.LayoutParams(
      //  RadioGroup.LayoutParams.WRAP_CONTENT, 
      // RadioGroup.LayoutParams.WRAP_CONTENT); 

     // add 20 radio buttons to the group 

     //for (int i = 0; i < 4; i++){ 
      // RadioButton newRadioButton = new RadioButton(this); 
      // c3 = db.getAns(4); 
     //for (int j=0;j<i;j++)  
      // c3.moveToNext(); 
      // String label = c3.getString(0); 

     //newRadioButton.setText(label); 
     //newRadioButton.setId(i); 
     //radiogroup.addView(newRadioButton, layoutParams); 
     } 

    } 

    } 

這裏是我的XML佈局:

<AbsoluteLayout android:id="@+id/AbsoluteLayout01" android:layout_width="fill_parent" android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android"> 


       <RadioGroup 
       android:id="@+id/rdbGp1" 
       android:layout_width="wrap_content" 
       android:layout_height="wrap_content" 
       android:layout_y="30dip" 
       xmlns:android="http://schemas.android.com/apk/res/android" 
       android:orientation="vertical" 
       > 
       </RadioGroup> 


<Button android:text="Save" android:id="@+id/btnSave" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_x="50dip" android:layout_y="250dip"></Button> 
<Button android:text="Next" android:id="@+id/btnNext" android:layout_width="100dip" android:layout_height="wrap_content" android:layout_x="175dip" android:layout_y="250dip"></Button> 

<TextView android:layout_x="17dip" android:layout_y="15dip" android:id="@+id/TextView01" android:layout_width="wrap_content" android:text="@+id/TextView01" android:layout_height="wrap_content"></TextView> 






</AbsoluteLayout> 
+2

將示例代碼添加到您的問題是非常好的,但這是很多代碼。哪個循環在哪裏?是對按鈕作出反應的聽衆嗎?請僅發佈相關代碼,而不是完整的應用程序。 – Janusz 2010-06-17 10:23:15

+0

這是將文本分配給單選按鈕的代碼: for(int j = 0; j <3; j ++) { c3.moveToNext(); label = c3.getString(0); newRadioButton.setText(label); } – 2010-06-18 06:12:03

+0

此循環位於按鈕單擊偵聽器中。 – 2010-06-18 06:32:16

回答

2

如果我知道了這一點,並且代碼與所有大括號有點混淆,那麼您在for循環中創建RadioButtons?您創建newRadioButton並將其添加到佈局。之後完成,你有newRadioButton,這是你創建的最後一個。當你嘗試在NewRadioButton上設置文本時,你所做的唯一的事情就是將文本設置在同一個單選按鈕上三次。

所以你必須使用他們的RadioGroup單獨獲取每個RadioButton。

我希望有所幫助。

+0

我同意你的意見。我有4個單選按鈕,其文本在以下循環中設置: for(int j = 0; j <3; j ++){c3.moveToNext(); label = c3.getString(0); newRadioButton.setText(標籤); } 問題是,他們的名字是一樣的......對嗎?還是其他的東西?請解釋。 – 2010-06-18 06:20:53

+1

他們的名字不一樣,但是你有一個最終變量newRadioButton,當你完成你的第一個循環時,你創建的最後一個RadioButton將是newRadioButton。所以當你進入另一個循環時,你所做的就是在相同的單選按鈕上設置文本3次。 解決方法是從他們的父無線電組中獲取每個單選按鈕: for(int j = 0; j <3; j ++){ c3.moveToNext(); ((RadioButton)radiogroup.getChildAt(j))。setText(c3.getString(0)); } – Rabas 2010-06-18 09:03:59

+0

謝謝我解決了我的問題。現在我正在進行測驗的得分部分。爲此,我需要獲取用戶所選單選按鈕的文本。 我該怎麼做? – 2010-06-18 14:03:45