Android SQlite查询按错误列排序
以下是我的表格设置:Android SQlite查询按错误列排序,android,sqlite,Android,Sqlite,以下是我的表格设置: public static final String TABLE_DEBT = "debt"; public static final String COLUMN_ID = "_id"; public static final String COLUMN_DEBT_NAME = "debt_name"; public static final String COLUMN_DEBT_TOTAL = "debt_total"; public static final Strin
public static final String TABLE_DEBT = "debt";
public static final String COLUMN_ID = "_id";
public static final String COLUMN_DEBT_NAME = "debt_name";
public static final String COLUMN_DEBT_TOTAL = "debt_total";
public static final String COLUMN_APR = "apr";
public static final String COLUMN_PAYMENT = "payment";
public static final String COLUMN_SPECIAL_RATE = "s_rate";
public static final String COLUMN_SPECIAL_TIME = "s_time";
public static final String COLUMN_FIRST_FEE = "first_apr_fee";
public static final String COLUMN_PAY_DATE = "pay_date";
private static final String DATABASE_NAME = "debt.db";
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_CREATE = "create table " + TABLE_DEBT + "( " + COLUMN_ID + " integer primary key autoincrement, " + COLUMN_DEBT_NAME + " text not null, " + COLUMN_DEBT_TOTAL
+ " text not null, " + COLUMN_APR + " text not null, " + COLUMN_PAYMENT + " text not null, " + COLUMN_SPECIAL_RATE + " text, " + COLUMN_SPECIAL_TIME + " text, " + COLUMN_FIRST_FEE + " text, " + COLUMN_PAY_DATE + " text)";
我使用此条件格式中的这些查询按用户选择进行排序:
if (DebtPlanner.PlanType.equals("highint")) {
c = database.rawQuery("SELECT * FROM debt ORDER BY apr DESC;", null);
Log.d("TAG", "DebtPlanner.PlanType is " + DebtPlanner.PlanType);
} else if (DebtPlanner.PlanType.equals("lowbal")) {
c = database.rawQuery("SELECT * FROM debt ORDER BY debt_total ASC;", null);
Log.d("TAG", "DebtPlanner.PlanType is " + DebtPlanner.PlanType);
} else if (DebtPlanner.PlanType.equals("highbal")) {
c = database.rawQuery("SELECT * FROM debt ORDER BY debt_total DESC;", null);
Log.d("TAG", "DebtPlanner.PlanType is " + DebtPlanner.PlanType);
}
添加代码:
for (c.moveToFirst(); !c.isAfterLast(); c.moveToNext()) {
indName[j++] = c.getString(c.getColumnIndex("debt_name"));
indBal[k++] = c.getDouble(c.getColumnIndex("debt_total"));
indPay[l++] = c.getDouble(c.getColumnIndex("payment"));
indApr[m++] = c.getDouble(c.getColumnIndex("apr"));
indSRate[e++] = c.getString(c.getColumnIndex("s_rate"));
indSTime[f++] = c.getInt(c.getColumnIndex("s_time"));
indDay[d++] = c.getString(c.getColumnIndex("pay_date"));
rowCounter[n++] = n;
}
for (int i : rowCounter) {
nameList.add(indName[r++]); // This is the name of each debt that shows out of order.
// lots of edited calucaltion here
}
但是,它们会排序,但不会按预期值排序。例如,当我说按debt\u toal
(ASC或DESC)排序时,它按debt\u name
排序(它得到正确的ASC或DESC部分)
我查看了手机中的实际表格,所有数据都在正确的列中。我甚至不知道从这里到哪里去
额外代码:
public class DebtPlanner extends Application {
public static String PlanType = "highint";
public static String highIntPlan() {
return DebtPlanner.PlanType = "highint";
}
public static String lowBalPlan() {
return DebtPlanner.PlanType = "lowbal";
}
public static String highBalPlan() {
return DebtPlanner.PlanType = "highbal";
}
}在Java中比较字符串时,必须使用
equals()
,而不是=
:
if (DebtPlanner.PlanType.equals("default")) {
关于这一点,有很多很好的解释
此外,您还应该在后续查询中使用
else if
,因为它们是相互排斥的想法
它一直在正确地排序。问题是,列是字符串。因此,从技术上讲,2大于1000000。现在我只需要想办法纠正这个 确定,用于将列排序为整数而不是文本:
SELECT * FROM debt ORDER BY CAST(apr AS INTEGER) DESC;
使用
为了确保您的多个
if
s中有正确的数据,您完全正确,我不敢相信我漏掉了。但是,问题仍然存在,所以请不要关闭线程。好的,我记下了评论,但这可能不会阻止其他人。。。无论如何,用上面的两个更改更新你的代码,然后用结果更新你的问题。太好了,你如何在列表视图中使用光标?发布这段代码、XML(如果您使用的是自定义布局)和屏幕截图就好了。我想这可能就是问题所在。这实际上并没有输出到ListView,而是输出到TableLayout(都是通过编程实现的,没有XML)。(我将在将来更新它,因为一个表不是很有效——而且已经过时了?)有太多的代码要为此输出。我会看看我能找到什么,然后给你回复。你是对的,表格不是显示大量数据的最佳方式。列表视图是最好的选择,因为它们只显示屏幕上可以容纳的内容,并缓存其余内容。感谢您的提示。我这样做了,根据日志,我在if
中得到了正确的结果。尝试将代码结构更改为short:将highint替换为apr和其他。然后尝试:c=database.rawQuery(“通过“+DebtPlanner.PlanType+”DESC;”,null从债务订单中选择*)代码>我喜欢简化代码的方法。但是,我不知道为什么,我收到了好几张窗户漏水的日志。
Log.d("TAG", "DebtPlanner.PlanType is " + DebtPlanner.PlanType);