Can anyone help with this situation? I want to show history in a recycleview in a fragment from firebase database. But doesn't show. What went wrong i can not figure out. The app works fine but showing nothing in recyclerview.
My history class
public class History {
public String Date;
public String Amount;
public String Via;
public String Mobile_Number;
public History() {
}
public History(String date, String amount, String via, String mobile_Number)
{
Date = date;
Amount = amount;
Via = via;
Mobile_Number = mobile_Number;
}
public String getDate() {
return Date;
}
public void setDate(String date) {
Date = date;
}
public String getAmount() {
return Amount;
}
public void setAmount(String amount) {
Amount = amount;
}
public String getVia() {
return Via;
}
public void setVia(String via) {
Via = via;
}
public String getMobile_Number() {
return Mobile_Number;
}
public void setMobile_Number(String mobile_Number) {
Mobile_Number = mobile_Number;
}
}
I am using this viewholder class.
public class History {
public String Date;
public String Amount;
public String Via;
public String Mobile_Number;
public History() {
}
public History(String date, String amount, String via, String mobile_Number)
{
Date = date;
Amount = amount;
Via = via;
Mobile_Number = mobile_Number;
}
public String getDate() {
return Date;
}
public void setDate(String date) {
Date = date;
}
public String getAmount() {
return Amount;
}
public void setAmount(String amount) {
Amount = amount;
}
public String getVia() {
return Via;
}
public void setVia(String via) {
Via = via;
}
public String getMobile_Number() {
return Mobile_Number;
}
public void setMobile_Number(String mobile_Number) {
Mobile_Number = mobile_Number;
}
}
This fragment is used on pager.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mMainView = inflater.inflate(R.layout.fragment_profile, container,
false);
recyclerView = (RecyclerView)
mMainView.findViewById(R.id.profile_recycler);
String uid = FirebaseAuth.getInstance().getCurrentUser().getUid();
String email = FirebaseAuth.getInstance().getCurrentUser().getEmail();
mDatabase =
FirebaseDatabase.getInstance().getReference().child("Users").child(uid);
mDatabase.keepSynced(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
Query query = FirebaseDatabase.getInstance()
.getReference()
.child("Users").child(uid).child("History").orderByKey();
FirebaseRecyclerOptions<History> options = new
FirebaseRecyclerOptions.Builder<History>()
.setQuery(query, History.class).build();
FirebaseRecyclerAdapter adapter = new
FirebaseRecyclerAdapter<History,HistoryViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull HistoryViewHolder holder,
int position, @NonNull History model) {
holder.setAmountText("100");
holder.setDateText("dd-mm-yyyy");
holder.setMobileText("model.getMobile_Number()");
holder.setViaText("model.getVia()");
}
@Override
public HistoryViewHolder onCreateViewHolder(ViewGroup parent, int
viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.history_layout,parent,false);
return new HistoryViewHolder(view);
}
};
recyclerView.setAdapter(adapter);
return mMainView;
}
History layout to populate every data
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/history_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="0dp"
android:layout_marginTop="4dp"
android:fontFamily="serif"
android:text="Date"
android:textColor="@color/colorPrimary"
android:textStyle="bold" />
<TextView
android:id="@+id/history_mobile_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="@+id/history_date"
android:layout_marginStart="16dp"
android:layout_toEndOf="@+id/history_date"
android:text="Mobile Number" />
<TextView
android:id="@+id/history_via"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/history_mobile_number"
android:layout_below="@id/history_mobile_number"
android:text="Via"
android:textStyle="bold" />
<TextView
android:id="@+id/history_amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignStart="@+id/history_via"
android:layout_below="@+id/history_via"
android:layout_marginBottom="4dp"
android:text="Taka" />
</RelativeLayout>
To get data you need to subscribe with
adapter.startListening();
and then unsubscribe with adapter.stopListening();
You need to start listening after you set your adapter. You can do it in onStart()
method in the fragment and stop in onStop()
. And before you do it add check for the adapter not being null or you may get a nullpointerexeption
. This firebase adapter can be tricky. Once I tried to use it and ended up rewriting the whole thing with the regular RecyclerViewAdapter class.
Also, I see you're using the ""
around the method calls. You are creating a string from this text and it won't be called as expected. Remove the ""
@Override
protected void onBindViewHolder(@NonNull HistoryViewHolder holder,
int position, @NonNull History model) {
holder.setAmountText("100");
holder.setDateText("dd-mm-yyyy");
holder.setMobileText(model.getMobile_Number());
holder.setViaText(model.getVia());
}