如何访问(修改、添加元素)到java中的内部class中的ArrayList/Collection?
How to access (modify, add elements) to an ArrayList/Collection in an Inner class in java?
我有这个搜索功能,可以使用 GeoFire 使用 firebase 数据库搜索特定用户。
我想做什么:
在 GeoQueryEventListener()
和 onKeyEntered
中,我想将找到的每个键添加到 ArrayList(在 onKeyEntered()
之外)。
我遇到的问题:
当我在 onKeyEntered()
中打印它时,它起作用了。
但是一旦我退出 GeoQueryEventListener()
,keysGeolocated
就是空的
public ArrayList<User> search(GeoFire geoFire, int day, final boolean isBartender, double latitude, double longitude, double radius, double rate, double nightlyRate, double hourlyRate, final String speciality){
keysGeolocated = new ArrayList<>();
FirebaseDatabase database = FirebaseDatabase.getInstance();
root = database.getReference();
DatabaseReference usersRef = root.child("Users");
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(latitude, longitude), radius);
geoQuery.addGeoQueryEventListener(new MyGeoListener(keysGeolocated));
System.out.println("KEYS GEOLOCATED" + keysGeolocated);
final ArrayList<User> result = new ArrayList<>();
for(String key : keysGeolocated){
Query queryByLocation = usersRef.orderByKey().equalTo(key);
DatabaseReference refToLocationQuery = queryByLocation.getRef();
if(checkRefNull(refToLocationQuery) == true) {
System.out.println("QUERY BY LOCATION NULL");
continue;
}
Query queryByTypeOfUser = refToLocationQuery.orderByChild("isBarTender").equalTo(isBartender);
DatabaseReference refToTypeOfUserQuery = queryByTypeOfUser.getRef();
if(checkRefNull(refToTypeOfUserQuery) == true) {
System.out.println("QUERY BY TYPE NULL");
continue;
}
Query queryByRate = refToTypeOfUserQuery.orderByChild("rate").startAt(rate);
DatabaseReference refToRateQuery = queryByRate.getRef();
if(checkRefNull(refToRateQuery) == true) {
System.out.println("QUERY BY RATE NULL");
continue;
}
DatabaseReference finalRef = null;
if(isBartender == true){
if(nightlyRate == 0){
Query queryByPrice = refToRateQuery.orderByChild("nightlyRate").endAt(nightlyRate);
finalRef = queryByPrice.getRef();
}
else if(hourlyRate == 0){
Query queryByPrice = refToRateQuery.orderByChild("hourlyRate").endAt(hourlyRate);
finalRef = queryByPrice.getRef();
}
//TODO Impelement checks for available dates
}
else{
finalRef = refToRateQuery;
}
finalRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getValue()!=null){
User userFound = dataSnapshot.getValue(User.class);
if(isBartender == true){
Bartender barTenderFound = (Bartender) userFound;
if(speciality != null){
if(barTenderFound.getSpeciality().contains(speciality)){
result.add(barTenderFound);
}
}
else{
result.add(barTenderFound);
}
}
else{
result.add(userFound);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
return result;
}
public boolean checkRefNull(DatabaseReference ref){
final boolean[] result = new boolean[1];
result[0] = false;
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("TEST1");
if(dataSnapshot.getValue()==null){
result[0] = true;
System.out.println("TESTTEST");
}
else{
System.out.println("TESTTEST TRUE" + dataSnapshot.getValue());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return result[0];
}
public void setBarTender(boolean barTender){
this.barTender = barTender;
}
}
class MyGeoListener implements GeoQueryEventListener {
private ArrayList<String> keysGeolocated;
MyGeoListener(ArrayList<String> keysGeolocated){
this.keysGeolocated = keysGeolocated;
}
@Override
public void onKeyEntered(String key, GeoLocation location) {
keysGeolocated.add(key);
}
@Override
public void onKeyExited(String key) {
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
}
创建一个 Class 来扩展该侦听器并覆盖所有方法
class MyGeoListener extends GeoQueryEventListener{
private ArrayList<String> keysGeolocated;
MyGeoListener(ArrayList<String> keysGeolocated){
this.keysGeolocated = keysGeolocated;
}
@Override
public void onKeyEntered(String key, GeoLocation location) {
keysGeolocated.add(key);
System.out.println("1111111111 : "+keysGeolocated);
System.out.println("2222222222 : "+key);
}
@Override
public void onKeyExited(String key) {
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
}
现在使用这个 class 如下:
geoQuery.addGeoQueryEventListener(new MyGeoListener(keysGeolocated));
由于侦听器是异步的并且需要时间来填充列表,因此紧接的下一个调用将始终不在列表中打印任何值。
尝试将打印放入延迟处理程序中,例如延迟 30000 毫秒。您可能会在列表中看到条目,因为那时应该已经触发了侦听器事件。
由于 arraylist 是可变的并且您正在传递它的引用,因此当侦听器调用时,参数中的 arraylist 也会被修改。
祝你好运,如果还有问题请告诉我。
我有这个搜索功能,可以使用 GeoFire 使用 firebase 数据库搜索特定用户。
我想做什么:
在 GeoQueryEventListener()
和 onKeyEntered
中,我想将找到的每个键添加到 ArrayList(在 onKeyEntered()
之外)。
我遇到的问题:
当我在 onKeyEntered()
中打印它时,它起作用了。
但是一旦我退出 GeoQueryEventListener()
,keysGeolocated
就是空的
public ArrayList<User> search(GeoFire geoFire, int day, final boolean isBartender, double latitude, double longitude, double radius, double rate, double nightlyRate, double hourlyRate, final String speciality){
keysGeolocated = new ArrayList<>();
FirebaseDatabase database = FirebaseDatabase.getInstance();
root = database.getReference();
DatabaseReference usersRef = root.child("Users");
GeoQuery geoQuery = geoFire.queryAtLocation(new GeoLocation(latitude, longitude), radius);
geoQuery.addGeoQueryEventListener(new MyGeoListener(keysGeolocated));
System.out.println("KEYS GEOLOCATED" + keysGeolocated);
final ArrayList<User> result = new ArrayList<>();
for(String key : keysGeolocated){
Query queryByLocation = usersRef.orderByKey().equalTo(key);
DatabaseReference refToLocationQuery = queryByLocation.getRef();
if(checkRefNull(refToLocationQuery) == true) {
System.out.println("QUERY BY LOCATION NULL");
continue;
}
Query queryByTypeOfUser = refToLocationQuery.orderByChild("isBarTender").equalTo(isBartender);
DatabaseReference refToTypeOfUserQuery = queryByTypeOfUser.getRef();
if(checkRefNull(refToTypeOfUserQuery) == true) {
System.out.println("QUERY BY TYPE NULL");
continue;
}
Query queryByRate = refToTypeOfUserQuery.orderByChild("rate").startAt(rate);
DatabaseReference refToRateQuery = queryByRate.getRef();
if(checkRefNull(refToRateQuery) == true) {
System.out.println("QUERY BY RATE NULL");
continue;
}
DatabaseReference finalRef = null;
if(isBartender == true){
if(nightlyRate == 0){
Query queryByPrice = refToRateQuery.orderByChild("nightlyRate").endAt(nightlyRate);
finalRef = queryByPrice.getRef();
}
else if(hourlyRate == 0){
Query queryByPrice = refToRateQuery.orderByChild("hourlyRate").endAt(hourlyRate);
finalRef = queryByPrice.getRef();
}
//TODO Impelement checks for available dates
}
else{
finalRef = refToRateQuery;
}
finalRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.getValue()!=null){
User userFound = dataSnapshot.getValue(User.class);
if(isBartender == true){
Bartender barTenderFound = (Bartender) userFound;
if(speciality != null){
if(barTenderFound.getSpeciality().contains(speciality)){
result.add(barTenderFound);
}
}
else{
result.add(barTenderFound);
}
}
else{
result.add(userFound);
}
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
return result;
}
public boolean checkRefNull(DatabaseReference ref){
final boolean[] result = new boolean[1];
result[0] = false;
ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
System.out.println("TEST1");
if(dataSnapshot.getValue()==null){
result[0] = true;
System.out.println("TESTTEST");
}
else{
System.out.println("TESTTEST TRUE" + dataSnapshot.getValue());
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
return result[0];
}
public void setBarTender(boolean barTender){
this.barTender = barTender;
}
}
class MyGeoListener implements GeoQueryEventListener {
private ArrayList<String> keysGeolocated;
MyGeoListener(ArrayList<String> keysGeolocated){
this.keysGeolocated = keysGeolocated;
}
@Override
public void onKeyEntered(String key, GeoLocation location) {
keysGeolocated.add(key);
}
@Override
public void onKeyExited(String key) {
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
}
创建一个 Class 来扩展该侦听器并覆盖所有方法
class MyGeoListener extends GeoQueryEventListener{
private ArrayList<String> keysGeolocated;
MyGeoListener(ArrayList<String> keysGeolocated){
this.keysGeolocated = keysGeolocated;
}
@Override
public void onKeyEntered(String key, GeoLocation location) {
keysGeolocated.add(key);
System.out.println("1111111111 : "+keysGeolocated);
System.out.println("2222222222 : "+key);
}
@Override
public void onKeyExited(String key) {
}
@Override
public void onKeyMoved(String key, GeoLocation location) {
}
@Override
public void onGeoQueryReady() {
}
@Override
public void onGeoQueryError(DatabaseError error) {
}
}
现在使用这个 class 如下:
geoQuery.addGeoQueryEventListener(new MyGeoListener(keysGeolocated));
由于侦听器是异步的并且需要时间来填充列表,因此紧接的下一个调用将始终不在列表中打印任何值。
尝试将打印放入延迟处理程序中,例如延迟 30000 毫秒。您可能会在列表中看到条目,因为那时应该已经触发了侦听器事件。
由于 arraylist 是可变的并且您正在传递它的引用,因此当侦听器调用时,参数中的 arraylist 也会被修改。
祝你好运,如果还有问题请告诉我。