英文:
getting null Value from firestore to recycleview
问题
NotificationActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notifications);
getSupportActionBar().setTitle(R.string.menu_notifications);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
initRecyclerView();
getNotifications();
Adview();
}
private void initRecyclerView() {
Log.d(TAG, "initRecyclerView: 初始化 RecyclerView。");
mRecyclerView = findViewById(R.id.recyclerv_view);
if (mNotificationsAdapter == null) {
mNotificationsAdapter = new NotificationsAdapter(this, mNotifications);
}
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mNotificationsAdapter);
}
private void getNotifications() {
SharedPreferences prefs = getSharedPreferences("TOKEN_PREF", MODE_PRIVATE);
String token = prefs.getString("token", "");
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference notificationsCollectionRef = db.collection("notifications").document(token).collection("messages");
Query query = notificationsCollectionRef
.orderBy("Date", Query.Direction.DESCENDING);
query.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Notifications notifications = document.toObject(Notifications.class);
mNotifications.add(notifications);
}
mNotificationsAdapter.notifyDataSetChanged();
} else {
Log.d(TAG,"查询失败。请检查日志。");
}
});
}
NotificationsAdapter.java:
class NotificationsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final String TAG = "NotificationsAdapter";
private ArrayList<Notifications> mNotifications;
private Context mContext;
public NotificationsAdapter(Context context, ArrayList<Notifications> notifications) {
mNotifications = notifications;
mContext = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
RecyclerView.ViewHolder holder;
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.notifications_listitem, parent, false);
holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: 调用。");
if (holder instanceof ViewHolder) {
((ViewHolder) holder).title.setText(mNotifications.get(position).getTitle());
((ViewHolder) holder).message.setText(mNotifications.get(position).getMessage());
((ViewHolder) holder).date_time.setText(mNotifications.get(position).getDate() + "\n" + mNotifications.get(position).getTime());
((ViewHolder) holder).parentLayout.setOnClickListener(view -> {
Log.d(TAG, "点击事件:点击了 " + mNotifications.get(position).getTitle());
Intent intent = new Intent(mContext, NotificationDetails.class);
// intent.putExtra("image_url", mImages.get(position));
intent.putExtra("shop_name", mNotifications.get(position).getTitle());
intent.putExtra("message", mNotifications.get(position).getMessage());
mContext.startActivity(intent);
});
}
}
@Override
public int getItemCount() {
return mNotifications.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CircleImageView image;
TextView title, message, date_time;
RelativeLayout parentLayout;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.shop_image);
title = itemView.findViewById(R.id.title);
message = itemView.findViewById(R.id.message);
date_time = itemView.findViewById(R.id.date_time);
parentLayout = itemView.findViewById(R.id.parent_layout);
}
}
}
Notifications.java:
@IgnoreExtraProperties
public class Notifications {
private String title;
private String message;
private String date;
private String time;
public Notifications() {
}
public Notifications(String title, String message, String date, String time) {
this.title = title;
this.message = message;
this.date = date;
this.time = time;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
英文:
I have made Notificatiion Activity that getting the data from Cloud Firestore. After I test the app. I got always null from database, I checked in the debug. I found the data is read from database, but not setting it in Notification.java. Here is my code:
NotificationActivity.java:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_notifications);
getSupportActionBar().setTitle(R.string.menu_notifications);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
initRecyclerView();
getNotifications();
Adview();
}
private void initRecyclerView() {
Log.d(TAG, "initRecyclerView: init recyclerview.");
mRecyclerView = findViewById(R.id.recyclerv_view);
if (mNotificationsAdapter == null) {
mNotificationsAdapter = new NotificationsAdapter(this, mNotifications);
}
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mRecyclerView.setAdapter(mNotificationsAdapter);
}
private void getNotifications() {
SharedPreferences prefs = getSharedPreferences("TOKEN_PREF", MODE_PRIVATE);
String token = prefs.getString("token", "");
FirebaseFirestore db = FirebaseFirestore.getInstance();
CollectionReference notificationsCollectionRef = db.collection("notifications").document(token).collection("messages");
Query query = notificationsCollectionRef
.orderBy("Date", Query.Direction.DESCENDING);
query.get().addOnCompleteListener(task -> {
if (task.isSuccessful()) {
for (QueryDocumentSnapshot document : task.getResult()) {
Notifications notifications = document.toObject(Notifications.class);
mNotifications.add(notifications);
}
mNotificationsAdapter.notifyDataSetChanged();
} else {
Log.d(TAG,"Query Failed. Check Logs.");
}
});
}
NotificationsAdapter.java:
class NotificationsAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
private static final String TAG = "NotificationsAdapter";
private ArrayList<Notifications> mNotifications;
private Context mContext;
public NotificationsAdapter(Context context, ArrayList<Notifications> notifications) {
mNotifications = notifications;
mContext = context;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
RecyclerView.ViewHolder holder;
View view = LayoutInflater.from(parent.getContext()).inflate(
R.layout.notifications_listitem, parent, false);
holder = new ViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, final int position) {
Log.d(TAG, "onBindViewHolder: called.");
if (holder instanceof ViewHolder) {
((ViewHolder) holder).title.setText(mNotifications.get(position).getTitle());
((ViewHolder) holder).message.setText(mNotifications.get(position).getMessage());
((ViewHolder) holder).date_time.setText(mNotifications.get(position).getDate() + "\n" + mNotifications.get(position).getTime());
((ViewHolder) holder).parentLayout.setOnClickListener(view -> {
Log.d(TAG, "onClick: clicked on: " + mNotifications.get(position).getTitle());
Intent intent = new Intent(mContext, NotificationDetails.class);
// intent.putExtra("image_url", mImages.get(position));
intent.putExtra("shop_name", mNotifications.get(position).getTitle());
intent.putExtra("message", mNotifications.get(position).getMessage());
mContext.startActivity(intent);
});
}
}
@Override
public int getItemCount() {
return mNotifications.size();
}
public class ViewHolder extends RecyclerView.ViewHolder {
CircleImageView image;
TextView title, message, date_time;
RelativeLayout parentLayout;
public ViewHolder(View itemView) {
super(itemView);
image = itemView.findViewById(R.id.shop_image);
title = itemView.findViewById(R.id.title);
message = itemView.findViewById(R.id.message);
date_time = itemView.findViewById(R.id.date_time);
parentLayout = itemView.findViewById(R.id.parent_layout);
}
}
Notifications.java
@IgnoreExtraProperties
public class Notifications {
private String title;
private String message;
private String date;
private String time;
public Notifications() {
}
public Notifications(String title, String message, String date, String time) {
this.title = title;
this.message = message;
this.date = date;
this.time = time;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getMessage() {
return message;
}
public void setMessage(String message) {
this.message = message;
}
public String getDate() {
return date;
}
public void setDate(String date) {
this.date = date;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
}
答案1
得分: 1
请确保您的Firestore / Firebase 属性与您的 Java/Kotlin 数据类属性相同。我的意思是大写字母和其他所有内容。
英文:
Make sure your Firestore / firebase attributes and you java/Kotlin data class attributes are the same. I mean caps letters and everything.
答案2
得分: 0
调用getNotification()方法首先。
英文:
Call the getNotification() method first.
答案3
得分: 0
我通过将模型类中的变量改为以大写字母开头来解决了这个问题。
英文:
I solved the issue by changing the variables in the model class to be starting with capital letter.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论