gpt4 book ai didi

android - 使用 android-databinding 方法格式化友好的日期

转载 作者:行者123 更新时间:2023-11-29 15:35:02 34 4
gpt4 key购买 nike

我想将我的日期 YYYY/MM/DD 格式化为更友好的模式。

我使用 android-databinding。

我期望的输出应该是示例:2006 年 8 月 22 日,星期二。我当前来自 Json 的输入是“2018-09-27”(模型中的字符串数据)

我的代码:

public class DateUtils {

SimpleDateFormat fromServer = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat myFormat = new SimpleDateFormat("dddd, dd MMMM yyyy");

public String getDateToFromat (String reciveDate) {
String newFormatString = myFormat.format(fromServer.parse(reciveDate));
return newFormatString;
};

}

我的布局:

<layout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<data class ="CurrencyBindingDetailItem">
<import type="com.example.htw.currencyconverter.utils.DateUtils"/>
<import type="android.view.View" />
<variable name="currencyItemDetailDate" type="com.example.htw.currencyconverter.model.CurrencyDate"/>
<variable name="currencyBindingItemDetail" type="com.example.htw.currencyconverter.model.CurrencyBinding"/>
<variable name="callback" type="com.example.htw.currencyconverter.callback.ClickCallback"/>
</data>
<TextView
android:textSize="28dp"
android:text="@{DateUtils.getDateToFromat(currencyItemDetailDate.date)}"
android:textColor="@color/primary_text"
android:id="@+id/date_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />

我确实有错误:

Found data binding errors.
****/ data binding error ****msg:**cannot find method getDateToFromat**(java.lang.String) in class com.example.htw.currencyconverter.utils.DateUtils

我做了清理并重新启动和重建。

最佳答案

你为什么不创建一个数据绑定(bind)适配器,让你的 xml 保持更清晰?由于您来自服务器的日期是字符串格式,因此适配器将如下所示:

@BindingAdapter("bindServerDate")
public static void bindServerDate(@NonNull TextView textView, String date) {
/*Parse string data and set it in another format for your textView*/
}

它的用法:

在你的 viewModel 中创建 ObservableField<String> serverDate并根据您的响应设置值,在 xml set app:bindServerDate="@{viewModel.serverDate}" 中.不要忘记添加 viewModel作为variable并从你的 activity/fragment 中设置它

关于android - 使用 android-databinding 方法格式化友好的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52535708/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com