Items are not showing up in my RecyclerView despite no compilation error
up vote
0
down vote
favorite
I've managed to store the data of the content in the Firebase
database. However, the items are not showing up on the recyclerView
at all. I think it might be an issue with my loadData()
section as I've ran into some issues regarding adapter must be declared abstract, previously. However, I modified the code already.
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>
java android android-studio android-recyclerview recycler-adapter
add a comment |
up vote
0
down vote
favorite
I've managed to store the data of the content in the Firebase
database. However, the items are not showing up on the recyclerView
at all. I think it might be an issue with my loadData()
section as I've ran into some issues regarding adapter must be declared abstract, previously. However, I modified the code already.
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>
java android android-studio android-recyclerview recycler-adapter
add a comment |
up vote
0
down vote
favorite
up vote
0
down vote
favorite
I've managed to store the data of the content in the Firebase
database. However, the items are not showing up on the recyclerView
at all. I think it might be an issue with my loadData()
section as I've ran into some issues regarding adapter must be declared abstract, previously. However, I modified the code already.
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>
java android android-studio android-recyclerview recycler-adapter
I've managed to store the data of the content in the Firebase
database. However, the items are not showing up on the recyclerView
at all. I think it might be an issue with my loadData()
section as I've ran into some issues regarding adapter must be declared abstract, previously. However, I modified the code already.
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>
java android android-studio android-recyclerview recycler-adapter
java android android-studio android-recyclerview recycler-adapter
edited Nov 9 at 7:05
Paul Karam
2,34061734
2,34061734
asked Nov 9 at 3:24
Shi Min Lee
65
65
add a comment |
add a comment |
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
active
oldest
votes
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53219409%2fitems-are-not-showing-up-in-my-recyclerview-despite-no-compilation-error%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown