2011-12-30 98 views
0

我正在尋找遊戲的問題和answers.i採取了textview和4 radio組buttons.and我從資產文件夾從外部數據文件中提取數據。當我安裝應用到它的工作原理fine.when我重新打開應用程序在它只是顯示沒有顯示在無線電buttons.here任何文本問題emulater的emulater是我在數據庫文件中的代碼應用程序第二次打開時不顯示數據

public String makeatext(String My_database_table,int i) { 
    SQLiteDatabase myDB = getDatabase(); 
    String results = new String(); 

    try { 
     String firstColumn = "questions"; 
    // final String KEY_ROWID = "id"; 

     // Cursor c = myDB.rawQuery("SELECT questions FROM " + 
     // My_database_table+ ";",null); 
     Cursor c = myDB.query(true, My_database_table, 
       new String[] { firstColumn },null, null, null, null, null, 
       null); 


     int iquestion = c.getColumnIndex(firstColumn); 
     if(c.moveToPosition(i)){ 
     results = c.getString(iquestion)+"\n"; 
     } 

     //while (c.moveToPosition(1)) { 
      //String firstName = c.getString(iquestion); 
      //results =(" "+ firstName + " "); 
     //} 
     return results; 


    } catch (Exception e) { 
     Log.e("ERROR","ERROR in Make test file :"+e.toString()); 
     e.printStackTrace(); 
     // TODO: handle exception 
    } 

    return results; 
} 

和活動文件中,我只是把它叫做

String shoow = myDb.makeatext("question", Qno); 

showQues tion.setText(shoow); 和oncreate方法的頂部我啓動數據庫爲private final DataBaseHelper myDb = new DataBaseHelper(this); 任何人都可以說我爲什麼這是happenig.do我需要在活動文件中寫for循環也或我應該在活動類中的遊標。

PLZ幫我提前

感謝

單選按鈕在數據庫文件中的代碼如下,因爲我有4個按鈕的4個按鈕的代碼會因爲這

爲相同
public String makeExtra1(String My_database_table ,int positions) { 
    String results = new String(); 
    try { 
     String secondColumn = "Extra1"; 
     Cursor c = myDataBase.query(true, My_database_table, 
       new String[] { secondColumn }, null, null, null, null, null, 
       null); 
     int iExtra1 = c.getColumnIndex(secondColumn); 
     if(c.moveToPosition(positions)){ 
      results = results+c.getString(iExtra1)+"\n"; 
     } 
     return results; 
    } catch (Exception e) { 
     Log.e("ERROR","ERROR in Make test file :"+e.toString()); 
     e.printStackTrace(); 
     // TODO: handle exception 
    } 
    return results;} 

和在活動文件

String showextra1 = myDb.makeExtra1("question", Qno); 
r0.setText(showextra1); 

我repeted THI如同改變makeExtra2,3,4並將其分配給r1,r2,r3一樣。

+0

和你在哪裏設置你的單選按鈕的文本?請添加它的代碼。 – Hiral 2011-12-30 06:57:02

+0

你能提供你的'Activity'類的完整代碼嗎? – Jin35 2011-12-30 07:36:37

+0

可能是你需要重寫onResume setText – 2011-12-30 07:55:40

回答

0
  1. 您應該從數據中讀取數據後致電Cursor.close()。最好做到這一點finally{}塊。

  2. 如果您只想提出每個查詢一個答案 - 填寫where參數myDataBase.query()

+0

jin35因爲我想添加c.close();在finally {}塊,它給出了一個錯誤,因爲在eclipse – MADDY 2011-12-30 09:03:31

+0

oohhh oohhh謝謝花了一個洞的一天後,終於我得到了應用程序正常運行,因爲我想要 – MADDY 2011-12-30 11:33:06

0

的可達代碼錯誤是因爲你寫後返回的結果最後{}塊;

在finally {}塊後面移動這一行,eclipse不會給你任何錯誤。

也請使用myDB.close();在這最後的塊。

相關問題