Android 如何根据SharedReference对SQLite数据库进行一次创建和一次恢复排序?
我试图让我的应用程序中的数据库在数据通过自定义列表适配器传递到listview之前对数据进行排序。我以为我已经弄明白了,但每次我运行它,我都会得到一个FC。onCreate中的代码如下所示:Android 如何根据SharedReference对SQLite数据库进行一次创建和一次恢复排序?,android,sqlite,sorting,preferences,listadapter,Android,Sqlite,Sorting,Preferences,Listadapter,我试图让我的应用程序中的数据库在数据通过自定义列表适配器传递到listview之前对数据进行排序。我以为我已经弄明白了,但每次我运行它,我都会得到一个FC。onCreate中的代码如下所示: public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); debtlist = (ListView)findViewB
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,基本上强制它重新创建适配器并将其分配给列表,但这只会产生相同的null指针错误
关于如何根据偏好选择排序顺序,我有什么遗漏吗
<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>
最高利率
最高余额
没有种类
内特
平衡
身份证件
我的猜测是constantsCursor永远不会被初始化,因为如果条件满足,这些都不会被初始化。字符串是对象,所以不能用这种方式将它们与==运算符进行比较(请参阅讨论),所以所有条件的计算结果都是false。使用Object.equals(other_Object)
测试等效性,例如:else if(sortorder.equals(“id”)
还请注意,
Cursor.requery()
已被弃用。最好在光标上调用close(),然后再调用另一个。可能将数据库代码移动到一个私有方法中,该方法返回一个游标。然后从onCreate()和onResume()调用这个私有方法,如果需要的话。Force Close:)因为一个bug而自动退出。太棒了。我来试试这个。我觉得它不符合任何条件,但总是假设==与“equals”相同,再次感谢