2017-01-02 105 views
0

我想添加問題,選項,對數據庫的答案。我創建了具有5列的表格 id,QUESTION,OPTION 1,OPTION 2,OPTION 3ANSWER向數據庫添加問題

基本上,我試圖創建一個測驗類型的遊戲,所以我想在我的表中存儲問題,選項和答案。爲此,我創建了:

  • a Helperclass其中延伸SQLiteOpenHelper。具有構造函數需要5個參數即詢問,有三個選項和答案,並在
  • Questions類我mainactivity.xml

我沒有太多的只是一個TextView來顯示一個問題和三個按鈕顯示選項。

我得到的數據庫停止錯誤,但我知道我非常接近,有一些小錯誤。

的logcat的窗口說:

---產生的原因:java.lang.IllegalStateException:getDatabase遞歸調用。

MainActivity.java

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 
    helperclassAdapter = new HelperclassAdapter(this); 
    tv1 = (TextView) findViewById(R.id.labelquestion); 
    b1 = (Button) findViewById(R.id.opt1); 
    b2 = (Button) findViewById(R.id.opt2); 
    b3 = (Button) findViewById(R.id.opt3); 
    SQLiteDatabase sqLiteDatabase = helperclassAdapter.helperclass.getWritableDatabase(); 
} 

public void clickopt1(View view) { 

} 

public void clickopt2(View view) { 

} 

public void clickopt3(View view) { 

} 

HelperclassAdapter.java

public class HelperclassAdapter { 
Helperclass helperclass; 
Context context; 

public HelperclassAdapter(Context context) { 
    helperclass = new Helperclass(context); 
    this.context = context; 
} 
static public class Helperclass extends SQLiteOpenHelper { 
    //i added the SQLiteDatabase object over here and using this object  called insert method 
    SQLiteDatabase sqLiteDatabase; 
    Context context; 
    private static final String DATABASE_NAME = "DATABASE1"; 
    private static final int DATABASE_VERSION = 39; 
    private static final String TABLE_NAME = "TABLE1"; 
    private static final String UID = "_ID"; 
    private static final String QUESTION = "QUESTION"; 
    private static final String OPT1 = "OPT1"; 
    private static final String OPT2 = "OPT2"; 
    private static final String OPT3 = "OPT3"; 
    private static final String ANSWER = "ANSWER"; 
    private static final String CREATE_TABLE = 
      "CREATE TABLE " + TABLE_NAME + " (" + UID + " INTEGER PRIMARY KEY, " + QUESTION + " VARCHAR(255), " + OPT1 + " VARCHAR(255), " + OPT2 + " VARCHAR(255), " + OPT3 + " VARCHAR(255), " + ANSWER + " VARCHAR(255));"; 
    private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME; 


    public Helperclass(Context context) { 
     super(context, DATABASE_NAME, null, DATABASE_VERSION); 
     this.context = context; 
     Toast.makeText(context, "constructor called", Toast.LENGTH_SHORT).show(); 
    } 

    @Override 
    public void onCreate(SQLiteDatabase sqLiteDatabase) { 
     try { 
      sqLiteDatabase.execSQL(CREATE_TABLE); 
      addquestions(); 
      Toast.makeText(context, "oncreate called", Toast.LENGTH_SHORT).show(); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
      Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show(); 
     } 
    } 

    @Override 
    public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) { 
     try { 
      Toast.makeText(context, "onupgrade called", Toast.LENGTH_SHORT).show(); 
      sqLiteDatabase.execSQL(DROP_TABLE); 
      onCreate(sqLiteDatabase); 
     } catch (SQLException e) { 
      e.printStackTrace(); 
      Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show(); 
     } 
    } 
    public void addquestions() { 
     Questions q1 = new Questions("what is your hobby?", "singing", "dancing", "reading", "sining"); 
     this.addingeachquestions(q1); 
     Questions q2 = new Questions("how are you?", "fine", "good", "sad", "good"); 
     this.addingeachquestions(q2); 
     Questions q3 = new Questions("whats your name?", "sam", "jhon", "alice", "sam"); 
     this.addingeachquestions(q3); 
     Toast.makeText(context, "addquestion method called", Toast.LENGTH_SHORT).show(); 
    } 
    //BEFORE 
    private void addingeachquestions(Questions question) { 
     ContentValues values = new ContentValues(); 
     values.put(Helperclass.QUESTION, question.getQUESTION()); 
     values.put(Helperclass.OPT1, question.getOPTA()); 
     values.put(Helperclass.OPT2, question.getOPTB()); 
     values.put(Helperclass.OPT3, question.getOPTC()); 
     values.put(Helperclass.ANSWER, question.getANSWER()); 
     SQLiteDatabase db = getWritableDatabase(); 
     db.insert(Helperclass.TABLE_NAME, null, values); 
     Toast.makeText(context, "questions added", Toast.LENGTH_SHORT).show(); 
    } 
} 
    //AFTER 
    private void addingeachquestions(Questions question) { 
     ContentValues values = new ContentValues(); 
     values.put(Helperclass.QUESTION, question.getQUESTION()); 
     values.put(Helperclass.OPT1, question.getOPTA()); 
     values.put(Helperclass.OPT2, question.getOPTB()); 
     values.put(Helperclass.OPT3, question.getOPTC()); 
     values.put(Helperclass.ANSWER, question.getANSWER()); 
     sqLiteDatabase.insert(Helperclass.TABLE_NAME, null, values); 
     Toast.makeText(context, "questions added", Toast.LENGTH_SHORT).show(); 
    } 

