在我的应用中,我正在计算日期时差。在firebase中插入节点时,我节省了创建节点的当前时间。在适配器中,我希望以秒为单位或几天显示cardview
的差异
问题是适配器中没有编译时错误。但是textview中没有显示任何内容。
这是我在firebase中插入节点的活动
addMovie.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String currentDateTime = DateFormat.getDateTimeInstance().format(new Date());
String title = etTitle.getText().toString();
String desc = etDesc.getText().toString();
String url = etUrl.getText().toString();
String cst = etCast.getText().toString();
savedData(title, desc, url,cst,currentDateTime);
}
private void savedData(String title, String desc, String url,String cast,String time) {
String key = updateId;
if (updateId == null) {
key = databaseReference.push().getKey();
}
uploadImage(selectedimg, key);
Movie movie = new Movie(title, desc, url, key,cast,time);
Toast.makeText(this, "movie "+movie.getCast(), Toast.LENGTH_SHORT).show();
movieReference.child(key).setValue(movie);
etTitle.setText("");
etDesc.setText("");
etUrl.setText("");
movieImage.setImageResource(0);
pd.dismiss();
}
这是我的适配器
public class MyAdapter extends RecyclerView.Adapter<MyViewHolder> {
ArrayList<Movie> movies;
movieItemListener listener;
Context context;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
DecimalFormat df;
public MyAdapter(ArrayList<Movie> movies) {
this.movies = movies;
}
public MyAdapter(@NonNull Context context, ArrayList<Movie> movies, movieItemListener listener) {
this.context = context;
this.movies = movies;
this.listener = listener;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.movie_layout, parent, false);
MyViewHolder viewHolder = new MyViewHolder(view);
return viewHolder;
}
@Override
public void onBindViewHolder(final MyViewHolder holder, final int position) {
df = new DecimalFormat("#.#");
final Movie movie = movies.get(position);
String currentTime = DateFormat.getDateTimeInstance().format(new Date());
final String saveTime = movie.getCurrentDateTime();
Toast.makeText(context, "abc"+getTimeDiff(currentTime, saveTime), Toast.LENGTH_SHORT).show();
holder.tvDate.setText(getTimeDiff(currentTime, saveTime));
holder.tvRatings.setText(df.format(movie.getAvgRating()));
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference().child("movies");
databaseReference.child(movie.getId()).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
holder.tvRatings.setText(df.format(dataSnapshot.child("avgRating").getValue()));
holder.tvViews.setText(String.valueOf(dataSnapshot.child("viewsCount").getValue()) + " Views");
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
holder.tvTitle.setText(movie.getTitle());
holder.overflow.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
showPopupMenu(holder.overflow, position, movie);
}
});
}
private String getTimeDiff(String currentTime, String saveTime) {
String diff="";
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/M/yyyy hh:mm:ss");
try {
Date date1 = simpleDateFormat.parse(currentTime);
Date date2 = simpleDateFormat.parse(saveTime);
diff= printDifference(date1, date2);
Toast.makeText(context, "in diff ", Toast.LENGTH_SHORT).show();
} catch (ParseException e) {
e.printStackTrace();
}
return diff;
}
public String printDifference(Date startDate, Date endDate) {
long different = endDate.getTime() - startDate.getTime();
long secondsInMilli = 1000;
long minutesInMilli = secondsInMilli * 60;
long hoursInMilli = minutesInMilli * 60;
long daysInMilli = hoursInMilli * 24;
long elapsedDays = different / daysInMilli;
different = different % daysInMilli;
long elapsedHours = different / hoursInMilli;
different = different % hoursInMilli;
long elapsedMinutes = different / minutesInMilli;
different = different % minutesInMilli;
long elapsedSeconds = different / secondsInMilli;
String days=String.valueOf(elapsedDays);
String hours=String.valueOf(elapsedHours);
String minutes=String.valueOf(elapsedMinutes);
String seconds=String.valueOf(elapsedSeconds);
/* if(elapsedSeconds>59)
return minutes;
else if(elapsedMinutes>59)
return hours;
else if(elapsedHours>24)
return days;
else*/
return seconds;
}
private void showPopupMenu(View view, final int position, final Movie movie) {
PopupMenu popup = new PopupMenu(context, view);
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.movie_menu, popup.getMenu());
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_edit:
listener.onUpdate(movie);
notifyDataSetChanged();
return true;
case R.id.action_delete:
listener.onDelete(movie, position);
return true;
default:
}
return false;
}
});
popup.show();
}
@Override
public int getItemCount() {
return movies.size();
}
}
我一直在寻找很长时间但未能找到解决办法 有谁知道我在这做错了什么?
答案 0 :(得分:2)
您使用的是错误的模式,这会导致异常并且diff
将作为空标记返回,因此您可以使用
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("dd/MM/yyyy hh:mm:ss");
try {
Date date1 = simpleDateFormat.parse(currentTime);
Date date2 = simpleDateFormat.parse(saveTime);
即使是更好的解决方案,也可以使用
DateFormat df = DateFormat.getDateTimeInstance();
try {
Date date1 = df.parse(currentTime);
Date date2 = df.parse(saveTime);
答案 1 :(得分:0)
我完全不明白,但希望这可能有用 在您的节点中,将当前日期保存为
long currentDateTime=new Date().getTime();
然后在你的getTimeDifference方法
private String getTimeDifference(){
long difference=new Date().getTime()-movie.getCurrentDateTime();
DateFormat formatter = new SimpleDateFormat("HH:mm:ss:SSS");
return formatter.format(new Date(difference));
}