编辑文字不适用于数据绑定

时间:2019-01-22 20:57:20

标签: android data-binding

在我的布局中,我删除了顶部的应用栏,并粘贴了一个编辑文本字段。 然后,我通过数据绑定来绑定该编辑文本,并在视图模型中设置一个ObservableField。但这是行不通的...如果我删除具有布局的android:text字段并从视图中设置文本,则可以在编辑文本中看到字符串,但是如果再次设置绑定,则没有可见的字符串。当然,我的视图模型中的ObservableField为null。这是怎么回事? 有什么例外吗?

编辑: 好的,我解决了我的问题:

FragmentTodayBinding fragmentTodayBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_today, container, false);

问题出在容器中。我将此词更改为null,并且效果很好。

布局:

layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">

<data>
    <variable
        name="mainViewModel"
        type="com.example.daniellachacz.weatherapp2.viewmodel.MainViewModel" >
    </variable>

    <variable
        name="todayFragmentInterface"
        type="com.example.daniellachacz.weatherapp2.view.TodayFragmentInterface">
    </variable>
</data>

<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/blue">

<EditText
    android:layout_width="300dp"
    android:layout_height="40dp"
    android:id="@+id/location_edit"
    android:text="@={mainViewModel.city}"
    android:layout_marginTop="5dp"
    android:textColor="@color/colorWhite"/>


<Button
    android:id="@+id/search_button"
    android:onClick="@{(v)-> todayFragmentInterface.onSearchClick(v)}"
    android:layout_width="40dp"
    android:layout_height="40dp"
    android:layout_alignParentEnd="true"
    android:layout_marginEnd="10dp"
    android:layout_marginTop="5dp"
    android:background="@drawable/ic_search_white_24dp" />

<TextView
    android:id="@+id/condition_text_view"
    android:layout_width="350dp"
    android:layout_height="50dp"
    android:textColor="@color/colorWhite"
    android:textSize="27sp"
    android:layout_marginTop="60dp"
    android:layout_marginStart="15dp"/>

<TextView
    android:id="@+id/temp_text_view"
    android:layout_width="150dp"
    android:layout_height="120dp"
    android:layout_below="@+id/condition_text_view"
    android:layout_alignStart="@+id/condition_text_view"
    android:textColor="@color/colorWhite"
    android:textSize="45sp"
    android:gravity="center" />

<ImageView
    android:id="@+id/icon_image_view"
    android:layout_width="150dp"
    android:layout_height="120dp"
    android:layout_below="@+id/condition_text_view"
    android:layout_alignEnd="@+id/condition_text_view" />

<TextView
    android:id="@+id/wind_text_view"
    android:layout_width="200dp"
    android:layout_height="37dp"
    android:layout_below="@+id/temp_text_view"
    android:layout_alignParentStart="true"
    android:layout_marginStart="15dp"
    android:textColor="@color/colorWhite"
    android:textSize="16sp"
    android:gravity="center"/>

<TextView
    android:id="@+id/humidity_text_view"
    android:layout_width="200dp"
    android:layout_height="37dp"
    android:layout_below="@+id/temp_text_view"
    android:layout_alignParentStart="true"
    android:layout_marginStart="15dp"
    android:layout_marginTop="36dp"
    android:gravity="center"
    android:textColor="@color/colorWhite"
    android:textSize="16sp" />

<TextView
    android:id="@+id/pressure_text_view"
    android:layout_width="200dp"
    android:layout_height="37dp"
    android:layout_below="@+id/temp_text_view"
    android:layout_alignParentStart="true"
    android:layout_marginStart="15dp"
    android:layout_marginTop="72dp"
    android:gravity="center"
    android:textColor="@color/colorWhite"
    android:textSize="16sp" />

Viewmodel:

public class MainViewModel extends AndroidViewModel {

private CurrentWeatherRepository currentWeatherRepository;
public final ObservableField<String> city = new ObservableField<>();


public MainViewModel(@NonNull Application application) {
    super(application);
    currentWeatherRepository = new CurrentWeatherRepository(application);
}

public void getCity() {
    currentWeatherRepository.getCurrentWeather(String.valueOf(city.get()), ApiClient.KEY, ApiClient.UNITS);
}

活动:

public class TodayFragment extends Fragment {


@SuppressLint("SetTextI18n")
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    FragmentTodayBinding fragmentTodayBinding = DataBindingUtil.inflate(inflater,R.layout.fragment_today, container, false);
    View view = fragmentTodayBinding.getRoot();
    final MainViewModel mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);

    fragmentTodayBinding.locationEdit.setText("X");

    fragmentTodayBinding.setTodayFragmentInterface(new TodayFragmentInterface() {
        @Override
        public void onSearchClick(View v) {
            Toast.makeText(getActivity(), "X", Toast.LENGTH_SHORT).show();
            mainViewModel.getCity();
        }
    });

    mainViewModel.getWeather().observe(getViewLifecycleOwner(), new Observer<Weather>() {
        @Override
        public void onChanged(Weather weather) {
            fragmentTodayBinding.windTextView.setText("Wind: " + String.valueOf(weather.getWind().getSpeed()) + " km/h");
            fragmentTodayBinding.humidityTextView.setText("Humidity: " + weather.getMain().getHumidity() + " %");
            fragmentTodayBinding.pressureTextView.setText("Pressure: " + weather.getMain().getPressure() + " mBar");
            Double temp = weather.getMain().getTemp();
            DecimalFormat df = new DecimalFormat("#0.0");
            fragmentTodayBinding.tempTextView.setText(df.format(temp) + " ℃");
            String desc = weather.getDescription();
            String desc2 = Character.toString(desc.charAt(0)).toUpperCase()+desc.substring(1);
            fragmentTodayBinding.conditionTextView.setText(desc2);
            Glide.with(TodayFragment.this).load(weather.getIconUrl()).into(fragmentTodayBinding.iconImageView);
        }
    });

    return view;
}

0 个答案:

没有答案