Issue
I tried most of the solution which were mentioned here with similar problem but none seems to work for me...for now what i have observed is that the code is able to get the data from firebase and it is able to set the data but the getitemcount method is not updating it is still display zero even after i have set it to the itemlist size any help as to what i am doing wrong would be helpful
Activity class
package com.example.sololeveling;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import android.os.Bundle;
import android.widget.Toast;
import com.example.sololeveling.DAO.CasinoCouponsDao;
import com.example.sololeveling.DAO.CasinoHistoryDao;
import com.example.sololeveling.DAO.CouponDao;
import com.example.sololeveling.DAO.UserDAO;
import com.example.sololeveling.Entity.CasinoHistory;
import com.example.sololeveling.adapter.CasinoHistoryAdapter;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class HistoryActivity extends AppCompatActivity {
CasinoHistoryDao casinoHistoryDao;
RecyclerView casinoHistoryView;
CasinoHistoryAdapter casinoHistoryAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
initializeLevelObjects();
loadRollHistory();
casinoHistoryView.setAdapter(casinoHistoryAdapter);
casinoHistoryView.setLayoutManager(new LinearLayoutManager(this));
}
private void loadRollHistory()
{
casinoHistoryDao.get().addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
ArrayList<CasinoHistory> casinoHistoryArrayList = new ArrayList<>();
for(DataSnapshot dataSnapshot : snapshot.getChildren())
{
CasinoHistory casinoHistory = dataSnapshot.getValue(CasinoHistory.class);
casinoHistory.setKey(dataSnapshot.getKey());
casinoHistoryArrayList.add(casinoHistory);
}
casinoHistoryAdapter.setCasinoHistoryArrayList(casinoHistoryArrayList);
}
@Override
public void onCancelled(@NonNull DatabaseError error) {
Toast.makeText(HistoryActivity.this, "Fail to get data.", Toast.LENGTH_SHORT).show();
}
});
}
private void initializeLevelObjects()
{
casinoHistoryDao = new CasinoHistoryDao();
casinoHistoryView = findViewById(R.id.historyrollview);
casinoHistoryAdapter = new CasinoHistoryAdapter(this);
}
}
Adapter class
package com.example.sololeveling.adapter;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.sololeveling.Entity.CasinoHistory;
import com.example.sololeveling.R;
import com.example.sololeveling.ViewHolder.CasinoHistoryViewHolder;
import java.util.ArrayList;
public class CasinoHistoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder>
{
private Context context;
ArrayList<CasinoHistory> casinoHistoryArrayList = new ArrayList<>();
public CasinoHistoryAdapter(Context context) {
this.context = context;
}
public void setCasinoHistoryArrayList(ArrayList<CasinoHistory> casinoHistories)
{
this.casinoHistoryArrayList = casinoHistories;
}
@NonNull
@Override
public RecyclerView.ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(context).inflate(R.layout.casino_history_list, parent, false);
return new CasinoHistoryViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
CasinoHistoryViewHolder casinoHistoryViewHolder = (CasinoHistoryViewHolder) holder;
CasinoHistory casinoHistory = casinoHistoryArrayList.get(position);
casinoHistoryViewHolder.historyView.setText("you got " + casinoHistory.getCoupon() + " at " + casinoHistory.getDate());
}
@Override
public int getItemCount()
{
return casinoHistoryArrayList.size();
}
}
ViewHolder class
package com.example.sololeveling.ViewHolder;
import android.util.Log;
import android.view.View;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.example.sololeveling.R;
public class CasinoHistoryViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener
{
public TextView historyView;
public CasinoHistoryViewHolder(@NonNull View itemView) {
super(itemView);
historyView = itemView.findViewById(R.id.historyview);
Log.d("CasinoHistoryViewHolder","inside");
}
@Override
public void onClick(View view) {
}
}
main activity xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HistoryActivity">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/historyrollview"
android:layout_width="412dp"
android:layout_height="729dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
item list xml
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.card.MaterialCardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="75dp"
android:clickable="true"
android:focusable="true"
app:cardUseCompatPadding="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="@+id/historyview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="daily task"
android:textAppearance="@style/TextAppearance.AppCompat.Large" />
</LinearLayout>
</com.google.android.material.card.MaterialCardView>
Solution
Call notifyDataSetChanged() method after changing your data. This android function notifies the attached observers that the underlying data has been changed and any View reflecting the data set should refresh itself.
public void setCasinoHistoryArrayList(ArrayList<CasinoHistory> casinoHistories)
{
this.casinoHistoryArrayList = casinoHistories;
notifyDataSetChanged();
}
Answered By - Sartaj Roshan
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.