我試圖讓我的應用程序中的數據庫對數據進行排序,然後將其傳遞給具有自定義列表適配器的列表視圖。我想我已經想通了,但每次運行它,我都會得到一個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>
反正什麼是'FC'? –
強制關閉:)由於缺陷導致它自行退出。 –