Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/183.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android逻辑问题_Android_Null_Logic - Fatal编程技术网

Android逻辑问题

Android逻辑问题,android,null,logic,Android,Null,Logic,我目前正在学习Android并编写一个应用程序来帮助我完成工作。我已经使用这个优秀的网站很长一段时间了,经过大量研究,它帮助我了解了大多数概念 我想我会问我的第一个问题,因为我相信它会有一个简单的答案-以下陈述中的逻辑没有按照预期工作: protected void onListItemClick(ListView l, View v, final int pos, final long id){ Cursor cursor = (Cursor) rmDbHelper.fetchIns

我目前正在学习Android并编写一个应用程序来帮助我完成工作。我已经使用这个优秀的网站很长一段时间了,经过大量研究,它帮助我了解了大多数概念

我想我会问我的第一个问题,因为我相信它会有一个简单的答案-以下陈述中的逻辑没有按照预期工作:

protected void onListItemClick(ListView l, View v, final int pos, final long id){

    Cursor cursor = (Cursor) rmDbHelper.fetchInspection(id);
    String inspectionRef = cursor.getString(cursor.getColumnIndex(
            RMDbAdapter.INSPECTION_REF));
    String companyName = cursor.getString(cursor.getColumnIndex(
            RMDbAdapter.INSPECTION_COMPANY));

    if (inspectionRef == null && companyName == null){
        inspectionDialogueText = "(Inspection Reference unknown, Company Name unknown)";    
    }
    else if (inspectionRef != null && companyName == null) {
        inspectionDialogueText = "(" + inspectionRef + ", Company Name unknown)";
        }
    else if (inspectionRef == null && companyName != null) {
        inspectionDialogueText = "(Inspection Reference unknown, " + companyName + ")";
    }
    else {
        inspectionDialogueText = "(" + inspectionRef + ", " + companyName + ")";
    }
我不确定是否应该在if语句中使用null或“”,但无论哪种方式,它都不起作用,因为它只打印inspectionRef和companyName,而不管它们是否包含任何内容

对不起,我只是个傻瓜

非常感谢,

David

Android有一个很好的方法来检查空(
“”
)和
null
字符串

TextUtils.isEmpty(str)
它只是
(str==null | | str.length()==0)
,但它为您节省了一点代码

如果要筛选出只包含空格的字符串(
),可以添加
trim()

如果您使用的是Java 1.6,则可以将
str.length()==0
替换为
str.isEmpty()

例如,您的代码可以替换为

if (TextUtils.isEmpty(inspectionRef)){
    inspectionRef = "Inspection Reference unknown";
}
if (TextUtils.isEmpty(companyName)){
    companyName = "Company Name unknown";
}
// here both strings have either a real value or the "does not exist"-text
String inspectionDialogueText = "(" + inspectionRef + ", " + companyName + ")";

如果你在你的代码中使用这段逻辑,你可以把它放在一些实用的方法中

/** returns maybeEmpty if not empty, fallback otherwise */
private static String notEmpty(String maybeEmpty, String fallback) {
    return TextUtils.isEmpty(maybeEmpty) ? fallback : maybeEmpty;
}
像这样使用它

String inspectionRef = notEmpty(cursor.getString(cursor.getColumnIndex(
        RMDbAdapter.INSPECTION_REF)), "Inspection Reference unknown");
String companyName = notEmpty(cursor.getString(cursor.getColumnIndex(
        RMDbAdapter.INSPECTION_COMPANY)), "Company Name unknown");

inspectionDialogueText = "(" + inspectionRef + ", " + companyName + ")";
Android有一个很好的方法来检查空(
“”
)和
null
字符串

TextUtils.isEmpty(str)
它只是
(str==null | | str.length()==0)
,但它为您节省了一点代码

如果要筛选出只包含空格的字符串(
),可以添加
trim()

如果您使用的是Java 1.6,则可以将
str.length()==0
替换为
str.isEmpty()

例如,您的代码可以替换为

if (TextUtils.isEmpty(inspectionRef)){
    inspectionRef = "Inspection Reference unknown";
}
if (TextUtils.isEmpty(companyName)){
    companyName = "Company Name unknown";
}
// here both strings have either a real value or the "does not exist"-text
String inspectionDialogueText = "(" + inspectionRef + ", " + companyName + ")";

如果你在你的代码中使用这段逻辑,你可以把它放在一些实用的方法中

/** returns maybeEmpty if not empty, fallback otherwise */
private static String notEmpty(String maybeEmpty, String fallback) {
    return TextUtils.isEmpty(maybeEmpty) ? fallback : maybeEmpty;
}
像这样使用它

String inspectionRef = notEmpty(cursor.getString(cursor.getColumnIndex(
        RMDbAdapter.INSPECTION_REF)), "Inspection Reference unknown");
String companyName = notEmpty(cursor.getString(cursor.getColumnIndex(
        RMDbAdapter.INSPECTION_COMPANY)), "Company Name unknown");

inspectionDialogueText = "(" + inspectionRef + ", " + companyName + ")";

您还应该检查isEmpty after==null check您还应该检查isEmpty after==null checkHi Zapl,感谢您的详细回复。今晚我要试一试。会给你的anwser加上一个勾号,但我还没有代表!干杯,戴夫。嗨,扎普,谢谢你的详细回复。今晚我要试一试。会给你的anwser加上一个勾号,但我还没有代表!干杯,戴夫。