如果条件在Java中不起作用

时间:2011-08-19 11:14:10

标签: java android

我已经编写了一个条件if if用于检查字符串FKLoadStatus是否为“C”。但即使字符串FKLoadStatus返回值“C”本身也无法工作......任何人都会检查代码中是否有任何错误。 ..... 我从sqllite数据库

中获取FKLoadStatus的值
 sql=" SELECT trim(FKLoadStatus) as FKLoadStatus , tblLoad.PKLoad,  " +
                    "date(tblLoad.PlannedLoadDate) AS PlannedLoadDate, tblLoad.Carrier," +
                    " tblLoad.FKCarrierDriver, COUNT(tblDelivery.FKLoad) AS NoOfDeliveries ," +
                    " DriverReportTime FROM tblLoad LEFT OUTER JOIN tblDelivery" +
                    " ON tblLoad.PKLoad = tblDelivery.FKLoad WHERE PKLoad = ? " +
                    " GROUP BY FKLoadStatus , tblLoad.PKLoad, tblLoad.PlannedLoadDate, " +
                    " tblLoad.Carrier, tblLoad.FKCarrierDriver , DriverReportTime";

            dbAdapter = new DatabaseAdapter(this);
            dbAdapter.open();

            cursor=dbAdapter.ExecuteRawQuery(sql,strpkManifest);        

            if (cursor.moveToFirst())
            ManifestNo.setText(cursor.getString(cursor
                        .getColumnIndex("PKLoad")));
            ManifestDate.setText(cursor.getString(cursor
                    .getColumnIndex("PlannedLoadDate")));
            Carrier.setText(cursor.getString(cursor.getColumnIndex("Carrier")));
            CarrierDriver.setText(cursor.getString(cursor.getColumnIndex("FKCarrierDriver")));
            DepotReportTime.setText(cursor.getString(cursor.getColumnIndex("DriverReportTime")));
            NoofDeliveries.setText(cursor.getString(cursor.getColumnIndex("NoOfDeliveries")));
            FKLoadStatus=cursor.getString(cursor.getColumnIndex("FKLoadStatus"));


            if FKLoadStatus!="C" )
            {
                confirm_Depot_button.setEnabled(true);

            }
            else
            {
                confirm_Depot_button.setEnabled(false);


}

3 个答案:

答案 0 :(得分:7)

您需要使用.equals()方法比较字符串,而不是直接比较对象引用。

答案 1 :(得分:1)

如果比较字符串,则应使用.equals()方法:

if (!"C".equals(FkLoadStatus)) {
    confirm_Depot_button.setEnabled(true);    
}
else {
    confirm_Depot_button.setEnabled(false);
}

或者甚至更好,你可能只是:

confirm_Depot_button.setEnabled(!"C".equals(FkLoadStatus));

答案 2 :(得分:1)

使用==!=运算符无法完成Java中的String变量比较。应该使用方法.equals()

完成