0

I am trying to get text from getText method from database and display the string in the TextView. But I have no idea why it is not working.

Database:

 public String getType(String search){

        String type="";

        String searchTypeQuery="SELECT * FROM "+ DatabaseData.TABLE_NAME + " WHERE "+ DatabaseData.NAME+ " LIKE '%" +search +"%'";
        SQLiteDatabase db=this.getWritableDatabase();

        Cursor cursor=db.rawQuery(searchTypeQuery,null);
        // looping through all rows
        if (cursor.moveToFirst()) {
            do {

                String objectName = cursor.getString(cursor.getColumnIndex(DatabaseData.TYPE));
                type=objectName;

            } while (cursor.moveToNext());
        }

        cursor.close();
        db.close();

        // return the list of records
        return type;
    }

Main:

if (database.getType("Sample")=="A"){
                textview.setText("Type");
            }

I tried this "textview.setText(database.getType("Sample"));" and it worked. But when i try the above code it doesn't work.

Nelson John
  • 144
  • 2
  • 16

2 Answers2

1

Try database.getType("Sample").equals("A") as the if-condition. And here is explanation.

Community
  • 1
  • 1
user35603
  • 765
  • 2
  • 8
  • 24
1

You have to use equals method when you are comparing Strings. Remember that Strings are objects.

You can read this to know more info about comparing Strings: https://docs.oracle.com/javase/tutorial/java/data/comparestrings.html

Genaut
  • 1,810
  • 2
  • 29
  • 60