如何从 Firebase 获取数据到 Recyclerview 中的片段?
Posted
技术标签:
【中文标题】如何从 Firebase 获取数据到 Recyclerview 中的片段?【英文标题】:How to Fetch data form firebase to fragments in Recyclerview? 【发布时间】:2021-10-06 11:50:57 【问题描述】:Java 这是我的应用程序片段的代码,当我运行它时,我的应用程序在我启动应用程序时立即崩溃,得到客户主页片段它崩溃,在 logcat 中它显示 CustomerHomefragemnt 中有错误所以这是我无法使用的代码解决它如果有人可以提供帮助,请回答。这是我的片段代码`
public class CustomerHomeFragment extends Fragment implements
SwipeRefreshLayout.OnRefreshListener
RecyclerView recyclerView;
private List<UpdateDishModel> updateDishModelList;
private CustomerHomeAdapter adapter;
String State, City, Sub;
DatabaseReference dataaa, databaseReference;
SwipeRefreshLayout swipeRefreshLayout;
SearchView searchView;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container,
@Nullable Bundle savedInstanceState)
View v = inflater.inflate(R.layout.fragment_customerhome, null);
setHasOptionsMenu(true);
recyclerView = v.findViewById(R.id.recycle_menu);
recyclerView.setHasFixedSize(true);
Animation animation = AnimationUtils.loadAnimation(getContext(), R.anim.move);
recyclerView.startAnimation(animation);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
updateDishModelList = new ArrayList<>();
swipeRefreshLayout = (SwipeRefreshLayout) v.findViewById(R.id.swipelayout);
swipeRefreshLayout.setOnRefreshListener(this);
swipeRefreshLayout.setColorSchemeResources(R.color.colorPrimaryDark, R.color.green);
swipeRefreshLayout.post(new Runnable()
@Override
public void run()
swipeRefreshLayout.setRefreshing(true);
String userid = FirebaseAuth.getInstance().getCurrentUser().getUid();
dataaa = FirebaseDatabase.getInstance()
.getReference("Customer").child(userid);
dataaa.addListenerForSingleValueEvent(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
Customer cust = dataSnapshot.getValue(Customer.class);
State = cust.getState();
City = cust.getCity();
Sub = cust.getSuburban();
customermenu();
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
);
);
return v;
@Override
public void onRefresh()
customermenu();
private void customermenu()
swipeRefreshLayout.setRefreshing(true);
DatabaseReference databaseReference =
FirebaseDatabase.getInstance().getReference("FoodSupplyDetails")
.child(State).child(City).child(Sub);
databaseReference.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
updateDishModelList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren())
for (DataSnapshot snapshot1 : snapshot.getChildren())
UpdateDishModel updateDishModel =
snapshot1.getValue(UpdateDishModel.class);
updateDishModelList.add(updateDishModel);
adapter = new CustomerHomeAdapter(getContext(), updateDishModelList);
recyclerView.setAdapter(adapter);
swipeRefreshLayout.setRefreshing(false);
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
swipeRefreshLayout.setRefreshing(false);
);
searchView.setOnQueryTextListener(new SearchView.OnQueryTextListener()
@Override
public boolean onQueryTextSubmit(String query)
return false;
@Override
public boolean onQueryTextChange(String newText)
search(newText);
return true;
);
private void search(final String searchtext)
ArrayList<UpdateDishModel> mylist = new ArrayList<>();
for (UpdateDishModel object : updateDishModelList)
if (object.getDishes().toLowerCase().contains(searchtext.toLowerCase()))
mylist.add(object);
adapter = new CustomerHomeAdapter(getContext(), mylist);
recyclerView.setAdapter(adapter);
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater)
inflater.inflate(R.menu.search, menu);
MenuItem menuItem = menu.findItem(R.id.Searchdish);
searchView = (SearchView) menuItem.getActionView();
searchView.setQueryHint("Search Dish");
```
`here is my Homeadapter code`
public class CustomerHomeAdapter extends
RecyclerView.Adapter<CustomerHomeAdapter.ViewHolder>
private Context mcontext;
private List<UpdateDishModel>updateDishModellist;
DatabaseReference databaseReference;
public CustomerHomeAdapter(Context context,List<UpdateDishModel>updateDishModellist)
this.updateDishModellist=updateDishModellist;
this.mcontext=context;
@NonNull
@Override
public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType)
View view=
LayoutInflater.from(mcontext)
.inflate(R.layout.customer_menudish,parent,false);
return new CustomerHomeAdapter.ViewHolder(view);
@Override
public void onBindViewHolder(@NonNull final ViewHolder holder, int position)
final UpdateDishModel updateDishModel=updateDishModellist.get(position);
Glide.with(mcontext).load(updateDishModel.getImageURL()).into(holder.imageView);
holder.Dishname.setText(updateDishModel.getDishes());
updateDishModel.getRandomUID();
updateDishModel.getChefId();
holder.price.setText("Price: ₹ " + updateDishModel.getPrice());
holder.itemView.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent intent=new Intent(mcontext,OrderDish.class);
intent.putExtra("FoodMenu",updateDishModel.getRandomUID());
intent.putExtra("ChefId",updateDishModel.getChefId());
mcontext.startActivity(intent);
);
@Override
public int getItemCount()
return updateDishModellist.size();
public class ViewHolder extends RecyclerView.ViewHolder
ImageView imageView;
TextView Dishname,price;
ElegantNumberButton additem;
public ViewHolder(@NonNull View itemView)
super(itemView);
imageView=itemView.findViewById(R.id.menu_image);
Dishname=itemView.findViewById(R.id.dishname);
price=itemView.findViewById(R.id.dishprice);
additem=itemView.findViewById(R.id.number_btn);
我认为此代码中存在错误,因为我无法解决问题,请帮助我解决此问题
private void customermenu()
swipeRefreshLayout.setRefreshing(true);
DatabaseReference databaseReference =
FirebaseDatabase.getInstance().getReference("FoodSupplyDetails")
.child(State).child(City).child(Sub);
databaseReference.addValueEventListener(new ValueEventListener()
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot)
updateDishModelList.clear();
for (DataSnapshot snapshot : dataSnapshot.getChildren())
for (DataSnapshot snapshot1 : snapshot.getChildren())
UpdateDishModel updateDishModel =
snapshot1.getValue(UpdateDishModel.class);
updateDishModelList.add(updateDishModel);
adapter = new CustomerHomeAdapter(getContext(), updateDishModelList);
recyclerView.setAdapter(adapter);
swipeRefreshLayout.setRefreshing(false);
@Override
public void onCancelled(@NonNull DatabaseError databaseError)
swipeRefreshLayout.setRefreshing(false);
);
enter image description here
enter image description here
【问题讨论】:
你遇到了什么错误? 当我移动到 customerhomefragment 活动时,我的应用程序崩溃了。我认为在从 firebase 获取数据时存在一些问题,我附上了图片链接,该链接定义了错误在 customerhomefragment 的私有 void 方法中。如果可以请帮助我,这会很棒 【参考方案1】:从您的代码来看,您的问题似乎在下面一行:
FirebaseDatabase.getInstance().getReference("FoodSupplyDetails")
.child(State).child(City).child(Sub);
错误告诉您不能为“.child()”传递 null。所以它告诉你 State、City 和 Sub 的值为空。您可以通过转到 Firebase 的控制台数据库并查看具有 firebase id 的相应“客户”的数据结构来确认这一点。
请检查您的 Firebase 数据库并确保客户数据存在。
【讨论】:
我附上了客户和 fooddetails 根的屏幕截图,来自 firebase 库,您可以参考存在数据但未获取数据,请帮助我 Sub 的值是多少?我在客户中没有看到它。没有郊区节目。通过将 Sub 和 value 密钥对添加到您的 Customer 中进行快速测试,然后重试。【参考方案2】:在运行查询之前,您应该确保值不为空。
if (State != null && City != null && Sub != null)
// Run your query here
else
Log.d("TAG", "Data provided is null")
当您获取客户信息时,请尝试记录该信息并检查其中哪些为空:
String State, City, Sub;
//...
State = cust.getState();
City = cust.getCity();
Sub = cust.getSuburban();
Log.d("TAG", State);
Log.d("TAG", City);
Log.d("TAG", Sub);
// call customermenu now
customermenu();
【讨论】:
嘿,它提供的显示数据为空,那么我如何从 firebase 获取它,我的代码有什么问题?? @VaibhavKale 它在哪里记录 null?从数据库或内部客户菜单功能中获取后? 我的活动中没有获取任何内容,但应用程序只是停止崩溃,所以我应该如何获取它?我是这个领域的初学者,请帮助我。 我加入了你的不和谐。我们应该如何以及在哪个房间谈论??【参考方案3】:错误告诉您不能为“.child()”传递 null。所以它告诉你数据的值(存储在 firebase 数据库中)为空。您可以通过访问 Firebase 的控制台数据库并查看与 firebase id 对应的数据结构来确认这一点。
【讨论】:
以上是关于如何从 Firebase 获取数据到 Recyclerview 中的片段?的主要内容,如果未能解决你的问题,请参考以下文章
如何从 Firebase 推送/获取数据并使用 Java 添加到二维数组?
如何从 Firebase 实时数据库中获取数据到 Flutter 中的列表中?
如何从 Firebase 获取数据到 Recyclerview 中的片段?
Flutter - 使用 StreamBuilder 从 firebase 获取数据后,如何使用 ScrollController 跳转到列表视图的底部?