我有一个应用程序正在检查存储在Firebase数据库中的兴趣点。每个用户都有自己对流派的特定兴趣,这会影响地图上显示的内容。
为了分析每个用户的这些兴趣,我从数据库中获取他们的兴趣并将它们放在特定于用户的散列图中。 在用户的构造函数中,我调用了setInterests()方法,该方法实际上在HashMap中执行数据设置。
我还在User类中有一个方法,用于检查用户interestMap
的特定genreID的内容。如果他们拥有它,则返回true,如果他们没有,则返回false。我还检查hashmap是否为空,如果是,我想显示所有数据。但是,当调用hasInterest()
方法时,尚未设置用户的interestMap。在User构造函数中调用setInterets()
,用户实例在我MapActivity
的onCreate中创建。
用户构造函数
public User(String username){
this.username = username;
interestMap = new HashMap<>();
setInterests();
}
有兴趣方法
// Check if the user has the listed interest
public boolean hasInterest(final long genreId){
String id = String.valueOf(genreId);
System.out.println("Has Interest: " + interestMap);
// If the user has no interests, display the listed interest
if(interestMap.isEmpty()){
Log.d(TAG, "User has no interests, showing all " + interestMap.get(id));
return true;
}
Object interest = interestMap.get(id);
// If the user does not have the listed interest, don't display it
if(interest == null){
Log.d(TAG, "User does not have interest in this, not showing data");
return false;
}
Log.d(TAG, "User has interest in Genre"+ interestMap.get(id)+ ", showing data");
return true;
}
设置兴趣方法
public void setInterests() throws DatabaseException{
mDatabase = FirebaseDatabase.getInstance().getReference("users/" + getUid()+"/interests");
try{
mDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot interestSnapshot: dataSnapshot.getChildren()){
String interestKey = interestSnapshot.getKey();
Object interestValue = interestSnapshot.getValue();
Log.d(TAG, "Key = " + interestKey + "; Value = " + interestValue);
push(interestKey, interestValue);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
Log.w(TAG, "Could not access data: ", databaseError.toException());
Log.d(TAG, "Error Code: " + databaseError.getCode());
}
});
}catch(DatabaseException e){
Log.w(TAG, "Database Exception " + e);
}
System.out.println(interestMap);
}
在创建方法上映射活动
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
checkLocationPermission();
}
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
Firebase.setAndroidContext(this);
auth = FirebaseAuth.getInstance();
user = new User(auth.getCurrentUser().getEmail());
在Maps OnLocationChanged
中调用Has Interest方法ref.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Log.d(TAG, "Inside onDataChange");
for (final DataSnapshot poiSnapshot : dataSnapshot.getChildren()) {
final String mPoiSnapshot = poiSnapshot.getKey();
// Get data from Firebase
final String mPoiTitle = (String) poiSnapshot.child("title").getValue();
final String mPoiStory = (String) poiSnapshot.child("story").getValue();
final long mPoiGenreID = (long) poiSnapshot.child("Genre/ID").getValue();
final String mPoiGenreType= (String) poiSnapshot.child("Genre/Type").getValue();
final POIGenre mPoiGenre = new POIGenre(mPoiGenreID, mPoiGenreType);
if(user.hasInterest(mPoiGenreID)) {
出于某种原因,在实际创建用户之前调用此hasInterest()
。我尝试在OnLocationChanged方法中创建我的用户,其中实际调用hasInterest()
作为调试方法,但这并没有影响任何事情。
这是我的调试,我打印出interestMap
的内容以及hasInterest()
方法检查时的内容。
I/System.out: Has Interest: {}
D/******* USER user@user.com: User has no interests, showing all null
I/System.out: Has Interest: {}
D/******* USER user@user.com: User has no interests, showing all null
I/System.out: Has Interest: {}
D/******* USER user@user.com: User has no interests, showing all null
I/System.out: Has Interest: {}
D/******* USER user@user.com: User has no interests, showing all null
D/******* USER user@user.com: Key = 1; Value = Historical
I/System.out: {1=Historical}
最后,您可以看到我的Firebase中的历史类型被放置在interestMap
我只是错过了一些痛苦明显的东西吗?任何帮助都会受到大力赞赏。