0

I am currently working on a project that will add a game to a database and then show that game and its information in a textview. I have 2 spinners, one for the first letter of the game, and one for the name of the system. I use the button Sort to assign the spinners to a string as shown here

Sort.setOnClickListener(new OnClickListener(){

                  @Override
                  //Clears the textview and gets info from the spinners
                  public void onClick(View v) {
                      view.setText("");
                      String system_pick = String.valueOf(system.getSelectedItem());
                      String letter_pick = String.valueOf(letters.getSelectedItem());
                   //displays a toast showing what is being searched for
                      Toast.makeText(SearchActivity.this,
                    "Sorting : " + 
                            "\n Letter: "+ String.valueOf(letters.getSelectedItem()) + 
                            "\n System : "+ String.valueOf(system.getSelectedItem()),
                            Toast.LENGTH_SHORT).show();
                    //Sends a query to the GameData class and returns a cursor  
                    Cursor results = gameData.select( system_pick, letter_pick);
                    results.moveToFirst();
                    //Displays the results in the textview
                    while (results.isAfterLast() == false) {
                        view.append("\n\n" + results.getString(1)+ "\n" + results.getString(2)+ "\n" + results.getInt(3));
                        results.moveToNext();
                    }
                    results.close();
                  }

                });

and the gameData.select() function is called which is here

public Cursor select( String system, String letter){
        SQLiteDatabase db = getWritableDatabase();
            Cursor cursor;

                cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE " + NAME + " LIKE " + "'" + letter + "%" + "'" + " AND " + SYSTEM + " LIKE " + "'" + system + "'",null);
            return cursor;
    }

The two spinners have an ALL choice i.e display all games in a system or all games with a specific letter in all systems. I cannot get a query to work that will return, say ALL for letters is selected and N64 is selected for system, it displays all games in N64. the cursor returns nothing.

I have tried

                if(letter=="ALL" && system !="ALL")
                    cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE "  +NAME + " = * "+ SYSTEM + " LIKE " + "'" + system + "'",null);
                else if(letter!="ALL" && system =="ALL")
                    cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE "  + NAME + " LIKE " + "'" + letter + "%" + "'" ,null);
                else if (letter=="ALL" && system =="ALL")
                    cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME,null);
                else
                    cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE " + NAME + " LIKE " + "'" + letter + "%" + "'" + " AND " + SYSTEM + " LIKE " + "'" + system + "'",null);
            return cursor;

but nothing worked. Any advice?

EDIT:

Everything is now working using

 if(letter.equals("ALL"))
                    cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE " + SYSTEM + " LIKE " + "'" + system + "'",null);
                else if(system.equals("ALL"))
                    cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE "  + NAME + " LIKE " + "'" + letter + "%" + "'" ,null);
                else if (letter.equals("ALL") && system.equals("ALL"))
                    cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME ,null);
                else
                    cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME +" WHERE " + NAME + " LIKE " + "'" + letter + "%" + "'" + " AND " + SYSTEM + " LIKE " + "'" + system + "'",null);

except for the

else if (letter.equals("ALL") && system.equals("ALL"))
                    cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME ,null);

part of this. It should display all games from all systems and letters but returns nothing.

Gallovous
  • 11
  • 3

0 Answers0