我想比较两个字符串。
s1 - 用户输入(String)和
s2 - dbHelper.getdata(s1)(String),搜索s1中的文本是否在数据库中
我的代码工作正常但我注意到一些奇怪的事情:
即使单词在数据库中,也不在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(); }我想通过这种方式:如果用户输入(s1)在数据库中(s2),则将用户输入添加到Listview 。 就像是:
if(s1==s2){ //add user input to listview)}如果用户输入(s1)不在数据库中,那么只需显示toast,而不是像我现在所关注的那样关闭应用程序。 就像是:
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但结果仍然相同。 急需帮助。
I am trying to compare two strings.
s1 - user input (String) and
s2 - dbHelper.getdata(s1) (String), search if the text in s1 is in the database
My code works fine BUT I noticed something strange :
Not adding the word/ user input (s1) in listview even if the word is in the database -- (I don't want this one) BUT whenever the word is not in my database it gives a toast which I want. (I use this code).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. Adding the word when i use the code below, which I want BUT closing the app when the word is not in the database.
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 IT THIS WAY: If the user input (s1) is in the database (s2) then add user input to Listview. Its like:
if(s1==s2){ //add user input to listview)}If the user input (s1) is NOT in the database then simply show toast, NOT closing the app like what I am into right now. its like:
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(); }I am using Android Studio and I have no idea about this so I am asking. I've done .contains, .equal but the result is still the same. Badly need help.
最满意答案
适配器不对您的数据负责,它负责将数据映射到ui元素。 因此,您需要将数据放入数据库,这将自动更改您的Cursor,而不是更新列表。 使用CursorAdapter或ContentObserver + ArrayAdpter来实现此功能。
Adapter is not responsible for your data, it is responsible for mapping data to ui elements. So you need to put your data to DB and that would change your Cursor automatically, than you will get your list updated. Use CursorAdapter or ContentObserver + ArrayAdpter to achieve this functionality.
更多推荐
发布评论