2015-03-18 87 views
0

我想比較兩個字符串。contentEquals not working

s1 - 用戶輸入(字符串)和

s2 - dbHelper.getdata(S1)(字符串),如果搜索S1中的文字是在數據庫

我的代碼工作但我注意到一些奇怪的東西:

  1. 即使單詞在dat中,也不會在listview中添加單詞/用戶輸入(s1)基地 - (我不想要這個)但是,只要這個詞不在我的數據庫中,它就會給我想要的祝酒。 (我使用這個代碼)。

public void viewWord(View view) { 
 
     s1 = text.getText().toString(); 
 
     s2 = dbHelper.getData(s1); 
 
     newData = text.getText().toString(); 
 
     s1.trim(); 
 
     s2.trim(); 
 

 
     if (s1.isEmpty()) { 
 
      Toast.makeText(getApplicationContext(), "Tap tiles to form a word.", Toast.LENGTH_SHORT).show(); 
 
     } 
 

 
     //check if s1 == s2 
 
     if (s1.contentEquals(s2)) { 
 
      Toast.makeText(getApplicationContext(), "Word Found", Toast.LENGTH_SHORT).show(); 
 

 
      if (!myList.contains(newData)) { // No duplicate entry 
 

 
       //display score 
 
       calculate(); 
 
       adapter = (ArrayAdapter) wordList.getAdapter(); 
 
       adapter.add((String) newData); 
 
       adapter.notifyDataSetChanged(); 
 

 
      } 
 
     } else { 
 

 
       //check if s1 is not equal to s2 
 
       Toast.makeText(getApplicationContext(), "Word not found. Try a new one.", Toast.LENGTH_SHORT).show(); 
 
      }



2.添加單詞時,我使用下面的代碼,這是我想要的,但在關閉應用程序時,這個詞是不是在數據庫中。

public void viewWord(View view) { 
 
    s1 = text.getText().toString(); 
 
    s2 = dbHelper.getData(s1); 
 
    newData = text.getText().toString(); 
 
    s1.trim(); 
 
    s2.trim(); 
 

 
    if (s1.isEmpty()) { 
 
     Toast.makeText(getApplicationContext(), "Tap tiles to form a word.", Toast.LENGTH_SHORT).show(); 
 
    } 
 

 
     //check if s1 is not equal to s2 
 

 
     if (!s1.contentEquals(s2)) { 
 
      Toast.makeText(getApplicationContext(), "Word Found", Toast.LENGTH_SHORT).show(); 
 

 
      if (!myList.contains(newData)) { // No duplicate entry 
 
      //display score 
 
      calculate(); 
 
      adapter = (ArrayAdapter) wordList.getAdapter(); 
 
      adapter.add((String) newData); 
 
      adapter.notifyDataSetChanged(); 
 

 
      } 
 
     } else { 
 

 
       //check if s1 equal to s2 
 

 
       Toast.makeText(getApplicationContext(), "Word not found. Try a new one.", Toast.LENGTH_SHORT).show(); 
 
      }



I WANT這樣說: 如果用戶輸入(S1)是在數據庫中(S2),然後添加用戶輸入以Listview。它像:

if(s1==s2){ //add user input to listview)} 

如果用戶輸入(S1)是不是在數據庫中,然後簡單地顯示敬酒,不關閉應用程序像我到現在什麼。它像:

if(s1!=s2){ //show toast)} 

DBHelper

public String getData(String word) 
 
    { 
 
     SQLiteDatabase db = helper.getWritableDatabase(); 
 
     String[] columns={DBHelper.WORD, DBHelper.SCORE}; 
 
     Cursor cursor=db.query(DBHelper.TABLE_NAME, columns, DBHelper.WORD+ "= '"+word+"'", null, null, null, null); 
 
     StringBuffer buffer = new StringBuffer(); 
 
     while(cursor.moveToNext()) 
 
     { 
 
      int index1 = cursor.getColumnIndex(DBHelper.WORD); 
 
      int index2 = cursor.getColumnIndex(DBHelper.SCORE); 
 
      String words = cursor.getString(index1); 
 
      String score = cursor.getString(index2); 
 
      buffer.append(score +"\n"); 
 
     } 
 
     return buffer.toString(); 
 
    }

我使用過Android Studio,我不知道這個,所以我問。我做了.contains,.equal但結果仍然相同。非常需要幫助。

+0

您需要執行equals,而不是contentEquals方法,這裏沒有CharSequence,只是普通字符串 – 2015-03-20 14:55:53

+0

我已經試過了,如下所述,但結果仍然相同。 – user4687430 2015-03-20 15:09:00

回答

0

適配器不負責你的數據,它負責將數據映射到UI元素。所以你需要把你的數據放到數據庫中,這樣會自動改變你的光標,比你更新列表更新。使用CursorAdapter或ContentObserver + ArrayAdpter來實現此功能。

+0

哦。你可以請我的代碼爲我的適配器?我不太熟悉CursorAdapter或ContentObserver。我編輯了上面的問題。 – user4687430 2015-03-20 15:22:07

+0

我給你的關鍵字,在互聯網上有很多例子,所以:創建CursorAdapter,例如 - https://github.com/codepath/android_guides/wiki/Populating-a-ListView-with-a-CursorAdapter。之後,在數據庫中插入新的單詞而不是在您的案例中適配器dbHelper.insertData(s1); – 2015-03-20 15:35:45

0

TRIM()函數返回的字符串修剪,所以如果你有改變的S1的價值和S2修整值,這樣做:

s1 = s1.trim(); 
s2 = s2.trim(); 

而現在相應的檢查值。 (等於()可以用在這裏,我猜)

+0

我使用trim(),因爲每當用戶在我的文本視圖中放入東西時,它會給出空格。並且我無法在我的數據庫中找到任何東西 – user4687430 2015-03-18 22:58:55

+0

仍然無法正常工作 – user4687430 2015-03-19 01:11:19

+0

您能否正確縮進代碼然後提出您有問題。在解釋你的問題時要更清楚些。 – 2015-03-19 12:01:09