如何在片段中显示来自 firebase 实时数据库的 recyclerView 数据。 Android 工作室
How to show the recyclerView data from firebase realtime database in fragments. Android studio
fragment_leaderboard.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:orientation="vertical"
tools:context=".LeaderboardsFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
row_leaderboard.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"
android:layout_width="match_parent"?
android:layout_height="wrap_content"
android:background="@color/cream">
<TextView
android:id="@+id/index"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:fontFamily="@font/gotham_bold"
android:text="#1"
android:textColor="@color/brown"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="@+id/imageView7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageView7" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/index"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/menu_profile2" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:fontFamily="@font/gotham_bold"
android:text="Name of Winner"
android:textColor="@color/brown"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/imageView7"
app:layout_constraintStart_toEndOf="@+id/imageView7"
app:layout_constraintTop_toTopOf="@+id/imageView7" />
<TextView
android:id="@+id/coins"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:fontFamily="@font/gotham_bold"
android:text="1230"
android:textColor="@color/brown"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/name" />
</androidx.constraintlayout.widget.ConstraintLayout>
LeaderBoardAdapter.java
package com.android.myquiz;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.android.myquiz.databinding.RowLeaderboardBinding;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
public class LeaderBoardAdapter extends
RecyclerView.Adapter<LeaderBoardAdapter.LeaderboardViewHolder> {
Context context;
ArrayList<User> users;
public LeaderBoardAdapter(Context context, ArrayList<User> users){
this.context = context;
this.users = users;
}
@NonNull
@NotNull
@Override
public LeaderboardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{
查看 view = LayoutInflater.from(context).inflate(R.layout.row_leaderboard, parent,
错误的);
return 新的 LeaderboardViewHolder(视图);
}
@Override
public void onBindViewHolder(@NonNull LeaderboardViewHolder holder, int position) {
User user = users.get(position);
holder.binding.name.setText(user.getName1());
holder.binding.coins.setText(String.valueOf(user.getCoins()));
holder.binding.index.setText(String.format("#%d", position+1));
}
@Override
public int getItemCount() {
return users.size();
}
public class LeaderboardViewHolder extends RecyclerView.ViewHolder {
TextView id;
RowLeaderboardBinding binding;
public LeaderboardViewHolder(@NonNull View itemView) {
super(itemView);
binding = RowLeaderboardBinding.bind(itemView);
}
}
}
LeaderboardFragments.java
package com.android.myquiz;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.myquiz.databinding.FragmentLeaderboardsBinding;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;
import org.w3c.dom.Document;
import java.util.ArrayList;
public class LeaderboardsFragment extends Fragment {
public LeaderboardsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
FragmentLeaderboardsBinding binding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentLeaderboardsBinding.inflate(inflater, container,false);
FirebaseFirestore database = FirebaseFirestore.getInstance();
final ArrayList<User> users = new ArrayList<>();
final LeaderBoardAdapter adapter = new LeaderBoardAdapter(getContext(), users);
binding.recyclerView.setAdapter(adapter);
binding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
database.collection("users")
.orderBy("coins", Query.Direction.DESCENDING).get().addOnSuccessListener(new
OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (DocumentSnapshot snapshot : queryDocumentSnapshots){
User user = snapshot.toObject(User.class);
users.add(user);
}
adapter.notifyDataSetChanged();
}
});
return binding.getRoot();
}
}
这是我的代码,我想在片段中从实时数据库中的 recyclerView 中检索数据。
好的,看起来您可以从 firebase 获取数据,但是在设置数据时出错了。
当您调用 adapter.notifyDataSetChanged()
时,您需要注意将新的 users
列表传递给适配器。
您需要在适配器中创建一个方法 class,
public void setUsersList(ArrayList<User> users) {
this.users = users;
notifyDatasetChanged();
}
并在您的片段中调用它,例如 adapter.setUsersList(users)
,而不是调用 adapter.notifyDataSetChanged()
.
fragment_leaderboard.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:orientation="vertical"
tools:context=".LeaderboardsFragment">
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
row_leaderboard.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"
android:layout_width="match_parent"?
android:layout_height="wrap_content"
android:background="@color/cream">
<TextView
android:id="@+id/index"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:fontFamily="@font/gotham_bold"
android:text="#1"
android:textColor="@color/brown"
android:textSize="15sp"
app:layout_constraintBottom_toBottomOf="@+id/imageView7"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="@+id/imageView7" />
<ImageView
android:id="@+id/imageView7"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="@+id/index"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/menu_profile2" />
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:fontFamily="@font/gotham_bold"
android:text="Name of Winner"
android:textColor="@color/brown"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/imageView7"
app:layout_constraintStart_toEndOf="@+id/imageView7"
app:layout_constraintTop_toTopOf="@+id/imageView7" />
<TextView
android:id="@+id/coins"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:fontFamily="@font/gotham_bold"
android:text="1230"
android:textColor="@color/brown"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="@+id/name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="@+id/name" />
</androidx.constraintlayout.widget.ConstraintLayout>
LeaderBoardAdapter.java
package com.android.myquiz;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.android.myquiz.databinding.RowLeaderboardBinding;
import org.jetbrains.annotations.NotNull;
import java.util.ArrayList;
public class LeaderBoardAdapter extends
RecyclerView.Adapter<LeaderBoardAdapter.LeaderboardViewHolder> {
Context context;
ArrayList<User> users;
public LeaderBoardAdapter(Context context, ArrayList<User> users){
this.context = context;
this.users = users;
}
@NonNull
@NotNull
@Override
public LeaderboardViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
{ 查看 view = LayoutInflater.from(context).inflate(R.layout.row_leaderboard, parent, 错误的); return 新的 LeaderboardViewHolder(视图); }
@Override
public void onBindViewHolder(@NonNull LeaderboardViewHolder holder, int position) {
User user = users.get(position);
holder.binding.name.setText(user.getName1());
holder.binding.coins.setText(String.valueOf(user.getCoins()));
holder.binding.index.setText(String.format("#%d", position+1));
}
@Override
public int getItemCount() {
return users.size();
}
public class LeaderboardViewHolder extends RecyclerView.ViewHolder {
TextView id;
RowLeaderboardBinding binding;
public LeaderboardViewHolder(@NonNull View itemView) {
super(itemView);
binding = RowLeaderboardBinding.bind(itemView);
}
}
}
LeaderboardFragments.java
package com.android.myquiz;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.android.myquiz.databinding.FragmentLeaderboardsBinding;
import com.google.android.gms.tasks.OnSuccessListener;
import com.google.firebase.firestore.DocumentSnapshot;
import com.google.firebase.firestore.FirebaseFirestore;
import com.google.firebase.firestore.Query;
import com.google.firebase.firestore.QuerySnapshot;
import org.w3c.dom.Document;
import java.util.ArrayList;
public class LeaderboardsFragment extends Fragment {
public LeaderboardsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
FragmentLeaderboardsBinding binding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
// Inflate the layout for this fragment
binding = FragmentLeaderboardsBinding.inflate(inflater, container,false);
FirebaseFirestore database = FirebaseFirestore.getInstance();
final ArrayList<User> users = new ArrayList<>();
final LeaderBoardAdapter adapter = new LeaderBoardAdapter(getContext(), users);
binding.recyclerView.setAdapter(adapter);
binding.recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
database.collection("users")
.orderBy("coins", Query.Direction.DESCENDING).get().addOnSuccessListener(new
OnSuccessListener<QuerySnapshot>() {
@Override
public void onSuccess(QuerySnapshot queryDocumentSnapshots) {
for (DocumentSnapshot snapshot : queryDocumentSnapshots){
User user = snapshot.toObject(User.class);
users.add(user);
}
adapter.notifyDataSetChanged();
}
});
return binding.getRoot();
}
}
这是我的代码,我想在片段中从实时数据库中的 recyclerView 中检索数据。
好的,看起来您可以从 firebase 获取数据,但是在设置数据时出错了。
当您调用 adapter.notifyDataSetChanged()
时,您需要注意将新的 users
列表传递给适配器。
您需要在适配器中创建一个方法 class,
public void setUsersList(ArrayList<User> users) {
this.users = users;
notifyDatasetChanged();
}
并在您的片段中调用它,例如 adapter.setUsersList(users)
,而不是调用 adapter.notifyDataSetChanged()
.