简体   繁体   English

ArrayList如果列表为null时if语句不起作用

[英]ArrayList if statement does not work when the list is null

I have a spinner and an onItemSelected that compares a ArrayList to Parse.com, and then runs a Scan method that runs a bluetooth scan and if it finds a certain mac adress against parse it will make a toast. 我有一个微调器和一个将ArrayList与Parse.com进行比较的onItemSelected,然后运行一个Scan方法,该方法运行一个蓝牙扫描,如果它找到了一个针对解析的mac地址,它将进行烘烤。

I want to make that toast custom, but when the custom arraylist is == null, it should spit out a standard toast. 我想使该吐司自定义,但是当自定义arraylist == null时,它应该吐出标准吐司。 This works fine when I DONT check for the list == null. 当我不检查列表== null时,这可以正常工作。 If I enter something for the toast it runs fine and displays the custom toast. 如果我为烤面包输入内容,它将运行良好并显示自定义烤面包。 So the toast must never be null eventho nothing is defined. 因此,敬酒决不能为空,否则什么也没有定义。 I have tried customnotifications.clear several places in the code before something is added, but it does not work. 我尝试过customnotifications.clear,然后再添加一些内容,但在代码中清除了几个地方,但这是行不通的。

This is my statement for the check; 这是我的支票对帐单;

 public void ScanBL() {
    final ParseQuery<ParseUser> queryParseUser = ParseUser.getQuery();
    queryParseUser.findInBackground(new FindCallback<ParseUser>() {
        @Override
        public void done(List<ParseUser> BTList, ParseException arg1) {
            // TODO Auto-generated method stub
            //Log.d("Parse", "we made it this far");
            btadapter.cancelDiscovery();
            btadapter.startDiscovery();
            if (BTList != null && arg1 == null && list != null) {
                for (ParseUser parseObject : BTList) {
                    if (parseObject.getString("BT_ID") != null) {
                        for (String string : list) {
                            for(String s : customnotification) {
                                Log.d("Custom", s);
                            }
                            if (string.equals(parseObject.getString("BT_ID")) && excluded.contains(parseObject.getString("BT_ID")) && customnotification == null) {
                                String user = parseObject.getString("username");
                                Log.d("For each", user);
                                Toast toast2 = Toast.makeText(context, "Wow, " + user + " is nearby! Test", Toast.LENGTH_SHORT);
                                toast2.show();

                                }
                            else if (string.equals(parseObject.getString("BT_ID")) && excluded.contains(parseObject.getString("BT_ID"))){
                                String user = parseObject.getString("username");
                                for (String string2 : customnotification) {
                                    Toast toast = Toast.makeText(context, user + string2, Toast.LENGTH_SHORT);
                                    toast.show();
                                }
                                //out.append("\nFound nearby device: " + user);
                                //Log.d("Bluetooth", "burde parse?");
                            }
                        }
                    }
                }
            }

            else {
                Log.d("Bluetooth", "Fejl i returnering af data: ");
            }
        }
    });
}

And here I add the customnotifications arraylist; 在这里,我添加了customnotifications arraylist;

protected void changeNotifications() {
    customnotification.clear();

    String changemessage = changeMessage.getText().toString();
    Toast.makeText(getApplicationContext(), "Notifikation changed to " + changemessage, Toast.LENGTH_LONG).show();
    customnotification.add(changemessage);
    for (String string : customnotification){
        System.out.println(string);
    }

}

How on earth do I make it check correctly? 我到底该如何正确检查? Been at this for a day :( 在这里住了一天:(

It's been pointed out to me by a friend that ArrayLists cannot be null, so the obvious little change was simple 一位朋友向我指出ArrayLists不能为null,因此显而易见的微小变化很简单

if (string.equals(parseObject.getString("BT_ID")) && excluded.contains(parseObject.getString("BT_ID")) && customnotification == null)

changed to 变成

if (string.equals(parseObject.getString("BT_ID")) && excluded.contains(parseObject.getString("BT_ID")) && customnotification.isEmpty())

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM