在我的离子应用程序中,我正在使用Firebase检索所有已注册的驱动程序及其数据。我的架构如下:
ehara
Driver
fjk59KJjkfkjflhghk
driverId: 1
location
0: 9.086333699999999
1: 7.459455999999999
我的问题是这个;如果我仅知道他的driverId,如何查询数据库以获取他的位置?
答案 0 :(得分:2)
您将必须执行查询以查找具有给定ID的驱动程序:
//Pass the position of the item on the listview/playlist from previous activity /fragment.
Intent _intent = getIntent();
position = _intent.getIntExtra("postion_id", 0);
player = ExoPlayerFactory.newSimpleInstance(this, new DefaultTrackSelector());
playerView.setPlayer(player);
DefaultDataSourceFactory dataSourceFactory = new DefaultDataSourceFactory (this, Util.getUserAgent(this, "exo-demo"));
ConcatenatingMediaSource concatenatingMediaSource = new ConcatenatingMediaSource();
//Ensure to populate the allFiles array.
for (int i = 0; i < allFiles.size(); i++) {
File currentFile = new File(allFiles.get(i));
MediaSource mediaSource = new ExtractorMediaSource.Factory(dataSourceFactory).createMediaSource(Uri.parse("file://" + currentFile.getAbsolutePath()));
concatenatingMediaSource.addMediaSource(mediaSource);
}
player.prepare(concatenatingMediaSource);
//Play from the item selected on the playlist from previous activity/fragment
player.seekTo(position, C.TIME_UNSET);
player.setPlayWhenReady(true);
player.addListener(new Player.EventListener() {
@Override
public void onTimelineChanged(Timeline timeline, Object manifest, int reason) {
}
@Override
public void onTracksChanged(TrackGroupArray trackGroups, TrackSelectionArray trackSelections) {
}
@Override
public void onLoadingChanged(boolean isLoading) {
}
@Override
public void onPlayerStateChanged(boolean playWhenReady, int playbackState) {
}
@Override
public void onRepeatModeChanged(int repeatMode) {
}
@Override
public void onShuffleModeEnabledChanged(boolean shuffleModeEnabled) {
}
@Override
public void onPlayerError(ExoPlaybackException error) {
}
@Override
public void onPositionDiscontinuity(int reason) {
//THIS METHOD GETS CALLED FOR EVERY NEW SOURCE THAT IS PLAYED
int latestWindowIndex = player.getCurrentWindowIndex();
if (latestWindowIndex != position) {
// item selected in playlist has changed, handle here
position = latestWindowIndex;
// ...
}
}
@Override
public void onPlaybackParametersChanged(PlaybackParameters playbackParameters) {
}
@Override
public void onSeekProcessed() {
}
});
查询可以匹配多个子节点,因此代码需要在可能的多个结果上使用firebase.database().ref('Driver')
.orderByChid('driverId')
.equalTo('1')
.once('value')
.then(function(snapshot) {
snapshot.forEach(function(driverSnapshot) {
console.log(driverSnapshot.val());
});
})
。
由于听起来您的情况下ID已经是唯一的,请考虑使用该ID作为snapshot.forEach
节点中的密钥。
Driver
在这种情况下,读取将变成简单的查找(而不是查询):
Driver
id1
location
0: 9.086333699999999
1: 7.459455999999999
由于在此结构中只有一个具有给定密钥的节点,因此我们也不再需要firebase.database().ref('Driver')
.child('id1')
.once('value')
.then(function(snapshot) {
console.log(snapshot.val());
})
循环。