0

"QuoteTable" is a java class file that defines a database. The code below works perfectly fine.

// mCategory = (Spinner) findViewById(R.id.category); - created during OnCreate method 

private void fillData() {

// Fields from the database (projection)
// Must include the _id column for the adapter to work
String category = (String) mCategory.getSelectedItem();
String[] from = new String[] { QuoteTable.COLUMN_QUOTE };
// Fields on the UI to which we map
int[] to = new int[] { R.id.label };

getLoaderManager().initLoader(0, null, this);
adapter = new SimpleCursorAdapter(this, R.layout.motivate_row, null, from,
    to, 0);

setListAdapter(adapter); }

What I want to do is, select particular quotes from "QuoteTable" depending on the category selected in the Spinner. This category should match the COLUMN_CATEGORY field in "QuoteTable." So I tried this -

 private void fillData() {
String[] from = null;
// Fields from the database (projection)
// Must include the _id column for the adapter to work
if(category.equals("All")){
       from = new String[] { QuoteTable.COLUMN_QUOTE };
}
else{
        if(category.equals(QuoteTable.COLUMN_CATEGORY))
              from = new String[] { QuoteTable.COLUMN_QUOTE };
}
// Fields on the UI to which we map
int[] to = new int[] { R.id.label };

getLoaderManager().initLoader(0, null, this);
adapter = new SimpleCursorAdapter(this, R.layout.motivate_row, null, from,
    to, 0);

setListAdapter(adapter);  }

/* this error is resolved */ This gives me a "SimpleCursorAdapter" error at if(category == "All") Why is this the case? I've already used Cursor in another class to run a query in saving the database. This is merely my "View" and I want don't want to re-use that code.

Okay, now the previous (above mentioned) error is resolved. I had to fix the spinner in the layout file. However, although now there are no errors (runtime or compile time), the code logically does not do what I intend it to do. The spinner is present, but on selecting different options, nothing changes.

user2201545
  • 21
  • 1
  • 5

2 Answers2

0

String comparison should be done using .equals() method: "All".equals(category)

nikis
  • 11,166
  • 2
  • 35
  • 45
0

You should use .equals() to compare strings

if(category.equals("All")){

Check

What is the difference between == vs equals() in Java?

You have

adapter = new SimpleCursorAdapter(this, R.layout.motivate_row, null, from,
to, 0);

The third param is null should be a cursor

Community
  • 1
  • 1
Raghunandan
  • 132,755
  • 26
  • 225
  • 256