该应用通过OnDataChange
从Firebase获取数组列表,并且OnItemSelected
,但不幸的是它不会调用。 (实际上,微调器从Firebase数据库获取字符串。)
我已经使用本地arraylist和OnItemSelected
测试了代码。
为什么呢?
这些是我的功能:
public void initSpinner() {
restartAudioSpinner();
try {
Thread.sleep(4000);
} catch (InterruptedException e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(AddAudioItems.this, android.R.layout.simple_spinner_dropdown_item, categoriesTitle);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
}
public void restartAudioSpinner() {
DatabaseReference database;
database = FirebaseDatabase.getInstance().getReference("Audio Categories");
database.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
categorylist = new ArrayList<String>();
for (DataSnapshot data :dataSnapshot.getChildren()) {
CategoryNew category = data.getValue(CategoryNew.class);
categoriesList.add(new CategoryNew(category.getImageUrl(),category.getTitle()));
}
for (int i = 0; i < categoriesList.size(); i++) {
categoriesTitle.add(categoriesList.get(i).getTitle());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
Toast.makeText(AddAudioItems.this, categoriesTitle.get(i), Toast.LENGTH_SHORT).show();
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
Toast.makeText(AddAudioItems.this, "לא נבחרה שום רשומה, אנא בחר קטגוריה", Toast.LENGTH_SHORT).show();
}
答案 0 :(得分:0)
最终我自己找到了答案。 Firebase功能同步工作。我还没有找到改变它的方法 - 以某种方式操纵它。 处理从firebase函数接收的数据的答案是在for(在数据库中收集信息)INSIDE onDataChange之后初始化所需的函数。 例如:
public void restartAudioSpinner() {
DatabaseReference database;
database = FirebaseDatabase.getInstance().getReference("Audio Categories");
database.addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
categorylist = new ArrayList<String>();
for (DataSnapshot data :dataSnapshot.getChildren()) {
CategoryNew category = data.getValue(CategoryNew.class);
categoriesList.add(new CategoryNew(category.getImageUrl(),category.getTitle()));
}
for (int i = 0; i < categoriesList.size(); i++) {
categoriesTitle.add(categoriesList.get(i).getTitle());
}
**// PUT THE DESIRED FUNCTION HERE!!!** EX: INIT THE SPINNER
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}