我设法将内容的数据存储在Firebase
数据库中。但是,这些项目根本没有显示在recyclerView
上。我认为loadData()
部分可能存在问题,因为我曾经遇到过一些关于适配器必须事先声明为抽象的问题。但是,我已经修改了代码。
DiaryActivity.java
package com.shiminu1521462c.fyp_2;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.content.Context;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
public class DiaryActivity extends AppCompatActivity {
private FirebaseAuth mAuth;
private DatabaseReference fNotesDatabase;
private RecyclerView mNotesList;
private GridLayoutManager gridLayoutManager;
private DatabaseReference noteRef;
private Toolbar mToolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_diary);
mToolbar = (Toolbar) findViewById(R.id.main_chat_toolbar);
setSupportActionBar(mToolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setTitle("Diary");
mNotesList = (RecyclerView) findViewById(R.id.notes_list);
gridLayoutManager = new GridLayoutManager(this, 2, GridLayoutManager.VERTICAL, false);
mNotesList.setHasFixedSize(true);
mNotesList.setLayoutManager(gridLayoutManager);
mNotesList.addItemDecoration(new GridSpacingDecoration(2, dpToPx(10), true));
mAuth = FirebaseAuth.getInstance();
if (mAuth.getCurrentUser() != null) {
fNotesDatabase = FirebaseDatabase.getInstance().getReference().child("Notes").child(mAuth.getCurrentUser().getUid());
}
loadData();
}
@Override
public void onStart() {
super.onStart();
}
private void loadData() {
FirebaseRecyclerOptions options =
new FirebaseRecyclerOptions.Builder<NoteModel>()
.setQuery(fNotesDatabase, NoteModel.class)
.build();
FirebaseRecyclerAdapter<NoteModel, NoteViewHolder> adapter
= new FirebaseRecyclerAdapter<NoteModel, NoteViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull final NoteViewHolder viewHolder, int position, @NonNull NoteModel model) {
final String noteId = getRef(position).getKey();
fNotesDatabase.child(noteId).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if (dataSnapshot.hasChild("title") && dataSnapshot.hasChild("timestamp")) {
String title = dataSnapshot.child("title").getValue().toString();
String timestamp = dataSnapshot.child("timestamp").getValue().toString();
viewHolder.setNoteTitle(title);
//viewHolder.setNoteTime(timestamp);
GetTimeAgo getTimeAgo = new GetTimeAgo();
viewHolder.setNoteTime(getTimeAgo.getTimeAgo(Long.parseLong(timestamp), getApplicationContext()));
viewHolder.noteCard.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(DiaryActivity.this, DiaryNewNoteActivity.class);
intent.putExtra("noteId", noteId);
startActivity(intent);
}
});
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
@NonNull
@Override
public NoteViewHolder onCreateViewHolder(@NonNull ViewGroup viewHolder, int i) {
View view = LayoutInflater.from(viewHolder.getContext()).inflate(R.layout.activity_diary, viewHolder, false);
NoteViewHolder viewHolder1 = new NoteViewHolder(view);
return viewHolder1;
}
};
mNotesList.setAdapter(adapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.diary_menu, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.diary_new_note_btn:
Intent newIntent = new Intent(DiaryActivity.this, DiaryNewNoteActivity.class);
startActivity(newIntent);
break;
case android.R.id.home:
Intent myIntent = new Intent(DiaryActivity.this, DashboardActivity.class);
startActivity(myIntent);
break;
}
return true;
}
/**
* Converting dp to pixel
*/
private int dpToPx(int dp) {
Resources r = getResources();
return Math.round(TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, r.getDisplayMetrics()));
}
}
Note.java
package com.shiminu1521462c.fyp_2;
import com.google.firebase.database.Exclude;
public class Note {
private String documentId;
private String title;
private String description;
private int priority;
public Note(){
//public no-arg constructor needed
}
public Note(String title, String description, int priority){
this.title = title;
this.description = description;
this.priority = priority;
}
@Exclude
public String getDocumentId() {
return documentId;
}
public void setDocumentId(String documentId) {
this.documentId = documentId;
}
public String getTitle(){
return title;
}
public String getDescription(){
return description;
}
public int getPriority() {
return priority;
}
public void setPriority(int priority) {
this.priority = priority;
}
}
NoteModel.java
package com.shiminu1521462c.fyp_2;
public class NoteModel {
public String noteTitle;
public String noteTime;
public NoteModel() {
}
public NoteModel(String noteTitle, String noteTime) {
this.noteTitle = noteTitle;
this.noteTime = noteTime;
}
public String getNoteTitle() {
return noteTitle;
}
public void setNoteTitle(String noteTitle) {
this.noteTitle = noteTitle;
}
public String getNoteTime() {
return noteTime;
}
public void setNoteTime(String noteTime) {
this.noteTime = noteTime;
}
}
NoteViewHolder.java
package com.shiminu1521462c.fyp_2;
import android.view.View;
import android.widget.TextView;
import androidx.cardview.widget.CardView;
import androidx.recyclerview.widget.RecyclerView;
public class NoteViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView textTitle, textTime;
CardView noteCard;
public NoteViewHolder(View itemView) {
super(itemView);
mView = itemView;
textTitle = mView.findViewById(R.id.note_title);
textTime = mView.findViewById(R.id.note_time);
noteCard = mView.findViewById(R.id.note_card);
}
public void setNoteTitle(String title) {
textTitle.setText(title);
}
public void setNoteTime(String time) {
textTime.setText(time);
}
}
activity_diary.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background"
tools:context=".DiaryActivity">
<androidx.appcompat.widget.Toolbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/main_chat_toolbar"
android:background="@color/colorPrimary"
android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">
</androidx.appcompat.widget.Toolbar>
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/notes_list"
android:layout_below="@id/main_chat_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.recyclerview.widget.RecyclerView>
</RelativeLayout>
single_note_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="156dp">
<androidx.cardview.widget.CardView
android:id="@+id/note_card"
android:layout_width="match_parent"
android:layout_height="128dp"
android:layout_margin="5dp"
android:clickable="true"
android:focusable="true"
android:maxHeight="128dp"
android:minHeight="128dp"
app:contentPadding="5dp">
<LinearLayout
android:layout_width="156dp"
android:layout_height="match_parent"
android:minWidth="156dp"
android:orientation="vertical">
<TextView
android:id="@+id/note_title"
android:layout_width="156dp"
android:layout_height="0dp"
android:layout_marginBottom="10dp"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="5"
android:text="Note Title"
/>
<TextView
android:id="@+id/note_time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="1"
android:text="Updated time"
android:textSize="12sp" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>