将通过FirestoreRecyclerAdapter获得的数据传递给意图

时间:2018-06-28 09:55:19

标签: android firebase google-cloud-firestore firebaseui

我正在使用 FirebaseUI for Firestore 在片段中使用 RecyclerView和CardView组合显示项目。

我能够成功地查询结果,设置适配器选项并启动适配器本身,从以下提取的简单代码中可以明显看出。

//Setting up to query the firestore
    Query query = FirebaseFirestore.getInstance()
            .collection("records")
            .whereEqualTo("userId", FirebaseAuth.getInstance().getCurrentUser().getUid())
            .orderBy("timestamp", Query.Direction.DESCENDING);


    //setting up the recycleradapter options
    FirestoreRecyclerOptions<RecordsModel> options = new FirestoreRecyclerOptions.Builder<RecordsModel>()
            .setQuery(query, RecordsModel.class)
            .build();

    //using firebase ui firestore adapter, therefore eliminating the need for  our own custom adapter
    FirestoreRecyclerAdapter adapter = new FirestoreRecyclerAdapter<RecordsModel, recordViewHolder>(options) {
        @Override
        public void onDataChanged() {
            progressBar.setVisibility(View.GONE);

        }

        @Override
        public void onError(@NonNull FirebaseFirestoreException e) {
            Log.e(TAG, "onError: ", e);
        }

    //binds the data source on the various elements of the card view that is inflated using onCreateViewHolder below
        @Override
        protected void onBindViewHolder(@NonNull recordViewHolder holder, int position, @NonNull RecordsModel record) {


            holder.date.setText(String.valueOf(record.getFullDate()));
            holder.qty.setText(String.valueOf(record.getQty()));
            holder.rate.setText(String.valueOf(record.getRate()));
            holder.total.setText(String.valueOf(record.getTotal()));
            holder.milkType.setText(String.valueOf(record.getMilkType()));

        }

        //creates the viewholder for the recyclerview that is the cardview that i've designed
        @NonNull
        @Override
        public recordViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            LayoutInflater inflater = LayoutInflater.from(getContext());
            View view = inflater.inflate(R.layout.record_logs_card, parent, false);
            return new recordViewHolder(view);
        }


    };

   //initiating the recyclerview, setting the layout manager and the firestore adapter
    recyclerView = view.findViewById(R.id.recyclerview);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity().getApplicationContext()));
    recyclerView.setAdapter(adapter);
    adapter.startListening();


    return view;

    }

}

为了允许用户更新卡中显示的特定记录,我正在尝试

  1. 在RecyclerView.ViewHolder上实现View.OnClickListener
  2. 通过意图将点击的卡片上的数据传递到另一个活动,即updateRecord.java

以下代码演示了第一步,部分演示了步骤2。

//locates and initiates the elements of the card view as well as enables the clicking of the card
public class recordViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
    TextView date, qty, rate, total, milkType;
    Context mCtx = getActivity().getApplicationContext();

    public recordViewHolder(View itemView) {
        super(itemView);
        date = itemView.findViewById(R.id.date);
        qty = itemView.findViewById(R.id.qty);
        rate = itemView.findViewById(R.id.rate);
        total = itemView.findViewById(R.id.total);
        milkType = itemView.findViewById(R.id.milkType);

    }

    //what happens if a card is clicked
    @Override
    public void onClick(View view) {
        Intent intent = new Intent(mCtx, updateRecord.class);
        intent.putExtra("record",... )
        mCtx.StartActivity(intent);
    }
}

实际上如何在 FirestoreRecyclerOptions 创建的单击位置检索ArrayList,以便将其传递给意图?

假定模型类(RecordsModel.java)实现了Serializable。

谢谢!

0 个答案:

没有答案