【问题标题】:getView and Beacon(Estimote) problem in JavaJava中的getView和Beacon(Estimote)问题
【发布时间】:2020-04-17 11:50:00
【问题描述】:

我的想法是从信标中获取标题,然后根据该标题向我发送适当的布局。但突然它不会识别我的询问。像 String == String 一样不行。

这是一个视图的代码,如果你们需要其他东西我会发布或者我们可以创建一个 Skype 会话来教我这个。

@Override
public View getView(int position, View convertView, ViewGroup parent) {

    ProximityContent content = nearbyContent.get(position);
    String beacon = content.getTitle();

    if (beacon == "one") {
        if (convertView == null) {
            LayoutInflater inflater =
                    (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            assert inflater != null;

            convertView = inflater.inflate(R.layout.tester_beacon_i, parent, false);
        }
    } else if (beacon == "two") {
        if (convertView == null) {
            LayoutInflater inflater =
                    (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            assert inflater != null;

            convertView = inflater.inflate(R.layout.tester_beacon_ii, parent, false);
        }
    } else if (beacon == "three") {
        if (convertView == null) {
            LayoutInflater inflater =
                    (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            assert inflater != null;

            convertView = inflater.inflate(R.layout.tester_beacon_iii, parent, false);
        }
    } else {
        if (convertView == null) {
            LayoutInflater inflater =
                    (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            assert inflater != null;

            convertView = inflater.inflate(R.layout.tester_beacon_iv, parent, false);
        }
    }

    return convertView;
}

所以每次我的应用启动时,我只获得了 4 次 tester_beacon_iv 布局,而不是同时获得所有 4 个信标上的所有 4 个布局。

附:我正在使用 Estimote 信标。

【问题讨论】:

    标签: java android arrays android-studio estimote


    【解决方案1】:

    == 不能保证在 Java 中处理字符串。对于您的特殊情况,我建议使用 switch 语句。

    switch (beacon) {
    case "one":
        // ...
        break;
    case "two":
        // ...
        break;
    case "three":
        // ...
        break;
    default:
        // When none of them match ...
        break;
    }
    

    【讨论】:

    • 我这样做了,现在不是 4 次进入 layout_iv,而是再次进入 layout_1 而不是 layout_4 而不是 layout_1 和 layout_4... 与 .equal 相同...
    • @NikolaMilojic 您是否尝试过使用“当我删除 if (convertView == null)”时它起作用了?
    【解决方案2】:

    当您在 Java 中对两个字符串使用 == 时,您实际上并没有比较字符串本身。您将需要改用.equals(String)。这是因为== 实际上比较的是两个对象的引用,而不是它们的值。

    string1.equals(string2) 根据字符串中的实际字符比较两个字符串。

    在您的情况下,前三个信标永远不会匹配,因此您将获得布局 #4。你需要写:

    if (beacon.equals("one")) {
      ...
    }
    

    【讨论】:

    • 我这样做了,现在不是 4 次进入 layout_iv,而是再次进入 layout_1 而不是 layout_4 而不是 layout_1 和 layout_4...
    • 当我删除 if (convertView == null) 并执行 .equals 时它起作用了,非常感谢我的朋友。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多