在Fragment上使用RecyclcerView来显示来自Firestore的数据,我是否需要每个片段的适配器?

时间:2019-02-16 16:55:17

标签: android android-fragments android-recyclerview google-cloud-firestore

我在显示数据上使用Firestore到Fragment上的RecyclcerView。我的项目有两个不同的集合,每个片段将在每个片段上显示不同的数据。在这个项目上,我为每个RecyclcerView创建了两个适配器,为每个片段创建了一个适配器。但是,当我运行Project时,片段不会显示任何内容。我是否只需要对两个片段都使用适配器,或者我在做什么错了?

我正在使用TabLayout

我是编程新手。

那是我的代码,两个片段都有相似的代码,唯一的区别是集合Ref和数据:

片段代码:

public class PedidoFragmento extends Fragment {

    View v;
    private FirebaseFirestore db = FirebaseFirestore.getInstance();
    private CollectionReference notebookRefP = db.collection("NotebooRefP");

    private PedidoAdapter adapter;
    private RecyclerView recyclerView;


    public PedidoFragmento() {
    }


    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        v = inflater.inflate(R.layout.pedido_fragmento, container, false);

        recyclerView = v.findViewById(R.id.pedido_recyclerview);

        setUpRecyclerView();

        return v;
    }

    private void setUpRecyclerView() {


        Query query = notebookRefP.orderBy("pedido", Query.Direction.DESCENDING);

        FirestoreRecyclerOptions<Pedido> options = new FirestoreRecyclerOptions.Builder<Pedido>()
                .setQuery(query, Pedido.class)
                .build();

        adapter = new PedidoAdapter(options);
        recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
        recyclerView.setAdapter(adapter);


    }

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

    }

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

        }
    }
}

XML片段

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
    android:layout_height="match_parent">

    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/pedido_recyclerview"
        android:layout_marginBottom="20dp">

    </android.support.v7.widget.RecyclerView>

</android.support.design.widget.CoordinatorLayout>

适配器

public class PedidoAdapter extends FirestoreRecyclerAdapter<Pedido, PedidoAdapter.PedidoHolder> {


    public PedidoAdapter(@NonNull FirestoreRecyclerOptions<Pedido> options) {
        super(options);
    }

    @Override
    protected void onBindViewHolder(@NonNull PedidoHolder holder, int position, @NonNull Pedido model) {
    holder.textViewPedido.setText(model.getPedido());
    holder.textViewNomePedido.setText(model.getNome());

    }

    @NonNull
    @Override
    public PedidoHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
       View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.pedido_item,
               parent,false);
        return new PedidoHolder(v);
    }

    public class PedidoHolder extends RecyclerView.ViewHolder{
        TextView textViewPedido;
        TextView textViewNomePedido;


        public PedidoHolder(@NonNull View itemView) {
            super(itemView);
            textViewPedido = itemView.findViewById(R.id.txt_pedidoview);
            textViewNomePedido= itemView.findViewById(R.id.tx_nomepediview);



        }
    }
}

班级

public class Pedido {
    private String pedido;
    private String nome;

    public Pedido (){

    }

    public Pedido(String pedido, String nome) {
        this.pedido = pedido;
        this.nome= nome;

    }

    //getter

    public String getPedido() {
        return pedido;
    }

    public String getNome() {
        return nome;
    }

以某种方式未分配类“(Public Pedido(){})”)的第7行,并显示“从未使用过构造函数”。

这是片段中的Page适配器

public class PageAdapter extends FragmentPagerAdapter {

    private final List<Fragment> listfragment = new ArrayList<>();
    private final List<String> listTitle = new ArrayList<>();



    public PageAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        return listfragment.get(position);
    }

    @Override
    public int getCount() {
        return listTitle.size();
    }

    @Override
    public CharSequence getPageTitle(int position) {
        return listTitle.get(position);
    }


    public void AddFragment (Fragment fragment, String title ) {

        listfragment.add(fragment);
        listTitle.add(title);

    }




}

EDIT数据库结构 在进行测试时,我尝试了这两种方法,但仍然是空的

Database Strucutre Database Strucutre

0 个答案:

没有答案