2011-10-06 25 views
0

我试图让我的应用程序中的数据库对数据进行排序,然后将其传递给具有自定义列表适配器的列表视图。我想我已经想通了,但每次运行它,我都会得到一个FC。在的onCreate代码如下:如何根据sharedpreference对SQLite数据库onCreate以及onResume进行排序?

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

有问题的行是constanstCursor.requery();一。在我的偏好XML中,我将默认设置为“ID”,但我不认为这是问题。我也试着把相同的代码放在onResume代码中设置constanstCursor,基本上强制它重新创建适配器并将它分配给列表,但这只是给出了相同的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> 
+0

反正什么是'FC'? –

+0

强制关闭:)由于缺陷导致它自行退出。 –

回答

1

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

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

+0

真棒。我会试试这个。我有一种感觉,它不符合任何条件,但总是认为==与“等于”相同,再次感谢 –

+0

谢谢你是这个问题。 –

相关问题