2011-10-06 54 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

謝謝你是這個問題。 –

相關問題