如何在 android 的 recyclerview 中将最新项目标记为 "new"?
How to marked as "new" to newest items in the recycler view in anroid?
我有项目要通过回收站视图显示在 android 应用中。我已成功实施并从服务器获取数据。项目显示在回收站视图列表中。我还将最新的项目排序在列表的顶部。我在后端完成了使用 php 代码和 SQL 对列表顶部的最新项目进行排序并成功完成的操作。但现在我想在列表中实现这样的文本:- NEW。这意味着它只会显示最新的项目并在 5 天后自动删除。
我已经在前端成功实现了 NEW 标签并且它正在显示,但是新标签只针对当前日期显示。
我想再显示“新”标签 5 天。我提出了逻辑,但它对我没有帮助,它只显示当前日期。这是我显示新代码的代码:-
MyAdapter class --> onBindViewHolder()
Json data:-
"creation_date": "2021-09-06"
final String creation_date = ModelClass.getCreation_date(); //JSON data "creation date". getCreation_date() invoking Getter method from model class"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
String Current_date = sdf.format(c.getTime());
c.add(Calendar.DAY_OF_MONTH, 5); //here I have increase the date to 5
String EndDate = sdf.format(c.getTime());
if (Current_date.equals(creation_date)) {
myViewHolder.creation_date.setVisibility(View.VISIBLE);
}
else if (creation_date.equals(EndDate)) {
myViewHolder.creation_date.setVisibility(View.VISIBLE);
} else {
myViewHolder.creation_date.setVisibility(View.GONE);
}
主要问题是如何在创建日期之后再显示 NEW 标签 5 天以及 5 天后 Textview(creation_date) 可见性将消失?
现在,如果我的创建日期与当前日期相同,那么它会显示新标签,但我想再显示 5 天,5 天后它应该被删除。
现在输出显示如下:-
在创建新项目时,只需将“NEW”关键字的对象放入后端即可。然后在 java 或任何你正在使用的前端检查它。
您将拥有填充数据的适配器的项目布局,因此也将有一个“新”项目的布局,这对于普通项目是不可见的。因此,当您找到关键字“NEW”时,您可以显示显示“NEW”关键字的布局,该关键字之前是不可见的。
这是针对前端的。
现在是您的删除逻辑。
如果您想在 5 天后删除项目,请将该持续时间放在您希望在 5 天后删除的项目中。当用户下次打开您的应用程序时验证项目,如果您提供的时间已经过去,则从数组列表中删除该项目,或者如果您删除了 API.
,则可以从服务器中删除它
如果您有任何困惑,请告诉我,否则就可以了。
这个答案会对你有所帮助。试试吧,如果你遇到任何错误让我知道......
我已经使用了您上面的示例代码。这是我的代码版本:
final String creation_date = storiesModel.getCreation_date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat Cussdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
//new
Calendar cust = Calendar.getInstance();
try {
cust.setTime(Cussdf.parse(creation_date));
} catch (ParseException e) {
e.printStackTrace();
}
cust.add(Calendar.DAY_OF_MONTH, 1);
String customIncreDate = Cussdf.format(cust.getTime());
System.out.println("Your custom Increment creation date is: "+customIncreDate);
//another one
Calendar customDate = Calendar.getInstance();
try {
customDate.setTime(Cussdf.parse(creation_date));
} catch (ParseException e) {
e.printStackTrace();
}
customDate.add(Calendar.DAY_OF_MONTH,2); //for next increment create all "//another one" whole code and increase 2 to 3 for next 3 days
String CustomInDate = Cussdf.format(customDate.getTime());
System.out.println("Your 2nd custom Increment creation date is: "+CustomInDate);
//another one
//new
String Current_date = sdf.format(c.getTime());
c.add(Calendar.DAY_OF_MONTH, 1);//this is increment the current date only
String EndDate = sdf.format(c.getTime());
if (Current_date.equals(creation_date)) {
myStoryViewHolder.creation_date.setVisibility(View.VISIBLE);
}
else if (Current_date.equals(customIncreDate)){ //It is showing!!
myStoryViewHolder.creation_date.setVisibility(View.VISIBLE);
}
else if (Current_date.equals(CustomInDate)) {
myStoryViewHolder.creation_date.setVisibility(View.VISIBLE);
} else {
myStoryViewHolder.creation_date.setVisibility(View.GONE);
}
Calendar c = Calendar.getInstance();
这将有助于创建 Calendar 的实例,然后我们将使用 add()
方法在给定的日期字符串中添加天数。像这样:- c.add(Calendar.DAY_OF_MONTH, 1)
为了增加日历实例中的日期,我从这个站点获得了帮助:- https://beginnersbook.com/2017/10/java-add-days-to-date/
我有项目要通过回收站视图显示在 android 应用中。我已成功实施并从服务器获取数据。项目显示在回收站视图列表中。我还将最新的项目排序在列表的顶部。我在后端完成了使用 php 代码和 SQL 对列表顶部的最新项目进行排序并成功完成的操作。但现在我想在列表中实现这样的文本:- NEW。这意味着它只会显示最新的项目并在 5 天后自动删除。
我已经在前端成功实现了 NEW 标签并且它正在显示,但是新标签只针对当前日期显示。 我想再显示“新”标签 5 天。我提出了逻辑,但它对我没有帮助,它只显示当前日期。这是我显示新代码的代码:-
MyAdapter class --> onBindViewHolder()
Json data:-
"creation_date": "2021-09-06"
final String creation_date = ModelClass.getCreation_date(); //JSON data "creation date". getCreation_date() invoking Getter method from model class"
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
String Current_date = sdf.format(c.getTime());
c.add(Calendar.DAY_OF_MONTH, 5); //here I have increase the date to 5
String EndDate = sdf.format(c.getTime());
if (Current_date.equals(creation_date)) {
myViewHolder.creation_date.setVisibility(View.VISIBLE);
}
else if (creation_date.equals(EndDate)) {
myViewHolder.creation_date.setVisibility(View.VISIBLE);
} else {
myViewHolder.creation_date.setVisibility(View.GONE);
}
主要问题是如何在创建日期之后再显示 NEW 标签 5 天以及 5 天后 Textview(creation_date) 可见性将消失?
现在,如果我的创建日期与当前日期相同,那么它会显示新标签,但我想再显示 5 天,5 天后它应该被删除。
现在输出显示如下:-
在创建新项目时,只需将“NEW”关键字的对象放入后端即可。然后在 java 或任何你正在使用的前端检查它。 您将拥有填充数据的适配器的项目布局,因此也将有一个“新”项目的布局,这对于普通项目是不可见的。因此,当您找到关键字“NEW”时,您可以显示显示“NEW”关键字的布局,该关键字之前是不可见的。
这是针对前端的。
现在是您的删除逻辑。
如果您想在 5 天后删除项目,请将该持续时间放在您希望在 5 天后删除的项目中。当用户下次打开您的应用程序时验证项目,如果您提供的时间已经过去,则从数组列表中删除该项目,或者如果您删除了 API.
,则可以从服务器中删除它如果您有任何困惑,请告诉我,否则就可以了。
这个答案会对你有所帮助。试试吧,如果你遇到任何错误让我知道...... 我已经使用了您上面的示例代码。这是我的代码版本:
final String creation_date = storiesModel.getCreation_date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
SimpleDateFormat Cussdf = new SimpleDateFormat("yyyy-MM-dd");
Calendar c = Calendar.getInstance();
//new
Calendar cust = Calendar.getInstance();
try {
cust.setTime(Cussdf.parse(creation_date));
} catch (ParseException e) {
e.printStackTrace();
}
cust.add(Calendar.DAY_OF_MONTH, 1);
String customIncreDate = Cussdf.format(cust.getTime());
System.out.println("Your custom Increment creation date is: "+customIncreDate);
//another one
Calendar customDate = Calendar.getInstance();
try {
customDate.setTime(Cussdf.parse(creation_date));
} catch (ParseException e) {
e.printStackTrace();
}
customDate.add(Calendar.DAY_OF_MONTH,2); //for next increment create all "//another one" whole code and increase 2 to 3 for next 3 days
String CustomInDate = Cussdf.format(customDate.getTime());
System.out.println("Your 2nd custom Increment creation date is: "+CustomInDate);
//another one
//new
String Current_date = sdf.format(c.getTime());
c.add(Calendar.DAY_OF_MONTH, 1);//this is increment the current date only
String EndDate = sdf.format(c.getTime());
if (Current_date.equals(creation_date)) {
myStoryViewHolder.creation_date.setVisibility(View.VISIBLE);
}
else if (Current_date.equals(customIncreDate)){ //It is showing!!
myStoryViewHolder.creation_date.setVisibility(View.VISIBLE);
}
else if (Current_date.equals(CustomInDate)) {
myStoryViewHolder.creation_date.setVisibility(View.VISIBLE);
} else {
myStoryViewHolder.creation_date.setVisibility(View.GONE);
}
Calendar c = Calendar.getInstance();
这将有助于创建 Calendar 的实例,然后我们将使用 add()
方法在给定的日期字符串中添加天数。像这样:- c.add(Calendar.DAY_OF_MONTH, 1)
为了增加日历实例中的日期,我从这个站点获得了帮助:- https://beginnersbook.com/2017/10/java-add-days-to-date/