我试图让我的应用程序中的数据库对数据进行排序,然后将其传递给具有自定义列表适配器的列表视图。我想我已经想通了,但每次运行它,我都会得到一个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'? –
强制关闭:)由于缺陷导致它自行退出。 –