gpt4 book ai didi

java - 在 Android Studio Java 中读取数据库标题并显示在 ListView 中

转载 作者:行者123 更新时间:2023-12-02 01:25:26 26 4
gpt4 key购买 nike

我想读取数据库子项的标题并将其以 ListView 的形式显示在数据库中,这怎么可能?这是我尝试过的代码,但它不起作用:/我想在蓝色圆圈中显示标题,任何人都可以帮忙吗,因为我需要尽快完成此操作?提前致谢<3 enter image description here

 Bundle bundle = getIntent().getExtras();
if (bundle != null) {
selectedCity = bundle.getInt("city", 0);
selectedCategory = bundle.getInt("category", 0);
}

dataListView = findViewById(R.id.dataListView);
database = FirebaseDatabase.getInstance();
String _city = "";
switch (selectedCity){
case 1: _city = "Alexandria";
break;
case 2: _city = "Aswan";
break;
case 3: _city = "Cairo";
break;
case 4: _city = "Fayyoum";
break;
case 5: _city = "Giza";
break;
case 6: _city = "Hurghada";
break;
case 7: _city = "Luxor";
break;
case 8: _city = "Marsa Matruh";
break;
case 9: _city = "North Coast";
break;
case 10: _city = "Sharm El-Sheikh";
break;
case 11: _city = "Sharqia";
break;
case 12: _city = "Siwa";
break;
case 13: _city = "Taba";
break;


}
databaseReference = database.getReference("Places/"+_city);








adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_single_choice,
cityItems);
dataListView.setAdapter(adapter);
databaseReference.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
String value = dataSnapshot.getValue(String.class);
cityItems.add(value);
adapter.notifyDataSetChanged();
}

@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

}

@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

}

@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

}

@Override
public void onCancelled(@NonNull DatabaseError databaseError) {

}
});

什么也没有显示

最佳答案

尝试以下操作:

DatabaseReference ref = FirebaseDatabase.getInstance().getReference("Places");

ref.addValueEventListener(new ValueEventListener(){

@Override
public void onDataChange(DataSnapshot dataSnapshot){
for(DataSnapshot ds : dataSnapshot.getChildren()){
String key = ds.getKey();
}
}

首先添加对节点 Places 的引用,然后在直接子节点中循环,您将能够检索 key 。

关于java - 在 Android Studio Java 中读取数据库标题并显示在 ListView 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57021309/

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