如何在android中的firebase数据库中获取具有选定值的节点的密钥UID?

时间:2017-08-18 14:44:50

标签: java android firebase firebase-realtime-database

以下是我在firebase中的数据库

Areas 
   -AreaUID1 
      -Admin 
         -Admin1UID 
              -admin_name:"" 
              -admin_area:"" 
              -admin_email:"" 
         -Admin2UID 
              -admin_name:"" 
              -admin_area:""
              -admin_email:""
              .
              .
      -Events
          -EventUid1
                -event_name:""
                -event_timings:""
         -EventUid2
                -event_name:""
                -event_timings:""
            .
            .

      -Gallery
          -ImageUid1
                -image_link:""

  -AreaUid2
       -Admin 
         -Admin1UID 
              -admin_name:"" 
              -admin_area:"" 
              -admin_email:"" 
         -Admin2UID 
              -admin_name:"" 
              -admin_area:""
              -admin_email:""
              .
              .
      -Events
          -EventUid1
                -event_name:""
                -event_timings:""
         -EventUid2
                -event_name:""
                -event_timings:""
            .
            .

      -Gallery
          -ImageUid1
                -image_link:""

我想要实现的目标?

我想获得区域的AreaUID,考虑到在同一数据库的帮助下填充了一个admin_areas列表。现在,当用户点击列表中的项目时,我想获得其admin_area与列表中所选admin_area匹配的节点的AreaUID。

例如) 以下是向用户显示的区域列表 -

1)区域1

2)区域2

3)区域3

4)区域4

当用户点击区域2时,我想要获取其AreaUid。我是如何做到的?以下是一些供参考的代码

mDbRef = FirebaseDatabase.getInstance().getReference().child("Areas");
        arr = new ArrayList<>();
        listView = (ListView) findViewById(R.id.list_of_areas);
        ValueEventListener listener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
                    String areaUid = snapshot.getKey();
                    Log.i("AreaUID", areaUid);
                    showListOfAreas(areaUid);
                }
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {
                System.out.println(databaseError.toException());
            }
        };
        mDbRef.addValueEventListener(listener);


    }

    private void showListOfAreas(final String areaUid) {

        pd.setTitle("Fetching Registered Areas");
        pd.setMessage("Loading Areas...");
        pd.show();
        DatabaseReference mAreaNameRef = mDbRef.getRef().child(areaUid).child("Admin").child("admin1");

        mAreaNameRef.addValueEventListener(new ValueEventListener() {
            @Override

            public void onDataChange(DataSnapshot dataSnapshot) {

                final Admin admin = dataSnapshot.getValue(Admin.class);
                arr.add(admin.getAdmin_colony());
                adapter = new ArrayAdapter<>(ListOfAreasActivity.this,
                        android.R.layout.simple_list_item_1, android.R.id.text1, arr);
                listView.setAdapter(adapter);
                pd.dismiss();
                listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

                        String areaName = (String) parent.getItemAtPosition(position);
                        //findUserIdWithLocation(areaName);

                        SharedPreferences.Editor editor = getSharedPreferences("shared_prefs", MODE_PRIVATE).edit();
                        editor.putString("area_name", areaName);
                        editor.apply();
                        editor.commit();
                        Intent intent = new Intent(ListOfAreasActivity.this, MainActivity.class);
                        startActivity(intent);
                        finish();
                    }
                });

            }


            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });


    }

1 个答案:

答案 0 :(得分:0)

根据您的评论,可以从areaUid参数中检索showListOfAreas

private void showListOfAreas(final String areaUid) {

    ...

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

            // area's UID
            String areaUidAfterClick = areaUid;

            SharedPreferences.Editor editor = getSharedPreferences("shared_prefs", MODE_PRIVATE).edit();
            editor.putString("area_name", areaName);
            editor.apply();
            editor.commit();
            Intent intent = new Intent(ListOfAreasActivity.this, MainActivity.class);
            startActivity(intent);
            finish();
        }
    });

    ...

}

如果这不是您的意思,请告诉我。