【问题标题】:How to sort SQLite database onCreate, as well as onResume, based on sharedpreference?如何根据 sharedpreference 对 SQLite 数据库 onCreate 和 onResume 进行排序?
【发布时间】:2011-10-06 00:49:43
【问题描述】:

我正在尝试让我的应用程序中的数据库在数据通过自定义列表适配器传递到列表视图之前对其进行排序。我以为我已经弄清楚了,但是每次运行它时,我都会得到一个 FC。 onCreate 中的代码如下:

 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);

}

当我运行我的应用程序时,我得到一个 FC,查看 DDMS 我看到一个指向第 130 行的空指针异常,该异常位于我的 onResume 代码中:

 @Override
   public void onResume() {
    super.onResume();
    ((BaseAdapter) adapter1).notifyDataSetChanged();
    constantsCursor.requery();
    debtlist.setAdapter(adapter1);
    updateTotalProgress();
   }

有问题的行是 constantCursor.requery();一。在我的偏好 xml 中,我将默认设置为“id”,但我认为这不是问题。我还尝试在 onResume 代码中设置相同的代码来设置 constantCursor,基本上强制它重新创建适配器并将其分配给列表,但这只会给出相同的 NullPointer 错误。

关于如何根据偏好选择排序顺序,我有什么遗漏吗?

 <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>

【问题讨论】:

  • 强制关闭 :) 由于错误而自行退出。

标签: android sqlite sorting preferences listadapter


【解决方案1】:

我的猜测是,constantsCursor 永远不会被初始化,因为这些 if 条件都不满足。字符串是对象,因此您不能以这种方式将它们与 == 运算符进行比较(请参阅讨论in this post),因此您的所有条件都被评估为假。请改用Object.equals(other_object) 来测试等效性,如下所示:else if (sortorder.equals("id"))

另请注意,Cursor.requery() 已弃用。完成后最好在光标上调用 close() 并稍后再获取另一个。也许将该数据库代码移动到返回游标的私有方法中。然后从 onCreate() 和 onResume() 调用该私有方法,如果这是必要的话。

【讨论】:

  • 太棒了。我会试试这个。我有一种感觉,它不符合任何条件,但总是假设 == 与“等于”相同,再次感谢
猜你喜欢
  • 1970-01-01
  • 2012-07-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多