【发布时间】: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