1

这是我的表设置:

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_toal(ASC 或 DESC)排序时,它按debt_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";
    }

}

4

2 回答 2

1

在 Java 中比较 String 时,您必须使用equals(),而不是==

if (DebtPlanner.PlanType.equals("default")) {

How do I compare strings in Java?中有很好的解释。


此外,您应该else if在后续查询中使用它们,因为它们是相互排斥的想法。


它一直在正确排序。问题是,该列是字符串。所以,从技术上讲,2大于1000000。现在我只需要弄清楚如何纠正这个......

好的,使用CAST将列排序为整数而不是文本:

SELECT * FROM debt ORDER BY CAST(apr AS INTEGER) DESC;
于 2012-12-02T18:24:50.170 回答
1

采用

Log.d("TAG", "DebtPlanner.PlanType is " + DebtPlanner.PlanType);

确保您在多个ifs中拥有正确的数据

于 2012-12-02T18:26:54.013 回答