基于Spinner的Android Google Firebase数据库查询
例如,我在数据库中有4个子项:A、B、C、D。每个子项都有一个名为“countryName”的属性。假设A有英国,B有法国,C有意大利,D有意大利。这些childer显示为列表,列表上方是微调器,有3个选项-英国、法国、意大利。默认情况下,我在列表视图中看到所有4个子项。假设我在微调器上选择英国,然后我只看到对象a。假设我在微调器上选择意大利,然后在列表视图上看到对象C和D。我只是不知道如何使用各种参数调用对数据库的查询。以下是我的微调器代码:基于Spinner的Android Google Firebase数据库查询,android,firebase,firebase-realtime-database,querying,Android,Firebase,Firebase Realtime Database,Querying,例如,我在数据库中有4个子项:A、B、C、D。每个子项都有一个名为“countryName”的属性。假设A有英国,B有法国,C有意大利,D有意大利。这些childer显示为列表,列表上方是微调器,有3个选项-英国、法国、意大利。默认情况下,我在列表视图中看到所有4个子项。假设我在微调器上选择英国,然后我只看到对象a。假设我在微调器上选择意大利,然后在列表视图上看到对象C和D。我只是不知道如何使用各种参数调用对数据库的查询。以下是我的微调器代码: spinCountry = (Spinner) f
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) {
}
});
因此,我按照弗兰克·范·帕夫伦的建议添加了一个听众,从而解决了我的问题。下面的代码在onItemSelectedmethod中
你的问题似乎很好。你在问什么?如何从该查询中实际获取数据?您需要获取查询结果。有关查询的许多问题,请参阅。它们包含了很多代码,你可以用它们来激发灵感。你能给我们看看你的LocationCurrent类的内容吗?我刚刚解决了我的问题!谢谢你们。我认为我必须走另一条路,但有更简单的解决办法。请查看我的更新帖子,了解我是如何修复的。请将您的答案作为解决方案添加到下面,而不是作为问题底部的更新,以便其他人更容易找到我的答案,谢谢!
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
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