开发者

How to sort SQLite database onCreate, as well as onResume, based on sharedpreference?

开发者 https://www.devze.com 2023-04-10 19:41 出处:网络
I\'m trying to get the database in my application to sort the data before it gets passed to a listview with custom list adap开发者_运维百科ter.I thought I had it figured out, but every time I run it,

I'm trying to get the database in my application to sort the data before it gets passed to a listview with custom list adap开发者_运维百科ter. 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>


My guess is that constantsCursor is never getting intialized because none of those if-conditions are satisfied. Strings are objects, and so you can't compare them with the == operator this way (see discussion in this post), so all your conditions are evaluating to be false. Use Object.equals(other_object) to test for equivalency instead, like so: else if (sortorder.equals("id")).

Also note that Cursor.requery() is deprecated. Better to just call close() on the cursor when you're done with it and get another one later. Maybe move that database code into a private method that returns a Cursor. Then call that private method from both onCreate() and onResume(), if that's what is necessary.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号