在RecycleView中更新Firestore字段

时间:2019-05-03 20:42:14

标签: java android firebase android-recyclerview google-cloud-firestore

我想在发生OnClick时更新Firestore中的字段。 问题是我不知道如何检索我实际物品的ID,因为它是自动生成的。 (我想更新 assignedTo 字段)。 Firestore的结构如下:enter image description here

带有我的RecyclerView适配器的代码段是:

public class AvailableTaskListAdapter extends RecyclerView.Adapter<AvailableTaskListAdapter.ViewHolder> {
public List<Task> taskList;
Activity mActivity;
FirebaseFirestore firestore;

public AvailableTaskListAdapter(List<Task> taskList, Context mContext, FirebaseFirestore firestore) {
    this.taskList = taskList;
    this.mActivity = (Activity) mContext;
    this.firestore = firestore;
}

@NonNull
@Override
public AvailableTaskListAdapter.ViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.layout_available_tasks_item, viewGroup, false);
    view.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            //HERE I want to be happening the update of my field.


        }
    });
    return new AvailableTaskListAdapter.ViewHolder(view);

}

@Override
public void onBindViewHolder(@NonNull AvailableTaskListAdapter.ViewHolder viewHolder, int i) {
    viewHolder.tvEmail.setText(taskList.get(i).getUser());
    ...
}

@Override
public int getItemCount() {
    return taskList.size();
}

public class ViewHolder extends RecyclerView.ViewHolder {
    View mView;

    public TextView tvEmail;
    ...

    public ViewHolder(View itemView) {
        super(itemView);
        mView = itemView;

        tvEmail = (TextView) mView.findViewById(R.id.tvEmail);
        ...
    }
}

public void showDialog(final Activity activity) {
    final Dialog dialog = new Dialog(activity);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(false);
    dialog.setContentView(R.layout.dialog_apply_to_task);
    dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));

    FrameLayout mDialogNo = dialog.findViewById(R.id.frmNo);
    mDialogNo.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Toast.makeText(activity.getApplicationContext(),"Cancel" ,Toast.LENGTH_SHORT).show();
            dialog.dismiss();
        }
    });

    FrameLayout mDialogOk = dialog.findViewById(R.id.frmOk);
    mDialogOk.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(activity.getApplicationContext(),"Okay" ,Toast.LENGTH_SHORT).show();
            dialog.cancel();
        }
    });

    dialog.show();
}}

2 个答案:

答案 0 :(得分:0)

只需将文档的文档ID存储在Task对象中。向任务对象添加一个新字段,例如“ taskId”,当您从firebase中检索任务时,请使用document.id 例如:

db.collection("cities")
    .whereEqualTo("capital", true)
    .get()
    .addOnSuccessListener { documents ->
        for (document in documents) {
            Log.d(TAG, "${document.id} => ${document.data}")
        }
    }

答案 1 :(得分:0)

正如我在您的代码中所看到的那样,您将获得Task对象的列表,但没有获得每个文档的键。要解决此问题,您需要同时跟踪适配器中文档的键和值。建议的方法是保留两个列表,一个包含键,一个包含值。

因此,首先为适配器类中的键添加List<String>,如下所示:

private List<Task> taskList;
private List<String> keys;

然后将您的构造函数更改为接受两个列表:

public AvailableTaskListAdapter(List<Task> taskList, List<String> keys, Context mContext, FirebaseFirestore firestore) {
//                                                                ^^^^
    this.taskList = taskList;
    this.keys = keys; //Intialize the the second list
    this.mActivity = (Activity) mContext;
    this.firestore = firestore;
}

然后,当您创建AvailableTaskListAdapter类的新对象时,除了taskList之外,还传递keys列表。

AvailableTaskListAdapter adapter = new AvailableTaskListAdapter(taskList, keys, mContext, firestore);

基本上,键列表是按照与创建值列表相同的方式创建的。因此,当您遍历整个集合以创建Task对象的列表时,还需要获得如下ID:

FirebaseFirestore rootRef = FirebaseFirestore.getInstance();
CollectionReference tasksRef = rootRef.collection("tasks");
tasksRef.get().addOnCompleteListener(new OnCompleteListener<QuerySnapshot>() {
    @Override
    public void onComplete(@NonNull Task<QuerySnapshot> task) {
        if (task.isSuccessful()) {
            List<Task> taskList = new ArrayList<>();
            List<String> keys = new ArrayList<>();
            for (QueryDocumentSnapshot document : task.getResult()) {
                Task t = document.toObject(Task.class);
                taskList.add(t);
                String key = document.getId();
                keys.add(key);
            }

            //Create/notify the adapter
        }
    }
});