I updated to firebase-ui 6.3.0 which doesn't have the populateViewHolder method anymore. I changed the code according to the examples I found online. RecycleView didn't show anything until I added adapter.startListening() to the onStart method of my home activity.
The problem is that only 1 item is displayed and can't figure it out why. Since I use linearLayoutManager.setStackFromEnd(true) the item displayed is the last item, but if I comment out that line the first item is displayed (and the context manu doesn't work either).
I checked some similiar StackOverflow questions but couldn't find the problem.
Firebase RecyclerView OnDataChanged only last element is Shown
Recycler View showing only very last item added to realtime database
Here are my classes and xmls:
HomeActivity:
private RecyclerView transactionsRecyclerView;
private TransactionRecordAdapter transactionRecordAdapter;
private FirebaseHelper firebaseHelper;
private Query query;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
firebaseHelper = new FirebaseHelper();
query = this.firebaseHelper.getTransactionRecordsOrderedByDateQuery();
// FirebaseDatabase.getInstance().getReference().child(ConstantStringValue.DB_CHILD_TRANSACTION_RECORDS).child(auth.getCurrentUser().getUid())
// .orderByChild("date");
transactionsRecyclerView = findViewById(R.id.transactionsRecyclerView);
LinearLayoutManager linearLayoutManager = new LinearLayoutManager(this);
linearLayoutManager.setReverseLayout(true);
linearLayoutManager.setStackFromEnd(true);
transactionsRecyclerView.setLayoutManager(linearLayoutManager);
DividerItemDecoration itemDecoration = new DividerItemDecoration(this, VERTICAL);
transactionsRecyclerView.addItemDecoration(itemDecoration);
registerForContextMenu(transactionsRecyclerView);
}
@Override
protected void onStart() {
super.onStart();
transactionRecordAdapter = new TransactionRecordAdapter(TransactionRecord.class, query, this);
transactionsRecyclerView.setAdapter(transactionRecordAdapter);
transactionRecordAdapter.startListening();
}
@Override
protected void onStop() {
super.onStop();
transactionRecordAdapter.stopListening();
}
TransactionRecordAdapter:
private int position;
private String selectedItemId;
private Context context;
public TransactionRecordAdapter(Class<TransactionRecord> modelClass, Query ref, Context context) {
super(new FirebaseRecyclerOptions.Builder<TransactionRecord>()
.setQuery(ref, modelClass)
.build());
this.context = context;
}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
public String getSelectedItemId() {
return selectedItemId;
}
public void setSelectedItemId(String selectedItemId) {
this.selectedItemId = selectedItemId;
}
@Override
protected void onBindViewHolder(@NonNull final TransactionRecordViewHolder holder, int position,
@NonNull TransactionRecord model) {
holder.itemView.setOnLongClickListener(v -> {
setPosition(holder.getAdapterPosition());
setSelectedItemId(holder.getId());
return false;
});
holder.setAnnouncement(model.getAnnouncement());
...
holder.setId(this.getRef(position).getKey());
}
@NonNull
@Override
public TransactionRecordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.transrecord, parent, false);
return new TransactionRecordViewHolder(view);
}
@Override
public void onViewRecycled(TransactionRecordViewHolder holder) {
holder.itemView.setOnLongClickListener(null);
super.onViewRecycled(holder);
}
TransactionRecordViewHolder:
private View view;
private String id;
public TransactionRecordViewHolder(View itemView) {
super(itemView);
view = itemView;
view.setOnCreateContextMenuListener(this);
}
// ... setters
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
@Override
public void onCreateContextMenu(ContextMenu contextMenu, View view, ContextMenu.ContextMenuInfo contextMenuInfo) {
contextMenu.add(Menu.NONE, R.id.editTransHomeContext,
Menu.NONE, "Szerkesztés");
contextMenu.add(Menu.NONE, R.id.deleteTransHomeContext,
Menu.NONE, "Törlés");
}
activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
tools:context="hu.beczdev.cashbudget.activity.HomeActivity">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="@style/AppTheme.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/transactionsRecyclerView" />
</LinearLayout>
transrecord.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="@+id/transAmountTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:paddingStart="25dp"
android:paddingTop="10dp"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="@+id/transDateTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:paddingTop="10dp"
android:paddingEnd="25dp"
android:textSize="14sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18sp"
android:paddingStart="25dp"
android:textStyle="bold"
android:id="@+id/transCatNameTextView"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:textSize="15sp"
android:paddingStart="25dp"
android:id="@+id/transCommentView"/>
</LinearLayout>
</LinearLayout>
I think your issue not in firebase as it already returned a row, you at least need to wrap_content
of the height of your list item to not occupy the entire height of the RecyclerView
.
so in transrecord.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content" >