在 Android Activity 内绑定 HashMap

Bind HashMap inside Android Activity

我正在尝试使用 android 数据绑定库。为了开始使用它,我尝试使用 HashMap 但出现了一些错误。这是我的代码:

我的活动:

public class MainActivity extends AppCompatActivity {

    ActivityMainBinding activityMainBinding;
    HashMap<String, String> myMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        myMap = new HashMap<>();
        myMap.put("one", "ONE");
        myMap.put("two", "TWO");
        myMap.put("three", "THREE");
        activityMainBinding.setMap(myMap);
    }
}

activity_main.xml布局文件:

 <?xml version="1.0" encoding="utf-8"?>
    <layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">

        <data>
            <variable
                name="map"
                type="java.util.HashMap" />

             <variable
                  name="one"
                  type="String" />

        </data>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical"
            tools:context=".MainActivity">

            <TextView
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="@{map[one]}" />

        </LinearLayout>
    </layout>

错误:

Found data binding errors. ****/ data binding error ****msg:Cannot find the setter for attribute 'android:text' with parameter type V on android.widget.TextView.

我想你担心的是 HashMap 是一个通用的 class。您不能为 xml 中的数据绑定分配任何泛型类型。所以,你需要做的是为你的 HashMap 添加一些没有通用的包装器。像那样:

import java.util.HashMap;

public class MapContainer {

    public String get(String key) {
        return getMap().get(key);
    }

    private HashMap<String, String> getMap() {
        HashMap<String, String> myMap = new HashMap<>();
        myMap.put("one", "ONE");
        myMap.put("two", "TWO");
        myMap.put("three", "THREE");
        return myMap;
    }
}

之后你可以在你的数据绑定中定义一个函数 get class:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="map"
            type="changeThisVariableToYourProjectPackage.MapContainer" />

        <variable
            name="one"
            type="String" />

    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{map.get(one)}" />

    </LinearLayout>
</layout>

基于@samaromku 的回答只需更改:

android:text="@{map.get(`one`)}   

android:text='@{map.get("one")}'