从 Firestore 回收视图中获取文件时遇到问题 [重复]

Posted

技术标签:

【中文标题】从 Firestore 回收视图中获取文件时遇到问题 [重复]【英文标题】:Having problem to fetching file from firestore recycle view [duplicate] 【发布时间】:2021-07-05 16:14:45 【问题描述】:

我是一名 android 开发初学者,我在从主要活动的 firestore 回收视图中获取文件时遇到问题,但我可以从片段中获取文件。

这是我的主要活动

public class FoodInfoActivity extends AppCompatActivity 
RecyclerView recyclerView1,recyclerView2;
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference notebookRef;
    private NoteAdapter adapter;
    FirebaseAuth fAuth;
    @Override
    protected void onCreate(Bundle savedInstanceState) 
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_food_info);
        fAuth = FirebaseAuth.getInstance();
        setUpRecyclerView();
    

    private void setUpRecyclerView() 
        notebookRef = db.collection("FoodInside");
        Query query = notebookRef;
        FirestoreRecyclerOptions<FoodAdapter> options = new FirestoreRecyclerOptions.Builder<FoodAdapter>()
                .setQuery(query, FoodAdapter.class)
                .build();
        adapter = new NoteAdapter(options);
        RecyclerView recyclerView = findViewById(R.id.fi);
        recyclerView.setHasFixedSize(true);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.setAdapter(adapter);


    
        

这是我使用模型类获取文件的 Note 适配器

public class NoteAdapter extends FirestoreRecyclerAdapter<FoodAdapter, NoteAdapter.NoteHolder> 
    public NoteAdapter(@NonNull FirestoreRecyclerOptions<FoodAdapter> options) 
        super(options);
    
    @Override
    protected void onBindViewHolder(@NonNull NoteHolder holder, int position, @NonNull FoodAdapter foodAdapter) 
        holder.textViewTitle.setText(foodAdapter.getRestaurant());
        holder.textViewDescription.setText(foodAdapter.getItem());
        holder.textViewPriority.setText(String.valueOf(foodAdapter.getPrice()));
    
    @NonNull
    @Override
    public NoteHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) 
        View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.food_recycle_layout,
                parent, false);
        return new NoteHolder(v);
    
    class NoteHolder extends RecyclerView.ViewHolder 
        TextView textViewTitle;
        TextView textViewDescription;
        TextView textViewPriority;
        public NoteHolder(View itemView) 
            super(itemView);
            textViewTitle = itemView.findViewById(R.id.textView95a);
            textViewDescription = itemView.findViewById(R.id.textView123a);
            textViewPriority = itemView.findViewById(R.id.textView124a);
        
    

这是我的适配器类,

public class FoodAdapter 
    String Restaurant;
    String Item;
    String Price;
    String item_id;

    public FoodAdapter() 
    

    public FoodAdapter(String restaurant, String item, String price,String item_id) 
        Restaurant = restaurant;
        Item = item;
        Price = price;
        this.item_id = item_id;
    
public String getRestaurant() 
    return Restaurant;


public void setRestaurant(String restaurant) 
    Restaurant = restaurant;


public String getItem() 
    return Item;


public void setItem(String item) 
    Item = item;


public String getPrice() 
    return Price;


public void setPrice(String price) 
    Price = price;


public String getItem_id() 
    return item_id;


public void setItem_id(String item_id) 
    this.item_id = item_id;


为什么它不在主要活动中工作,而是在工作片段中工作。我只是害怕。累了……在这个项目上工作。所以需要解决这个问题

【问题讨论】:

【参考方案1】:
@Override
protected void onStart() 
    super.onStart();
    adapter.startListening();



@Override
protected void onStop() 
    super.onStop();
    adapter.stopListening();

将此行添加到主要活动。

【讨论】:

以上是关于从 Firestore 回收视图中获取文件时遇到问题 [重复]的主要内容,如果未能解决你的问题,请参考以下文章

回收站视图混合了其项目的内容

Firestore回收器适配器无法获取文档名称

Firestore如何在回收站视图中从firestore检索数据

Android Firestore 过滤器回收视图

从 mvvm 获取的数据在第一次访问时不会在回收站视图中更新

从 firestore 获取 doc