我想比較兩個字符串。contentEquals not working
s1
- 用戶輸入(字符串)和
s2
- dbHelper.getdata(S1)(字符串),如果搜索S1中的文字是在數據庫
我的代碼工作但我注意到一些奇怪的東西:
- 即使單詞在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但結果仍然相同。非常需要幫助。
您需要執行equals,而不是contentEquals方法,這裏沒有CharSequence,只是普通字符串 – 2015-03-20 14:55:53
我已經試過了,如下所述,但結果仍然相同。 – user4687430 2015-03-20 15:09:00