簡體   English   中英

Firestore 回收器適配器未獲取文檔名稱

[英]Firestore recycler adapter not fetching document names

我正在嘗試使用FirestoreRecyclerAdapter獲取所有文檔,如果RecyclerView項目成功填充了 7 個項目的 7 個文檔,但這里的問題是具有文本視圖的項目沒有填充文檔名稱。 請看一下我的源代碼:

FriendsResponse 類:

@IgnoreExtraProperties
public class FriendsResponse {
     FirebaseFirestore db;
    public String getTable1() {
        return Table1;
    }

    public void setTable1(String table1) {
        Table1 = table1;
    }

    private String Table1;


    public FriendsResponse() {

    }

    public FriendsResponse(String Table1) {
        this.Table1 = Table1;
    }


}

初始化 recyclerview 的 TableList 片段:

public class TableListFragment extends Fragment{

    private FirebaseFirestore db;
    private FirestoreRecyclerAdapter adapter;
    String documentnm;
    RecyclerView recyclerView;
    FloatingActionButton addt;
    private StaggeredGridLayoutManager _sGridLayoutManager;

    public static TableListFragment newInstance() {
        TableListFragment fragment = new TableListFragment();
        return fragment;
    }
    public TableListFragment() {

    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_tablelist, container, false);
        recyclerView = view.findViewById(R.id.rectab);
        addt=view.findViewById(R.id.addtab);
        init();
        getFriendList();
        addt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
        return view;
    }
    private void init(){
        _sGridLayoutManager = new StaggeredGridLayoutManager(3,
                StaggeredGridLayoutManager.VERTICAL);
        recyclerView.setLayoutManager(_sGridLayoutManager);
        db = FirebaseFirestore.getInstance();
    }

    private void getFriendList(){
        Query query = db.collection("Order");

        FirestoreRecyclerOptions<FriendsResponse> response = new FirestoreRecyclerOptions.Builder<FriendsResponse>()
                .setQuery(query, FriendsResponse.class)
                .build();

        adapter = new FirestoreRecyclerAdapter<FriendsResponse, FriendsHolder>(response) {
            @Override
            public void onBindViewHolder(FriendsHolder holder, int position, FriendsResponse model) {


                holder.exname.setText(model.getTable1());


                holder.itemView.setOnClickListener(v -> {
                    Snackbar.make(recyclerView, model.getTable1(), Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                });
            }

            @Override
            public FriendsHolder onCreateViewHolder(ViewGroup group, int i) {
                View view = LayoutInflater.from(group.getContext())
                        .inflate(R.layout.list_item, group, false);

                return new FriendsHolder(view);
            }

            @Override
            public void onError(FirebaseFirestoreException e) {
                Log.e("error", e.getMessage());
            }
        };

        adapter.notifyDataSetChanged();
        recyclerView.setAdapter(adapter);
    }
    public class FriendsHolder extends RecyclerView.ViewHolder {

        TextView exname;


        public FriendsHolder(View itemView) {
            super(itemView);
            exname= itemView.findViewById(R.id.topicname);

        }
    }

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

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



    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

}

屏幕截圖:

這是 list_item 的代碼:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView android:id="@+id/cardvw"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    card_view:cardCornerRadius="6dp"
    card_view:cardElevation="3dp"
    card_view:cardUseCompatPadding="true"
    xmlns:android="http://schemas.android.com/apk/res/android" xmlns:card_view="http://schemas.android.com/apk/res-auto">
    <LinearLayout android:orientation="vertical" android:padding="5dp" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp">
        <de.hdodenhof.circleimageview.CircleImageView
            android:id="@+id/topiclogo"
            android:layout_width="match_parent"
            android:layout_gravity="center"
            android:src="@drawable/table"
            android:layout_height="wrap_content"
            />
        <TextView android:textSize="15sp"
            android:textStyle="bold"
            android:textAlignment="center"
            android:textColor="#ffffa200"
            android:id="@+id/topicname"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />
    </LinearLayout>
</android.support.v7.widget.CardView>

數據庫截圖

據我了解,您想將文檔的 id 設置為該TextView 因此,因為這些名稱實際上是文檔 ID,您應該在onBindViewHolder()方法中使用以下代碼行:

String id = getSnapshots().getSnapshot(position).getId();
holder.exname.setText(id);

您正在使用的 POJO 類在獲取文檔的屬性時很有用,而不是獲取文檔 ID。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM