2017-08-04 62 views
0

我正在Android项目中,我有一个设备列表,我想显示感谢基础适配器。每个设备都有一个亮度级别,我想用搜索栏进行修改。 我试图识别设备寻求与这样的标签栏:发送数据绑定ID与标签不工作

<SeekBar 
        android:id="@+id/seekBar" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:max="100" 
        android:tag="@{Integer.toString(ampoule.idx)}" /> 

这是我安瓿对象:

private String Type ; 
private int LevelInt; 
private String hueColor; 
private String Name; 
private int idx; 

与结合:

<data> 
    <variable 
     name="ampoule" 
     type="com.example.****.***.containers.LightDevice" 
     /> 
</data> 

现在,当我尝试获取我的baseadapter的标签以获取该对象,我得到以下错误:

FATAL EXCEPTION: main 
                      Process: com.example.***.***, PID: 9057 
                      java.lang.NumberFormatException: Invalid int: "binding_4" 

而且这里是我得到的标签:

@Override 
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { 
    setDimmableLevel(seekBar.getTag().toString(),progress); 
} 

private void setDimmableLevel(String idx, final int progress) { 
    Map<String, String> queryParams = new HashMap<>(); 
    Log.d("TAG", "valeur idx : "+idx); 
    final LightDevice light = LightList.getInstance().getLight(Integer.parseInt(idx)); 

的错误是,我的变量值binding_4代替beeing的ampoule.idx值...

的任何想法,我应该怎么做?

谢谢

回答

0

请尝试这个,我认为它的类型铸造相关的问题,希望它的作品。

<SeekBar 
        android:id="@+id/seekBar" 
        android:layout_width="match_parent" 
        android:layout_height="wrap_content" 
        android:layout_weight="1" 
        android:max="100" 
        android:tag="@{String.valueof(ampoule.idx)}" /> 
+0

不,仍然无法正常工作... –

0

我想你忘了绑定步骤。绑定后,您的View标签应该被正确设置。如果您创建了绑定,则可能需要调用binding.executePendingBindings()以使标记值在下一行之前初始化。否则,它会延迟到下一帧,以允许在更新视图之前累积绑定值更改。

一个活动,你应该膨胀的布局是这样的:

@Override 
public void onCreate(...) { 
    super.onCreate(...); 
    MyLayoutBinding binding = DataBindingUtil.setContentView(this, R.layout.my_layout); 
    binding.setAmpoule(new LightDevice()); 
} 

如果从不同的位置夸大它,你可以使用静态inflaters。例如,从片段:

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle icicle) { 
    MyLayoutBinding binding = MyLayoutBinding.inflate(inflater, container, false); 
    binding.setAmpoule(new LightDevice()); 
    return binding.getRoot(); 
}