2017-06-08 2 views
0

バインディングは、私は変数データは、私が結合し、以下のアンドロイドのデータを使用していますAndroidのリサイクラービュー

public class DayTemp extends BaseObservable implements Serializable { 

    @SerializedName("dt") 
    long date; 
    @SerializedName("pressure") 
    double pressure; 
    @SerializedName("humidity") 
    long humidity; 
    @SerializedName("temp") 
    Temp temp; 
    @SerializedName("weather") 
    ArrayList<Weather> weathers; 
    @SerializedName("speed") 
    double speed; 
    @SerializedName("deg") 
    double deg; 
    @SerializedName("clouds") 
    double clouds; 

    @Bindable 
    public long getDate() { 
     return date; 
    } 

    public void setDate(long date) { 
     this.date = date; 
     notifyPropertyChanged(BR.date); 
    } 

    @Bindable 
    public double getPressure() { 
     return pressure; 
    } 

    public void setPressure(double pressure) { 
     this.pressure = pressure; 
     notifyPropertyChanged(BR.pressure); 
    } 

    @Bindable 
    public long getHumidity() { 
     return humidity; 
    } 

    public void setHumidity(long humidity) { 
     this.humidity = humidity; 
     notifyPropertyChanged(BR.humidity); 
    } 

    @Bindable 
    public Temp getTemp() { 
     return temp; 
    } 

    public void setTemp(Temp temp) { 
     this.temp = temp; 
     notifyPropertyChanged(BR.temp); 
    } 

    @Bindable 
    public ArrayList<Weather> getWeathers() { 
     return weathers; 
    } 

    public void setWeathers(ArrayList<Weather> weathers) { 
     this.weathers = weathers; 
     notifyPropertyChanged(BR.weathers); 
    } 

    @Bindable 
    public double getSpeed() { 
     return speed; 
    } 

    public void setSpeed(double speed) { 
     this.speed = speed; 
     notifyPropertyChanged(BR.speed); 
    } 

    @Bindable 
    public double getDeg() { 
     return deg; 
    } 

    public void setDeg(double deg) { 
     this.deg = deg; 
     notifyPropertyChanged(BR.deg); 
    } 

    @Bindable 
    public double getClouds() { 
     return clouds; 
    } 

    public void setClouds(double clouds) { 
     this.clouds = clouds; 
     notifyPropertyChanged(BR.clouds); 
    } 
} 

を設定するために使用していたクラスです私は天気のarraylistにアクセスすることはできませんまた、それに関連するフィールドを取得することができません。

public class Weather extends BaseObservable implements Serializable { 

    @SerializedName("id") 
    long id; 
    @SerializedName("main") 
    String main; 
    @SerializedName("description") 
    String desc; 
    @SerializedName("icon") 
    String icon; 

    @Bindable 
    public long getId() { 
     return id; 
    } 

    public void setId(long id) { 
     this.id = id; 
     notifyPropertyChanged(BR.id); 
    } 

    @Bindable 
    public String getMain() { 
     return main; 
    } 

    public void setMain(String main) { 
     this.main = main; 
     notifyPropertyChanged(BR.main); 
    } 

    @Bindable 
    public String getDesc() { 
     return desc; 
    } 

    public void setDesc(String desc) { 
     this.desc = desc; 
     notifyPropertyChanged(BR.desc); 
    } 

    @Bindable 
    public String getIcon() { 
     return icon; 
    } 

    public void setIcon(String icon) { 
     this.icon = icon; 
     notifyPropertyChanged(BR.icon); 
    } 
} 

レイアウトファイル:

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

    <data> 

     <import type="com.weatherappforleftshift.currentlocation.DateUtils"/> 

     <variable 
      name="daytemp" type="com.weatherappforleftshift.currentlocation.model.DayTemp"/> 


    </data> 

      <TextView 
        android:id="@+id/weather_status" 
        android:layout_width="wrap_content" 
        android:layout_marginLeft="10dp" 
        android:layout_marginStart="10dp" 
        android:text="@{HOW TO SET TEXT HERE FROM weathers list}" 
        android:layout_height="wrap_content" /> 


</layout> 

私は天気クラスからの私recyclerviewにアイコンと説明を設定します。

アイテムレイアウトでこれを達成する方法

+0

@pskink thnx –

+0

@tynnはデータバインディングのみを要求し、クラスからarraylistにアクセスすることはできません –

+0

_天気予報からここにテキストを設定する方法はありません。少なくとも、 'Weather'のリストを' CharSequence'に変換するコードを少なくとも提供します。ユーティリティメソッドまたは何か... – tynn

答えて

0

あなたは、私が興味

https://github.com/arunshankar87/triptrial/blob/master/app/src/main/java/com/arunshankar/triptrial/MyAdapter.java

のものであろう2つのファイルの下に具体的にrecyclerview

のデータバインディングを扱っている

https://github.com/arunshankar87/triptrial

このgithubのプロジェクトを見ることができます

https://github.com/arunshankar87/triptrial/blob/master/app/src/main/res/layout/row_layout.xml

+0

まずは入力のthnxですが、私はそれを超えてアクセスしたいと思います。今、この@ {daytemp.weathers.get()}をやっていますが、気象に関するデータを取得したい、気象は** arraylistです**。 –

0

あなたのVMに追加します。

@Bindable 
public String getWeathersText() { 
    StringBuilder builder = new StringBuilder(); 
    for (int i = 0; i < weathers.size(); i++) { 
     builder.append(weathers.get(i).getText); 
    } 
    return builder.toString(); 
} 

とあなたのレイアウトXMLで:

<TextView 
     android:id="@+id/weather_status" 
     android:layout_width="wrap_content" 
     android:layout_marginLeft="10dp" 
     android:layout_marginStart="10dp" 
     app:weathersText="@{daytemp.weathersText}" 
     android:layout_height="wrap_content" /> 

私はそれをチェックしませんでしたが、私はそれを助けるべきだと思う...私は「

+0

2つのデータ変数thnxを入力に使って解決しました。 –

関連する問題