我在“回收”视图中是新的,当我显示文本但已经从数据库调用时,我被卡住了。我进行了很多研究,找不到任何问题所在。我不知道我显示的是真还是假。
有人可以帮助我并检查我的代码。
MonitorCatalog.class
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_monitor_catalog, container, false);
recyclerView = (RecyclerView) rootView.findViewById(R.id.recycleview1);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL, false));
list = new ArrayList<CatalogDatabase>();
reference = FirebaseDatabase.getInstance().getReference("Catalog").child("Monitor").child("Basic");
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
CatalogDatabase cd = dataSnapshot.getValue(CatalogDatabase.class);
list.add(cd);
}
adapter = new RecycleAdapter(getActivity(), list);
recyclerView.setAdapter(adapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getContext(), "Opss.. Something is wrong ", Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
RecycleAdapter.class
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.MyViewHolder> {
Context context;
ArrayList<CatalogDatabase> catalogDatabases;
public RecycleAdapter(Context c, ArrayList<CatalogDatabase> cd)
{
context = c;
catalogDatabases = cd;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
return new MyViewHolder(LayoutInflater.from(context).inflate(R.layout.cardview, parent, false));
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
holder.nameItem.setText(catalogDatabases.get(position).getNameItem());
holder.priceItem.setText(catalogDatabases.get(position).getPriceItem());
}
@Override
public int getItemCount() {
return catalogDatabases.size();
}
class MyViewHolder extends RecyclerView.ViewHolder
{
TextView nameItem, priceItem;
public MyViewHolder(@NonNull View itemView) {
super(itemView);
nameItem = (TextView) itemView.findViewById(R.id.basicname);
priceItem = (TextView) itemView.findViewById(R.id.basicprice);
}
}
}
我希望任何人都能帮助我的代码。
答案 0 :(得分:0)
您正在为每个数据创建一个新的适配器,并将其应用于您的RecyclerView
。每次有新数据更改进入时,这都会强制您的列表从0重新加载。相反,请更新列表对象并通知适配器数据已更改。
换句话说:
onDataChange
删除以下几行adapter = new RecycleAdapter(getActivity(), list);
recyclerView.setAdapter(adapter);
// ...
list = new ArrayList<CatalogDatabase>();
adapter = new RecycleAdapter(getActivity(), list);
recyclerView.setAdapter(adapter);
// ...
// ...
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot dataSnapshot1: dataSnapshot.getChildren()){
CatalogDatabase cd = dataSnapshot.getValue(CatalogDatabase.class);
list.add(cd);
}
adapter.notifyDataSetChanged();
}
// ...
如果该项目已添加到列表的末尾(并且此后未发生其他更改),则也可以使用adapter.notifyItemInserted(list.size()-1)
代替adapter.notifyDataSetChanged()
。这将改善您的应用程序性能。
另请参阅其他可能有用的方法adapter.notify-
。