-1

My table name is DATABASE_TABLE2. I have 5 columns in that table. Now the statement I want to make is SELECT * DATABASE_TABLE2 Where REV_FACILITY = "Toilets"

And I want to display in an array list view.. This is the code I have tried and it is crashing. I know the problem is on the database method but i dont know what to do

private void displayListView() {


        Cursor cursor = dbHelper.getAllToilets();

        startManagingCursor(cursor);

        // The desired columns to be bound
        String[] columns = new String[]{

                SQL.KEY_REV_STATION_NAME,
                SQL.KEY_DATE,
                SQL.KEY_REV_FACILITY,
                SQL.KEY_RATING,
                SQL.KEY_COMMENT
        };

        // the XML defined views which the data will be bound to
        int[] to = new int[]{
                R.id.SRevName,
                R.id.SRevDate,
                R.id.SRevFacility,
                R.id.SRateBar,
                R.id.SRevComment,

        };

        // create the adapter using the cursor pointing to the desired data
        //as well as the layout information
        final SimpleCursorAdapter myCursorAdapter =
                new SimpleCursorAdapter(
                        this, R.layout.review_list,
                        cursor,
                        columns,
                        to);

        myCursorAdapter.setDropDownViewResource(android.R.layout.simple_selectable_list_item);

        ListView listView = (ListView) findViewById(R.id.ToiletReviews);
        // Assign adapter to ListView
        listView.setAdapter(myCursorAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {


            }
        });

    }
    public Cursor getAllToilets() {
        //String where = null;
        String where = KEY_REV_FACILITY = "Toilets";
        Cursor c = db.query(true, DATABASE_TABLE2, ALL_REV_KEYS,
                where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }
Diego Freniche
  • 5,225
  • 3
  • 32
  • 45
D4rkH34rt
  • 17
  • 5

2 Answers2

1

Look like here:

String where = KEY_REV_FACILITY = "Toilets";

You're assigning "Toilets" first to KEY_REV_FACILITY, then to where

Maybe you mean:

String where = KEY_REV_FACILITY + "= Toilets";

NOTE: if you're comparing Strings inside your SQLite DB I recommend you to use SELECT LIKE. Have a look at this SO answer.

Community
  • 1
  • 1
Diego Freniche
  • 5,225
  • 3
  • 32
  • 45
0

Change this:

   public Cursor getAllToilets() {
        //String where = null;
        String where = KEY_REV_FACILITY = "Toilets";
        Cursor c = db.query(true, DATABASE_TABLE2, ALL_REV_KEYS,
                where, null, null, null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }

To this:

public Cursor getAllToilets() {        
        String where = KEY_REV_FACILITY + "=?";
        String[] selectionArgs=new String[] {"Toilets"}
        Cursor c = db.query(DATABASE_TABLE2, columns,
                where, selectionArgs,null, null, null);
        if (c != null) {
            c.moveToFirst();
        }
        return c;
    }
Jemshit
  • 9,501
  • 5
  • 69
  • 106