我想从firebase检索数据以显示在ListView中,但是当我运行该应用程序时,ListView不显示任何数据。我看到了ListView,但是没有数据。你能告诉我为什么,我该怎么办?当我尝试使用Spinner时,我遇到了同样的问题。
public class CitiesList extends AppCompatActivity {
DatabaseReference databasePaths;
private ListView ListViewCities;
private ArrayList<String> ArrayListCities;
private ArrayAdapter<String> adapterCities;
Paths info;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_cities_list);
ListViewCities = (ListView) findViewById(R.id.ListViewCities);
ArrayListCities = new ArrayList<>();
databasePaths = FirebaseDatabase.getInstance().getReference("Paths");
databasePaths.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot items: dataSnapshot.getChildren())
{
info = items.getValue(Paths.class);
ArrayListCities.add(info.city);
}
adapterCities = new ArrayAdapter<>(CitiesList.this, android.R.layout.simple_list_item_1);
ListViewCities.setAdapter(adapterCities);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}}
答案 0 :(得分:0)
您需要为适配器设置新列表,而当前代码中没有这样做。修改下面的行
adapterCities = new ArrayAdapter<>(CitiesList.this, android.R.layout.simple_list_item_1);
ListViewCities.setAdapter(adapterCities);
为
adapterCities = new ArrayAdapter<>(CitiesList.this, android.R.layout.simple_list_item_1, ArrayListCities);
ListViewCities.setAdapter(adapterCities);
作为旁注,请尝试遵循编码样式准则。
答案 1 :(得分:0)
您需要通过适配器将其设置数据的地方传递给TextView。
因此,在R.layout.XXXX之后,您需要通过: android.R.id.text1