即使设置了 getitemcount 和布局管理器,RecyclerView 也不显示任何项目
RecyclerView not displaying any items even after getitemcount is set and layoutmanager is set
我尝试了这里提到的大多数解决方案,但都遇到了类似的问题,但 none 似乎对我有用......现在我观察到的是代码能够从 firebase 获取数据并且它能够设置数据,但是 getitemcount 方法没有更新它仍然显示为零,即使我已经将它设置为项目列表大小
关于我做错了什么的任何帮助都会有所帮助
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>
更改数据后调用 notifyDataSetChanged() 方法。此 android 函数通知附加的观察者基础数据已更改,任何反映数据集的视图都应自行刷新。
public void setCasinoHistoryArrayList(ArrayList<CasinoHistory> casinoHistories)
{
this.casinoHistoryArrayList = casinoHistories;
notifyDataSetChanged();
}
我尝试了这里提到的大多数解决方案,但都遇到了类似的问题,但 none 似乎对我有用......现在我观察到的是代码能够从 firebase 获取数据并且它能够设置数据,但是 getitemcount 方法没有更新它仍然显示为零,即使我已经将它设置为项目列表大小 关于我做错了什么的任何帮助都会有所帮助
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>
更改数据后调用 notifyDataSetChanged() 方法。此 android 函数通知附加的观察者基础数据已更改,任何反映数据集的视图都应自行刷新。
public void setCasinoHistoryArrayList(ArrayList<CasinoHistory> casinoHistories)
{
this.casinoHistoryArrayList = casinoHistories;
notifyDataSetChanged();
}