I'm trying to get the database in my application to sort the data before it gets passed to a listview with custom list adapter. I thought I had it figured out, but every time I run it, I get a FC. Code in onCreate is below:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
debtlist = (ListView)findViewById(R.id.debtlist);
registerForContextMenu(debtlist);
//Retrieve the users sort order
SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
String sortorder = sp.getString("sortPref","id");
db=new DatabaseHelper(this);
// constantsCursor=db.getReadableDatabase().rawQuery("SELECT _ID, Debt, StartingAmount, CurrentAmount, InterestRate, DueDate, MinimumPayment "+
// "FROM tblDebts ORDER BY _ID", null);
if (sortorder == "intrate")
{
constantsCursor=db.getReadableDatabase().rawQuery("SELECT _ID, Debt, StartingAmount, CurrentAmount, InterestRate, DueDate, MinimumPayment "+
"FROM tblDebts ORDER BY InterestRate DESC", null);
}
else if (sortorder == "balance")
{
constantsCursor=db.getReadableDatabase().rawQuery("SELECT _ID, Debt, StartingAmount, CurrentAmount, InterestRate, DueDate, MinimumPayment "+
"FROM tblDebts ORDER BY CurrentAmount DESC", null);
}
else if (sortorder == "id")
{
constantsCursor=db.getReadableDatabase().rawQuery("SELECT _ID, Debt, StartingAmount, CurrentAmount, InterestRate, DueDate, MinimumPayment "+
"FROM tblDebts ORDER BY _ID", null);
}
adapter1=new ImgCursorAdapter(this,
R.layout.debt_row, constantsCursor,
new String[] {DbAdapter.KEY_DEBT,
DbAdapter.KEY_STARTINGAMOUNT},
new int[] {R.id.toptext, R.id.bottomtext});
debtlist.setAdapter(adapter1);
DbAdapter debtDbAdapter = new DbAdapter(this);
debtDbAdapter.open();
updateTotalProgress();
Cursor cursor = debtDbAdapter.queueAll();
startManagingCursor(cursor);
}
When I run my app, I get a FC, looking at DDMS i see a nullpointer exception that points to line 130, which is in my onResume code:
@Override
public void onResume() {
super.onResume();
((BaseAdapter) adapter1).notifyDataSetChanged();
constantsCursor.requery();
debtlist.setAdapter(adapter1);
updateTotalProgress();
}
The line in question is the constanstCursor.requery(); one. in my preferences xml, I set the default to be "id" but I don't think thats the issue. I've also tried putting the same code to set the constanstCursor in the onResume code, to basically force it to recreate the adapter and assign it to the list, but this just gives the same NullPointer errors.
Is there something I'm missing on how to pick a sort order based on preference?
<string-array name="sortArray">
<item>Highest Interest Rate</item>
<item>Highest Balance</item>
<item>No Sort</item>
</string-array>
<string-array name="sortValues">
<item>intrate</item>
<item>balance</item>
<item>id</item>
</string-array>