Questions.java

public Questions() { 
    Question = ""; 
    Opt1 = ""; 
    Opt2 = ""; 
    Opt3 = ""; 
    Answer = ""; 
} 

public Questions(String qUESTION, String oPT1, String oPT2, String oPT3, 
       String aNSWER) { 
    Question = qUESTION; 
    Opt1 = oPT1; 
    Opt2 = oPT2; 
    Opt3 = oPT3; 

    Answer = aNSWER; 
} 

public String getQUESTION() { 
    return Question; 
} 

public String getOPTA() { 
    return Opt1; 
} 

public String getOPTB() { 
    return Opt2; 
} 

public String getOPTC() { 
    return Opt3; 
} 

public String getANSWER() { 
    return Answer; 
} 
+0

能否請您添加logcat的消息? –

+0

如果您確實是初學者,那麼您應該檢查Java中的命名約定。在代碼中獲得什麼是類或變量是很難的。你可以找到谷歌[那裏](https://source.android.com/source/code-style.html) –

+0

我已經把logcat窗口消息放在我上面編輯的問題中! –

回答

-1
  1. 我會建議你使用ContentProvider來使用數據庫。如果你是先行者,它看起來很可怕,但你必須學習它!

內容提供商是一種適用於您和其他應用程序(如果您選擇)以正確方式訪問數據庫的方式。

ContentProvider documentation

  • 我認爲馬力可變心不是intalized?

    @Override 
    public void onCreate(SQLiteDatabase sqLiteDatabase) { 
        try { 
         sqLiteDatabase.execSQL(CREATE_TABLE); 
    
    // hp isnt intallized? 
    
         hp.addquestions(); 
    
         Toast.makeText(context, "oncreate called", Toast.LENGTH_SHORT).show(); 
        } catch (SQLException e) { 
         e.printStackTrace(); 
         Toast.makeText(context, e.getMessage(), Toast.LENGTH_SHORT).show(); 
        } 
    } 
    
  • 閱讀我的意見,再次讀到的ContentProvider!它非常重要,不僅適用於創建自己的數據庫,還適用於您與其他數據庫一起使用。 (如從庫獲取圖像)

    private void addingeachquestions(Questions question) { 
     
         ContentValues values = new ContentValues(); 
     
         values.put(Helperclass.QUESTION, question.getQUESTION()); 
     
         values.put(Helperclass.OPT1, question.getOPTA()); 
     
         values.put(Helperclass.OPT2, question.getOPTB()); 
     
         values.put(Helperclass.OPT3, question.getOPTC()); 
     
         values.put(Helperclass.ANSWER, question.getANSWER()); 
     
    
     
    //getWritableDatabase calls the onCreate() methos and the onCreate() method calls    addquestions(); which calls getWriteableDatabase. this is recrusive call, a method that makes infinity loop that crush your app. 
     
    You should add the questions OUTSIDE the Helperclass. 
     
    
     
         SQLiteDatabase db = getWritableDatabase(); 
     
         db.insert(Helperclass.TABLE_NAME, null, values); 
     
         Toast.makeText(context, "questions added", Toast.LENGTH_SHORT).show(); 
     
        }

    +0

    是的,它已經initalized我已經編輯我的代碼上面請看看。 –

    +0

    logcat窗口正在說--- java.lang.IllegalStateException:遞歸地調用getDatabase。 –

    +0

    檢查我在你的代碼中的註釋(答案的bottum部分) –