Firebase检索包含身份验证

时间:2018-02-12 21:02:19

标签: java android json firebase firebase-realtime-database

如何实现基于帐户的数组的检索,目前当我运行以下代码时,它不会检索任何内容。我是一个完全的初学者,我试着看其他例子,但我和其他人不同。我的目标是检索属于该用户的所有数组。现在我只想返回这些项目的名称,但后来我也想使用该数组,所以第一个用户会有:

Fruit = ["apple", "banana", "mango" ]
Veg = ["tomato", "potato", "cabbage" ]

代码:

public class HomeActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{

private ZXingScannerView scannerView;
private final int permission_code = 1;

String [] selectedProfile;
Spinner spinner;
ArrayAdapter<CharSequence> adapter;

ListView lvProfiles;


private String userID;
private FirebaseDatabase mFirebaseDatabase;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener mAuthListener;
private DatabaseReference myRef;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home);

    mAuth = FirebaseAuth.getInstance();
    mFirebaseDatabase = FirebaseDatabase.getInstance();
    myRef = mFirebaseDatabase.getReference();
    FirebaseUser user = mAuth.getCurrentUser();
    userID = user.getUid();

    if(mAuth.getCurrentUser() == null){
        finish();
        startActivity(new Intent(this, MainActivity.class));
    }

    startSpinner();

    lvProfiles = (ListView)findViewById(R.id.lvProfiles);

    myRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            // This method is called once with the initial value and again
            // whenever data at this location is updated.
            showData(dataSnapshot);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

private void showData(DataSnapshot dataSnapshot) {
    for(DataSnapshot ds : dataSnapshot.getChildren()){
        String x = ds.child("users").child(userID).getValue(String.class);

        ArrayList<String> array  = new ArrayList<>();
        array.add(x);
        ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
        lvProfiles.setAdapter(adapter);
    }
}

我的数据库看起来像这样:

{
  "users" : {

"9kV3otdvOXU0CGMZWIIxggrBrj63" : {

  "Fruit" : [ "apple", "banana", "mango" ],
  "Veg" : [ "tomato", "potato", "cabbage" ]
},
"FKazRnUjrzMuQs6dbeIIGaGju5W2" : {

  "Gluten" : [ "123", "456", "789" ]
},
"xaLZIfNudthJ8BuY6WBqYrrYPsA2" : {

  "Berries" : [ "strawberry", "raspberry", "blackberry", "blueberry", "gooseberry" ],
  "Cheese" : [ "feta", "mozzarella", "cheddar" ],
  "Kiwi" : [ "kiwi" ]
}
  }
}

更新: 我改变了你的建议,但“水果”和“蔬菜”没有出现在旋转器中,你能提出什么建议吗?这是新代码。

private void showData(DataSnapshot dataSnapshot) {

    Map<String,Object> map = dataSnapshot.child("users").child(userID).getValue(Map.class);
    ArrayList<String> array  = new ArrayList<>();
    for (Map.Entry<String,Object> entry : map.entrySet()) {

        // key contains "Fruit" or "Veg"
        String key = entry.getKey();
        // value is the corresponding list
        Object value = entry.getValue();
        array.addAll((ArrayList) value);

    }
    ArrayAdapter adapterUser = new ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
    spinnerUser.setAdapter(adapterUser);

}

规则:

{
  "rules": {
    "users": {
      "$uid": {
        ".read": "$uid === auth.uid",
        ".write": "$uid === auth.uid"
      }
    }
  }
}

1 个答案:

答案 0 :(得分:1)

有许多事情令人费解,可能无法完成你想要的事情。我认为您正在寻找能够返回特定用户的列表地图的内容,然后将所有列表项收集到微调器中。如果是这样,试试这个

private void showData(DataSnapshot dataSnapshot) {

   Map<String,Object> map = (Map) dataSnapshot.getValue().child("users").child(userID);
   ArrayList<String> array  = new ArrayList<>();
   for (Map.Entry<String, Object> entry : map.entrySet()) {

      // key contains "Fruit" or "Veg"
      String key = entry.getKey();
      // value is the corresponding list
      Object value = entry.getValue();
      array.addAll((ArrayList) value);

   }
   ArrayAdapter adapter = new 
       ArrayAdapter(this,android.R.layout.simple_list_item_1,array);
   lvProfiles.setAdapter(adapter);

}

我无法对此进行测试,请尝试一下,让我知道发生了什么错误,我会相应地编辑答案。