gpt4 book ai didi

java - Firebase 检索数组名称和带有身份验证的数组

转载 作者:行者123 更新时间:2023-12-02 11:38:22 24 4
gpt4 key购买 nike

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

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"
}
}
}
}

最佳答案

有很多事情很复杂,并且可能无法实现您想要的效果。我认为您正在寻找能够返回每个特定用户的列表映射,然后将所有列表项收集到微调器中的东西。如果是这样,请尝试这个

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);

}

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

关于java - Firebase 检索数组名称和带有身份验证的数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48755457/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com