Android Google 在 Spinner 上查询 Firebase 数据库
Android Google Firebase Database Querying on Spinner
例如我在database:A、B、C、D 中有4 个children。它们每个都有一个名为'countryName' 的属性。假设A有英国,B有法国,C有意大利,D有意大利。这些孩子显示为列表,列表上方是带有 3 个选项的微调器 - 英国、法国、意大利。默认情况下,我在列表视图中看到所有 4 children。假设我在 Spinner 上选择英国,然后我只看到 object A。假设我在 Spinner 上选择意大利,然后我在列表视图上看到 objects C 和 D。我只是不知道如何使用各种参数调用对数据库的查询。这是我的微调器代码:
spinCountry = (Spinner) findViewById(R.id.spinCountry);
ArrayAdapter<CharSequence> spinAdapter = ArrayAdapter.createFromResource(this,
R.array.country_array, android.R.layout.simple_spinner_item);
spinAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinCountry.setAdapter(spinAdapter);
spinCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Get whatever is chosen on a spinner
String spinnerCountry = parent.getItemAtPosition(position).toString();
//Here is my query which on SQL would be 'SELECT * FROM THE DATABASE WHERE countryName EQUALS TO'country on a chosen on spinner'
Query querySpinner = mLocationDatabaseReference.orderByChild("countryName").equalTo(spinnerCountry);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
[修复]
因此,我按照 Frank van Puffelen 的建议添加了一个侦听器,设法解决了我的问题。
mLocationAdapter.clear();
querySpinner.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
locationCurrent = dataSnapshot.getValue(LocationCurrent.class);
mLocationAdapter.add(locationCurrent);
}...
//Everything else is empty on addChildEventListener method so no point pasting it here
因此,我按照 Frank van Puffelen 的建议添加了一个侦听器,设法解决了我的问题。下面的代码在 onItemSelected
method
里面
mLocationAdapter.clear();
querySpinner.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
locationCurrent = dataSnapshot.getValue(LocationCurrent.class);
mLocationAdapter.add(locationCurrent);
}...
//Everything else is empty on addChildEventListener method so no point pasting it here
例如我在database:A、B、C、D 中有4 个children。它们每个都有一个名为'countryName' 的属性。假设A有英国,B有法国,C有意大利,D有意大利。这些孩子显示为列表,列表上方是带有 3 个选项的微调器 - 英国、法国、意大利。默认情况下,我在列表视图中看到所有 4 children。假设我在 Spinner 上选择英国,然后我只看到 object A。假设我在 Spinner 上选择意大利,然后我在列表视图上看到 objects C 和 D。我只是不知道如何使用各种参数调用对数据库的查询。这是我的微调器代码:
spinCountry = (Spinner) findViewById(R.id.spinCountry);
ArrayAdapter<CharSequence> spinAdapter = ArrayAdapter.createFromResource(this,
R.array.country_array, android.R.layout.simple_spinner_item);
spinAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinCountry.setAdapter(spinAdapter);
spinCountry.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//Get whatever is chosen on a spinner
String spinnerCountry = parent.getItemAtPosition(position).toString();
//Here is my query which on SQL would be 'SELECT * FROM THE DATABASE WHERE countryName EQUALS TO'country on a chosen on spinner'
Query querySpinner = mLocationDatabaseReference.orderByChild("countryName").equalTo(spinnerCountry);
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
[修复]
因此,我按照 Frank van Puffelen 的建议添加了一个侦听器,设法解决了我的问题。
mLocationAdapter.clear();
querySpinner.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
locationCurrent = dataSnapshot.getValue(LocationCurrent.class);
mLocationAdapter.add(locationCurrent);
}...
//Everything else is empty on addChildEventListener method so no point pasting it here
因此,我按照 Frank van Puffelen 的建议添加了一个侦听器,设法解决了我的问题。下面的代码在 onItemSelected
method
mLocationAdapter.clear();
querySpinner.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
locationCurrent = dataSnapshot.getValue(LocationCurrent.class);
mLocationAdapter.add(locationCurrent);
}...
//Everything else is empty on addChildEventListener method so no point pasting it here