2014-01-19 96 views
0

下面的方法負責將新記錄插入到數據庫表中,並在按下button時調用它。然而,每當輸入新值時,被分配了EditText的內容的值的字符串輸入從未被分配給新內容。Android:EditText返回與內容相同的值

因此,插入新記錄僅結果一次。

任何建議爲什麼發生這種情況將不勝感激。

public void insertRecord(View additionBut) { 
     System.out.println("NOW INSIDE THE INSERT RECORD"); 
     input = addTextInput.getText().toString(); 
     addTextInput.getText().clear(); 
     System.out.println(input); 
     if (purpose.equals("ViewNovel")) { 
      md.addPiece(input, "0"); 
     } else if (purpose.equals("ViewPlay")) { 
      md.addPiece(input, "1"); 
     } else { 
      // whatever else 
     } 

     displayList(); 
    } 
+0

什麼是對象稱爲目的和additionBut也從來沒有使用過,這是故意的? – XWaveX

+0

'purpose'是一個持有插入目的的字符串,即如果正在查看小說,它會將'input'和代表小說的0一起插入。 View additionBut參數是因爲在XML中,被按下的按鈕(按鈕)調用此方法。 – otherdan

回答

1

您也可以清除以另一種方式您的EditText,試試下面的方法,

public void insertRecord(View additionBut) { 
     System.out.println("NOW INSIDE THE INSERT RECORD"); 
     input = addTextInput.getText().toString(); 
     addTextInput.setText("")        // Change here 
     System.out.println(input); 
     if (purpose.equals("ViewNovel")) { 
      md.addPiece(input, "0"); 
     } else if (purpose.equals("ViewPlay")) { 
      md.addPiece(input, "1"); 
     } else { 
      // whatever else 
     } 

     displayList(); 
    } 
+0

我也試過這個,但不幸的是,然後輸入仍然是一個空字符串,問題似乎是鍵入到EditText的新文本沒有被分配給'輸入'。 – otherdan

+0

只是從一個小代碼,我不能假設你想要做什麼。你應該上傳更多相關的代碼。 – Lucifer

+0

我在這裏有一個更完整的代碼清單:http://stackoverflow.com/questions/21216687/android-method-to-add-to-database-attempts-to-add-same-record-twice?noredirect=1# comment31954121_21216687 – otherdan

0

廣場

displayList(); 
addTextInput.getText().clear(); //after displayList(); 

可能是參考相同。正因爲如此,當您嘗試清除編輯文本時,輸入中的值會得到反映。如果是這樣,請嘗試使用編輯文本框中的值創建新的String對象。

相關問題