如何从Firebase检索itemclickviewlistener上的特定文档ID

时间:2020-02-11 09:05:05

标签: android google-cloud-firestore

fgboys.whereEqualTo("date", date)
                    .get()
                    .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                        @Override
                        public void onSuccess(final QuerySnapshot queryDocumentSnapshots) {

                             String data="";


                            if(tvDate.getText().toString().isEmpty()){
                                Toast.makeText(ProgramToProgram.this, "Please select date", Toast.LENGTH_SHORT).show();
                            }

                            else{

                                for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                                    PID = documentSnapshot.getId().toString().trim();
                                    String temp = documentSnapshot.get("session").toString();



                                    data = "\nPID: "+ PID +"\n" + "session: "+ temp+"\n";

                                    arrayList.add(data);
                                    arrayAdapter.notifyDataSetChanged();


                                }

我想检索我单击的那个特定文档的文档ID

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


        fgboys.get()
            .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                @Override
                public void onSuccess(QuerySnapshot queryDocumentSnapshots) {


                    for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                        pid = documentSnapshot.getId().toString().trim();
                    }
 Toast.makeText(ProgramToProgram.this, " pid:" + pid, Toast.LENGTH_SHORT).show();

                }
            });
    }
});

这是我尝试过的方法,但是在单击任何项​​目时,它仅返回一个特定的ID

所以我的数据库结构是:集合-ProgramDetails,在这个集合下,我有很多具有自动生成的ID的文档,而这个ID是我想要的

fgboys是我称为集合ProgramDetails的地方

1 个答案:

答案 0 :(得分:0)

每次单击项目时,您仅查询一个数据库位置( fgboys ),for循环将覆盖变量 pid 的加号,因此每次获得ID为列表中的最后一个文档。

您可以将documentId列表存储在单独的列表中,并检索单击的列表项的ID。

 private List<String> docIds=new ArrayList<>(); //declare id list
        fgboys.whereEqualTo("date", date)
                .get()
                .addOnSuccessListener(new OnSuccessListener<QuerySnapshot>() {
                    @Override
                    public void onSuccess(final QuerySnapshot queryDocumentSnapshots) {




                        if(tvDate.getText().toString().isEmpty()){
                            Toast.makeText(ProgramToProgram.this, "Please select date", Toast.LENGTH_SHORT).show();
                        }

                        else{
                            docIds.clear();
                            for (QueryDocumentSnapshot documentSnapshot : queryDocumentSnapshots) {
                                String data="";
                                PID = documentSnapshot.getId().toString().trim();
                                docIds.add(PID);
                                String temp = documentSnapshot.get("session").toString();



                                data = "\nPID: "+ PID +"\n" + "session: "+ temp+"\n";

                                arrayList.add(data);
                                arrayAdapter.notifyDataSetChanged();


                            }

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {


       pid=docIds.get(position);
 Toast.makeText(ProgramToProgram.this, " pid:" + pid, Toast.LENGTH_SHORT).show();

    }
});