简体   繁体   中英

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.

I want to make that toast custom, but when the custom arraylist is == null, it should spit out a standard toast. This works fine when I DONT check for the list == 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.

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;

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

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())

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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