如何在FirestoreRecyclerAdapter Android内部修改视图

时间:2018-09-16 18:55:01

标签: android google-cloud-firestore

我有以下FirestoreRecyclerAdapter

public class AdaptadorVentaCervezas extends FirestoreRecyclerAdapter<Cerveza, AdaptadorVentaCervezas.ViewHolder> {

public AdaptadorVentaCervezas(@NonNull FirestoreRecyclerOptions<Cerveza> options) {
    super(options);
}
@Override
public AdaptadorVentaCervezas.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.venta_cervezas, parent, false);
    return new AdaptadorVentaCervezas.ViewHolder(view);
}
class ViewHolder extends RecyclerView.ViewHolder {
    public TextView txtNombre, txtCantidad;
    public Button btnMenos, btnMas;
    public String CervezaID;

    public ViewHolder(View itemView) {
        super(itemView);
        txtNombre = (TextView) itemView.findViewById(R.id.txtNombre);
        txtCantidad = (TextView) itemView.findViewById(R.id.txtCantidad);
        btnMenos = (Button) itemView.findViewById(R.id.btMenosCerveza);
        btnMenos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int cantidad = VentaActivity.cantidadCervezaMap.get(CervezaID)-1;
                VentaActivity.cantidadCervezaMap.put(CervezaID, cantidad);
                txtCantidad.setText(Integer.toString(cantidad));
            }
        });

        btnMas = (Button) itemView.findViewById(R.id.btMasCerveza);
        btnMas.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                int cantidad = VentaActivity.cantidadCervezaMap.get(CervezaID)+1;
                VentaActivity.cantidadCervezaMap.put(CervezaID, cantidad);
                txtCantidad.setText(Integer.toString(cantidad));
            }
        });

    }
}
@Override
protected void onBindViewHolder(@NonNull AdaptadorVentaCervezas.ViewHolder holder, int position, @NonNull Cerveza item) {
    holder.txtNombre.setText(item.getNombre());
    holder.CervezaID = item.getId(); //Id the field, not the ID of the document
    VentaActivity.cervezaMap.put(holder.CervezaID,item);
    VentaActivity.cantidadCervezaMap.put(holder.CervezaID, 0);
}

}

在主要活动中,实现如下

Query query = FirebaseFirestore.getInstance()
            .collection("Cervezas").whereEqualTo("disponibilidad", true)
            .limit(50);
    opciones = new FirestoreRecyclerOptions
            .Builder<Cerveza>().setQuery(query, Cerveza.class).build();
    adaptador = new AdaptadorVentaCervezas(opciones);
    recyclerView = findViewById(R.id.reciclerViewVentaCervezas);
    recyclerView.setLayoutManager(new LinearLayoutManager(this));
    recyclerView.setAdapter(adaptador);

这工作得很好。现在,我在尝试向主要活动添加按钮时遇到了麻烦,即单击该按钮时会将 txtCantidad 的“ 0”分配给recyclerview的每个项目。如何访问recyclerview的textViews并设置新文本?

谢谢

1 个答案:

答案 0 :(得分:-1)

从此示例中为您的解决方案选择一两个想法

public class HomeFragment extends Fragment {

private RecyclerView blog_list_view;
private List<BlogPost> blog_list;

private FirebaseFirestore firebaseFirestore;
private FirebaseAuth firebaseAuth;
private BlogRecyclerAdapter blogRecyclerAdapter;

private DocumentSnapshot lastVisible;
private Boolean isFirstPageFirstLoad = true;

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


@Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container,
                         Bundle savedInstanceState) {

    View view = inflater.inflate(R.layout.fragment_home, container, false);

    blog_list = new ArrayList<>();
    blog_list_view = view.findViewById(R.id.blog_list_view);

    firebaseAuth = FirebaseAuth.getInstance();

    blogRecyclerAdapter = new BlogRecyclerAdapter(blog_list);
    blog_list_view.setLayoutManager(new LinearLayoutManager(container.getContext()));
    blog_list_view.setAdapter(blogRecyclerAdapter);
    blog_list_view.setHasFixedSize(true);

    if(firebaseAuth.getCurrentUser() != null) {

        firebaseFirestore = FirebaseFirestore.getInstance();

        blog_list_view.addOnScrollListener(new RecyclerView.OnScrollListener() {
            @Override
            public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
                super.onScrolled(recyclerView, dx, dy);

                Boolean reachedBottom = !recyclerView.canScrollVertically(1);

                if(reachedBottom){

                    loadMorePost();

                }

            }
        });

        Query firstQuery = firebaseFirestore.collection("Posts").orderBy("timestamp", Query.Direction.DESCENDING).limit(3);
        firstQuery.addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

                if (!documentSnapshots.isEmpty()) {

                    if (isFirstPageFirstLoad) {

                        lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1);
                        blog_list.clear();

                    }

                    for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                        if (doc.getType() == DocumentChange.Type.ADDED) {

                            String blogPostId = doc.getDocument().getId();
                            BlogPost blogPost = doc.getDocument().toObject(BlogPost.class).withId(blogPostId);

                            if (isFirstPageFirstLoad) {

                                blog_list.add(blogPost);

                            } else {

                                blog_list.add(0, blogPost);

                            }


                            blogRecyclerAdapter.notifyDataSetChanged();

                        }
                    }

                    isFirstPageFirstLoad = false;

                }

            }

        });

    }

    // Inflate the layout for this fragment
    return view;
}

public void loadMorePost(){

    if(firebaseAuth.getCurrentUser() != null) {

        Query nextQuery = firebaseFirestore.collection("Posts")
                .orderBy("timestamp", Query.Direction.DESCENDING)
                .startAfter(lastVisible)
                .limit(3);

        nextQuery.addSnapshotListener(getActivity(), new EventListener<QuerySnapshot>() {
            @Override
            public void onEvent(QuerySnapshot documentSnapshots, FirebaseFirestoreException e) {

                if (!documentSnapshots.isEmpty()) {

                    lastVisible = documentSnapshots.getDocuments().get(documentSnapshots.size() - 1);
                    for (DocumentChange doc : documentSnapshots.getDocumentChanges()) {

                        if (doc.getType() == DocumentChange.Type.ADDED) {

                            String blogPostId = doc.getDocument().getId();
                            BlogPost blogPost = doc.getDocument().toObject(BlogPost.class).withId(blogPostId);
                            blog_list.add(blogPost);

                            blogRecyclerAdapter.notifyDataSetChanged();
                        }

                    }
                }

            }
        });

    }
}

}