创建卡片时如何在 recyclerview (cardview) 中添加日期。 (卡片创建日期)
Posted
技术标签:
【中文标题】创建卡片时如何在 recyclerview (cardview) 中添加日期。 (卡片创建日期)【英文标题】:How to add date in recyclerview (cardview) when the card is created. (Card creation date) 【发布时间】:2020-12-21 10:33:16 【问题描述】:我正在开发一个便笺应用程序,用户在其中创建便笺,保存便笺后,便笺将保存并显示在回收站视图中。我正在寻找显示创建笔记当天日期的东西。我已经在适配器类中实例化了日历并设置了日期。问题是所有卡片都有今天的日期,而与创建日期无关。如下图所示 Screenshot of emulator
下面是我的适配器类
package com.cksapp.mvvmarchitecture;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.PopupMenu;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import java.text.DateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
public class NoteAdapter extends RecyclerView.Adapter<NoteAdapter.NoteHolder>
private static List<Note> notes = new ArrayList<>();
private onItemClicklistener listener;
private onItemClicklistener1 listener1;
private onItemClicklistener2 listener2;
private onItemClicklistener3 listener3;
private NoteViewModel noteViewModel;
@NonNull
@Override
public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View itemView = LayoutInflater.from(parent.getContext()).inflate(R.layout.note_item,parent,false);
return new NoteHolder(itemView);
@Override
public void onBindViewHolder(@NonNull NoteHolder holder, final int position)
final Note currentNote = notes.get(position);
holder.textViewTitle.setText(currentNote.getTitle());
holder.textViewDescription.setText(currentNote.getDescription());
Calendar c = Calendar.getInstance();**//calender instantiation**
String currentDate = DateFormat.getDateInstance().format(c.getTime());
holder.textViewdate.setText(currentDate);
private void deleteItem(int position)
noteViewModel.delete(notes.get(position));
@Override
public int getItemCount()
return notes.size();
public void setNotes(List<Note> notes)
this.notes = notes;
notifyDataSetChanged();
public Note getNoteAt(int position)
return notes.get(position);
class NoteHolder extends RecyclerView.ViewHolder
private TextView textViewTitle, textViewdate;
private TextView textViewDescription;
private ImageView menupopup;
public NoteHolder(@NonNull View itemView)
super(itemView);
textViewTitle = itemView.findViewById(R.id.text_view_title);
textViewDescription = itemView.findViewById(R.id.text_view_description);
textViewdate = itemView.findViewById(R.id.datetext);
menupopup = itemView.findViewById(R.id.options);
menupopup.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
PopupMenu popupMenu = new PopupMenu(v.getContext(), v);
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.show();
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener()
@Override
public boolean onMenuItemClick(MenuItem item)
final int position1 = getAdapterPosition();
switch (item.getItemId())
case R.id.opennotemenu:
if(position1 != RecyclerView.NO_POSITION && listener1 != null)
listener2.onItemclick2(notes.get(position1));
break;
case R.id.editmenu:
if(position1 != RecyclerView.NO_POSITION && listener1 != null)
listener1.onItemclick1(notes.get(position1));
break;
case R.id.deletemenu:
if(position1 != RecyclerView.NO_POSITION && listener != null)
listener.onItemclick(notes.get(position1));
break;
case R.id.sharenote:
if(position1 != RecyclerView.NO_POSITION && listener3 != null)
listener3.onItemclick3(notes.get(position1));
return true;
);
);
public interface onItemClicklistener
void onItemclick(Note note);
public void setonItemClicklistener(onItemClicklistener listener)
this.listener = listener;
public interface onItemClicklistener1
void onItemclick1(Note note);
public void setonItemClicklistener1(onItemClicklistener1 listener1)
this.listener1 = listener1;
public interface onItemClicklistener2
void onItemclick2(Note note);
public void setonItemClicklistener2(onItemClicklistener2 listener2)
this.listener2 = listener2;
public interface onItemClicklistener3
void onItemclick3(Note note);
public void setonItemClicklistener3(onItemClicklistener3 listener3)
this.listener3 = listener3;
**My add note activity**
package com.cksapp.mvvmarchitecture;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.NumberPicker;
import android.widget.Toast;
import java.text.DateFormat;
import java.util.Calendar;
import java.util.Date;
public class NewActivityNote extends AppCompatActivity
EditText title, description;
public static final String EXTRA_TITLE = "com.cksapp.mvvmarchitecture.EXTRA_TITLE";
public static final String EXTRA_ID = "com.cksapp.mvvmarchitecture.EXTRA_ID";
public static final String EXTRA_DESCRIPTION = "com.cksapp.mvvmarchitecture.EXTRA_DESCRIPTION";
String currentDate;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_note);
title = findViewById(R.id.title1);
description = findViewById(R.id.description1);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close);
Intent intent = getIntent();
if(intent.hasExtra(EXTRA_ID))
setTitle("Edit Note");
title.setText(intent.getStringExtra(EXTRA_TITLE));
description.setText(intent.getStringExtra(EXTRA_DESCRIPTION));
setTitle("Add Note");
@Override
public boolean onCreateOptionsMenu(Menu menu)
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.new_note, menu);
return true;
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item)
switch (item.getItemId())
case R.id.save:
savenote();
return true;
default:
return super.onOptionsItemSelected(item);
private void savenote()
String title1 = title.getText().toString();
String description1 = description.getText().toString();
if(title1.trim().isEmpty() || description1.trim().isEmpty())
Toast.makeText(NewActivityNote.this, "Enter title and description",Toast.LENGTH_SHORT).show();
return;
Intent data = new Intent();
data.putExtra(EXTRA_TITLE, title1);
data.putExtra(EXTRA_DESCRIPTION, description1);
int id = getIntent().getIntExtra(EXTRA_ID, -1);
if(id != -1)
data.putExtra(EXTRA_ID, id);
setResult(RESULT_OK, data);
finish();
public void doSave(View view)
savenote();
正如你所建议的,我在项目中的更改是,这是我添加的转换器类
import androidx.room.TypeConverter;
import java.sql.Date;
public class Converters
@TypeConverter
public static Date fromTimestamp(Long value)
return value == null ? null : new Date(value);
@TypeConverter
public static Long dateToTimestamp(Date date)
return date == null ? null : date.getTime();
我在实体类中添加了日期对象
import androidx.room.Entity;
import androidx.room.PrimaryKey;
import java.sql.Date;
@Entity(tableName = "note_table")
public class Note
@PrimaryKey(autoGenerate = true)
private int id;
private String title;
private String description;
private Date date;
public Note(String title, String description)
this.title = title;
this.description = description;
public void setDate(Date date)
this.date = date;
public Date getDate()
return date;
public void setId(int id)
this.id = id;
public int getId()
return id;
public String getTitle()
return title;
public String getDescription()
return description;
下面是我要记录日期的NewNoteActivity保存笔记方法
private void savenote()
String title1 = title.getText().toString();
String description1 = description.getText().toString();
Date date = Calendar.getInstance().getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
currentDate = sdf.format(date);
接下来我尝试将 currentDate 字符串访问到我的适配器类中以设置日期
@Override
public void onBindViewHolder(@NonNull NoteHolder holder, final int position)
final Note currentNote = notes.get(position);
holder.textViewTitle.setText(currentNote.getTitle());
holder.textViewDescription.setText(currentNote.getDescription());
NewActivityNote n = new NewActivityNote();
String date = n.currentDate;
holder.textViewdate.setText(date);
最后它给了我 null 代替日期文本视图。文本视图最终设置为空(空)
执行上述任务后我的模拟器的屏幕截图 result
下面是我完整的NewNoteActivity
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.util.Log;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.NumberPicker;
import android.widget.TextView;
import android.widget.Toast;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.Locale;
public class NewActivityNote extends AppCompatActivity
EditText title, description;
public static final String EXTRA_TITLE = "com.cksapp.mvvmarchitecture.EXTRA_TITLE";
public static final String EXTRA_ID = "com.cksapp.mvvmarchitecture.EXTRA_ID";
public static final String EXTRA_DESCRIPTION = "com.cksapp.mvvmarchitecture.EXTRA_DESCRIPTION";
String currentDate;
@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_note);
title = findViewById(R.id.title1);
description = findViewById(R.id.description1);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close);
Intent intent = getIntent();
if(intent.hasExtra(EXTRA_ID))
setTitle("Edit Note");
title.setText(intent.getStringExtra(EXTRA_TITLE));
description.setText(intent.getStringExtra(EXTRA_DESCRIPTION));
setTitle("Add Note");
@Override
public boolean onCreateOptionsMenu(Menu menu)
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.new_note, menu);
return true;
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item)
switch (item.getItemId())
case R.id.save:
savenote();
return true;
default:
return super.onOptionsItemSelected(item);
private void savenote()
String title1 = title.getText().toString();
String description1 = description.getText().toString();
Date date = Calendar.getInstance().getTime();
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss");
currentDate = sdf.format(date);
if(title1.trim().isEmpty() || description1.trim().isEmpty())
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_custom, (ViewGroup) findViewById(R.id.toastlayout));
TextView t1 = layout.findViewById(R.id.toasttext);
ImageView i1 = layout.findViewById(R.id.toastimage);
t1.setText("Enter title and description");
i1.setImageResource(R.drawable.ic_error);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
return;
Intent data = new Intent();
data.putExtra(EXTRA_TITLE, title1);
data.putExtra(EXTRA_DESCRIPTION, description1);
int id = getIntent().getIntExtra(EXTRA_ID, -1);
if(id != -1)
data.putExtra(EXTRA_ID, id);
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.toast_custom, (ViewGroup) findViewById(R.id.toastlayout));
TextView t1 = layout.findViewById(R.id.toasttext);
ImageView i1 = layout.findViewById(R.id.toastimage);
t1.setText("Enjoy, Note saved to your collections!");
i1.setImageResource(R.drawable.ic_check);
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
setResult(RESULT_OK, data);
finish();
public void doSave(View view)
savenote();
我的道课
package com.cksapp.mvvmarchitecture;
import androidx.lifecycle.LiveData;
import androidx.room.Dao;
import androidx.room.Delete;
import androidx.room.Insert;
import androidx.room.Query;
import androidx.room.Update;
import java.util.List;
@Dao
public interface NoteDao
@Insert
void insert(Note note);
@Update
void update(Note note);
@Delete
void delete(Note note);
@Query("DELETE FROM note_table")
void deleteAllNotes();
@Query("Select * from note_table order by id desc")
LiveData<List<Note>> getAllNotes();
【问题讨论】:
使用您当前的实现,它们都将显示相同的时间,因为您正在制作日历的实例,因为它们是绑定的,您没有从实际个人@987654330 那里获得时间@对象 我试图通过实例化日历类从 NewActivityNote 中提取时间,但我得到的是空值,而不是所需的日期。谢谢 【参考方案1】:如前所述,您需要将日期保存在便笺实体中。
如果您对获得时间的推荐方式感兴趣,请阅读this answer。
然后,您应该将您的时间转换为 Long
并将其(在您的 note
类中)保存到数据库中。
然后在显示日期时,您必须将数字转换回格式化的日期。
也可以让Room
处理带有时间信息的复杂对象,例如Date
类(由c.getTime()
调用返回)。
作为将Date
对象保存到数据库的示例,您可以定义一个简单的转换器类,如文档中的this example。
【讨论】:
感谢@cewaphi 我是否还必须在笔记实体的构造函数中保存日期对象?谢谢! “在构造函数中保存日期对象”是指类的构造函数将Date
类型变量作为输入参数,因此您的实体具有Date
类型的成员变量?如果你想把它保存到你的 Room 数据库中,你需要创建一个如上所述的转换器类。否则,Room 只能处理原始类型,例如String
和Boolean
请检查我的更改@cewaphi 感谢您的时间!
请问您为什么要尝试为日期创建活动实例而不是从您的currentNote
访问日期?你的savenote
方法在那里完成了吗?我没有看到任何保存操作发生
我这样做是因为我想保存用户单击保存按钮的日期。这是新笔记活动中的一种方法。我认为如果我添加全新的笔记活动类就很清楚了。 @cewaphi以上是关于创建卡片时如何在 recyclerview (cardview) 中添加日期。 (卡片创建日期)的主要内容,如果未能解决你的问题,请参考以下文章