1

For my android application, I want to read the temperature of my raspberry pi. From the server, the variable is returned as a double. From the XML, it is returned as a String. Therefore I thought why wouldn't I make a generic setter that can set the value if it is a String or double.

@Bindable
public String getCPUTemperature(){
    return Double.toString(mCPUTemp);
}

/**
 * Sets the CPU temperature for the device view model.
 * @param <T> The generic parameter T accepts a String as wel as a Double to set the available memory.
 */
public <T> void setCPUTemperature(T CPUTemp){
    if(CPUTemp.getClass().equals(String.class))
        this.mCPUTemp = Double.parseDouble((String)CPUTemp);
    else if(CPUTemp.getClass().equals(Double.class))
        this.mCPUTemp = (double)CPUTemp;
    notifyPropertyChanged(BR.cPUTemperature);
}

But after I created this setter I found out that:

Note: Arrays and a generic type, such as the Observable class, might display errors when there are no errors.

So I started to wonder. Is it possible to solve it with a generic variable and if so, is that the best way of solving it?

yennsarah
  • 5,467
  • 2
  • 27
  • 48
user3473161
  • 197
  • 1
  • 4
  • 18

1 Answers1

2

It sounds like the comment is related to Android Studio support for data binding. Android Studio's support for data binding is still improving. It sometimes shows warnings or red underlines when the code compiles and runs just fine.

What you're doing will work. Your current approach doesn't use T at all, so it may as well be Object. I don't think you're getting any real advantage from using generics.

You can do it without generics and I think it looks better:

public void setCPUTemperature(String CPUTemp){
    this.mCPUTemp = Double.parseDouble((String)CPUTemp);
    notifyPropertyChanged(BR.cPUTemperature);
}

public void setCPUTemperature(double CPUTemp){
    this.mCPUTemp = CPUTemp;
    notifyPropertyChanged(BR.cPUTemperature);
}

There are other alternatives. For example, you can use only double and then a quick String conversions in the XML:

public double getCPUTemperature(){
    return this.mCPUTemp;
}

public void setCPUTemperature(double CPUTemp){
    this.mCPUTemp = CPUTemp;
    notifyPropertyChanged(BR.cPUTemperature);
}

and then in the XML:

<TextView android:text='@{"" + model.cPUTemperature}' .../>
<TextView android:text="@{Double.toString(model.cPUTemperature)}" .../>
<TextView android:text="@{@string/tempFormat(model.cPUTemperature)}" .../>

If it were my application, I would choose the @string/tempFormat option so that I can easily change the formatting of the double. I prefer to avoid hard-coded formatting in the code, and XML is a better place.

George Mount
  • 20,708
  • 2
  • 73
  • 61
  • This is the answer I was looking for. I had my doubts about formatting the code in the View instead of the ViewModel. But after seeing this I tend to think it is the better place because it also leads to less code and that should make it easier to maintain. – user3473161 Mar 03 '17 at 07:24