当列表为空时,ArrayList if语句不起作用

时间:2015-10-08 14:30:04

标签: java android arraylist bluetooth

我有一个微调器和一个onItemSelected,它将ArrayList与Parse.com进行比较,然后运行一个运行蓝牙扫描的Scan方法,如果它找到一个特定的mac地址反对解析,它将进行吐司。

我想制作吐司自定义,但是当自定义arraylist = = null时,它应该吐出标准吐司。当我不检查列表== null时,这工作正常。如果我为吐司输入一些东西它运行正常并显示自定义吐司。所以toast绝不能是null eventho什么都没有定义。我已经尝试过customnotifications。在添加内容之前清除代码中的几个地方,但它不起作用。

这是我对支票的陈述;

 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: ");
            }
        }
    });
}

在这里我添加了自定义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);
    }

}

我该如何正确检查?一直在这一天:(

1 个答案:

答案 0 :(得分:0)

朋友已经向我指出ArrayLists不能为空,所以显而易见的小变化很简单

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

更改为

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