gpt4 book ai didi

java - 使用 FirebaseRecyclerAdapter 将 Firebase 实时数据库中的数据检索到 Recycler View 中

转载 作者:行者123 更新时间:2023-12-01 17:00:36 25 4
gpt4 key购买 nike

我是第一次使用 FirebaseRecyclerAdapter,因此无法使用它并面临一些问题并且无法在屏幕上显示任何数据 This image shows the structure of the databaseHomeFragment 的代码如下


import android.os.Bundle;

import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;

import com.firebase.ui.database.FirebaseRecyclerOptions;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import java.util.ArrayList;
import java.util.List;


/**
* A simple {@link Fragment} subclass.
* Use the {@link HomeFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class HomeFragment extends Fragment {

private RecyclerView recyclerView;
private ProductAdapter adapter;

// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";

// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;

public HomeFragment() {
// Required empty public constructor
}

/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* @param param1 Parameter 1.
* @param param2 Parameter 2.
* @return A new instance of fragment HomeFragment.
*/
// TODO: Rename and change types and number of parameters
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v= inflater.inflate(R.layout.fragment_home, container, false);
recyclerView = v.findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
FirebaseRecyclerOptions<Product> options = new FirebaseRecyclerOptions.Builder<Product>()
.setQuery(FirebaseDatabase.getInstance().getReference().child(FirebaseAuth.getInstance().getCurrentUser().getUid()), Product.class)
.build();

adapter = new ProductAdapter(options);
recyclerView.setAdapter(adapter);
return v;
}

@Override
public void onStart() {
super.onStart();
adapter.startListening();
}

@Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
}

ProductAdapter 的代码是


import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;

public class ProductAdapter extends FirebaseRecyclerAdapter<Product, ProductAdapter.ProductViewHolder> {

public ProductAdapter(@NonNull FirebaseRecyclerOptions<Product> options) {
super(options);
}

@Override
protected void onBindViewHolder(@NonNull ProductViewHolder holder, int position, @NonNull Product model) {
holder.pname.setText(model.getPname());
holder.price.setText(model.getPrice());
holder.description.setText(model.getDescription());
}

@NonNull
@Override
public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view= LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_row, parent, false);

return new ProductViewHolder(view);
}

class ProductViewHolder extends RecyclerView.ViewHolder{
TextView pname, price, description;
ImageView image;
public ProductViewHolder(@NonNull View itemView) {
super(itemView);
pname = itemView.findViewById(R.id.product_list_name);
price = itemView.findViewById(R.id.product_list_price);
description = itemView.findViewById(R.id.product_list_description);
image = itemView.findViewById(R.id.product_image);
}
}
}

Product类的代码如下

public class Product {
String pname,price,description,image,date,category,pid,time;
private boolean expanded;

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public String getCategory() {
return category;
}

public void setCategory(String category) {
this.category = category;
}

public String getPid() {
return pid;
}

public void setPid(String pid) {
this.pid = pid;
}

public String getTime() {
return time;
}

public void setTime(String time) {
this.time = time;
}

public Product() {
}

public Product(String pname, String price, String description, String image, String date, String category, String pid, String time) {
this.pname = pname;
this.price = price;
this.description = description;
this.image = image;
this.date = date;
this.category = category;
this.pid = pid;
this.time = time;
}

public String getPname() {
return pname;
}

public void setPname(String pname) {
this.pname = pname;
}

public String getPrice() {
return price;
}

public void setPrice(String price) {
this.price = price;
}

public String getDescription() {
return description;
}

public void setDescription(String description) {
this.description = description;
}

public String getImage() {
return image;
}

public void setImage(String image) {
this.image = image;
}

public boolean isExpanded() {
return expanded;
}

public void setExpanded(boolean expanded) {
this.expanded = expanded;
}

@Override
public String toString() {
return "Product{" +
"pname='" + pname + '\'' +
", price='" + price + '\'' +
", description='" + description + '\'' +
", image='" + image + '\'' +
", expanded=" + expanded +
'}';
}
}

所以我想从实时数据库中检索产品详细信息,并希望在 fragment 中显示为不同产品的列表

最佳答案

我猜测您的 Product 类中有额外的字段:

在您的 Product 类中:

删除此字段:

private boolean expanded;

也删除这些:

public boolean isExpanded() {
return expanded;
}

public void setExpanded(boolean expanded) {
this.expanded = expanded;
}

关于java - 使用 FirebaseRecyclerAdapter 将 Firebase 实时数据库中的数据检索到 Recycler View 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61510277/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com