我得到了一个recyclerView,它将根据使用数据绑定的 JSON 结果进行填充,我想要做的是检查/比较开始日期和过期日期以查看“交易”是否已过期(如果过期,则文本视图为“交易已过期“ 会出现)
"DealsPageInfo": [
{
"DisplayName": "string",
"StartDate": "2018-04-27T03:06:18.890Z",
"ExpiredDate": "2018-04-27T03:06:18.890Z",
"Url": "string",
"ImageUrl": "string",
"ShortDescription": "string"
}
这是我的一些问题:
*我应该使用 DATE / String 来存储对象吗?
* 我应该在哪里执行此操作?在我的片段/适配器下?
感谢提供任何来源/示例。
<TextView
android:id="@+id/list_offers_startDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="3dp"
android:text="@{offers.StartDate}"
android:textColor="@color/colorGrayText"
android:textSize="@dimen/list_fontsize"
/>
<TextView
android:id="@+id/list_offers_endDate"
android:visibility="gone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:text="@{offers.ExpiredDate}"
android:textColor="@color/colorGrayText"
android:textSize="@dimen/form_fontsize"
/>
* 我应该在哪里执行此操作?在我的片段/适配器下?在适配器的 onBindViewHolder 中执行此操作,
@Override
public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, final int position){
if (isDateExpired(listItems.get(position).getStartDate(),listItems.get(position).getEndDate())){
viewHolder.dealsExpiredtextview.setVisibility(View.GONE);
}else {
viewHolder.dealsExpiredtextview.setVisibility(View.VISIBLE);
}
}
在您的适配器中添加此方法,
public boolean isDateExpired(String startDate, String endDate) {
SimpleDateFormat dfDate = new SimpleDateFormat("dd-MM-yyyy");
boolean b = false;
try {
if (dfDate.parse(startDate).before(dfDate.parse(endDate))) {
return true; // If start date is before end date.
} else if (dfDate.parse(startDate).equals(dfDate.parse(endDate))) {
return false; // If two dates are equal.
} else {
return false; // If start date is after the end date.
}
} catch (ParseException e) {
e.printStackTrace();
return false;
}
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句