gpt4 book ai didi

java - 从 FirebaseDatabase 为全局变量赋值

转载 作者:行者123 更新时间:2023-12-02 11:07:05 26 4
gpt4 key购买 nike

我想知道是否可以使用 FirebaseDatabase 方法为全局变量赋值。我知道 FirebaseDatabase 方法是异步的,并且在后台执行,不会阻塞主线程。但我陷入了在 onDataChange() 方法之外需要该值的情况。这是我的 java 类文件:

public class DetailActivity extends YouTubeBaseActivity implements YouTubePlayer.OnInitializedListener {

private ProgressBar mProgressBar;
private CoordinatorLayout coordinatorLayout;

private static final int RECOVERY_DIALOG_REQUEST = 1;
YouTubePlayerView youTubePlayerView;

int currentPage = 0;
Timer timer;
final long DELAY_MS = 500;
final long PERIOD_MS = 3000;
CircleIndicator circleIndicator;
private ViewPager viewPager;
private PagerAdapter adapter;

private ArrayList<String> images;
private ArrayList<String> imageDetails;

private DatabaseReference databaseReference;

private TextView capital, area, population,languages,literacyRate;
private ImageView appbarImageView;
private ExpandableTextView historyTextView;

private String youTubeVideoLink;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
CollapsingToolbarLayout collapsingToolbarLayout = findViewById(R.id.collapsing_toolbar);

viewPager = findViewById(R.id.view_pager);
circleIndicator = findViewById(R.id.indicator);
mProgressBar = findViewById(R.id.progress_bar);

youTubePlayerView = findViewById(R.id.youtube_player_view);
youTubePlayerView.initialize(BuildConfig.ApiKey, this);

population = findViewById(R.id.population);
capital = findViewById(R.id.capital);
area = findViewById(R.id.area);

coordinatorLayout = findViewById(R.id.detail_container);
appbarImageView = findViewById(R.id.app_bar_image);
historyTextView = findViewById(R.id.expand_text_view);
literacyRate = findViewById(R.id.literacy_rate);
languages = findViewById(R.id.languages);

Intent intent = getIntent();
if(intent.hasExtra("State")) {
collapsingToolbarLayout.setTitle(intent.getStringExtra("State"));
databaseReference = FirebaseDatabase.getInstance().getReference().child("States").child(intent.getStringExtra("State").trim());
}



final Handler handler = new Handler();
final Runnable update = new Runnable() {
@Override
public void run() {
if(currentPage == 12) {
currentPage = 0;
}
viewPager.setCurrentItem(currentPage++,true);
}
};
timer = new Timer();
timer.schedule(new TimerTask() {
@Override
public void run() {
handler.post(update);
}
},DELAY_MS,PERIOD_MS);
}

private void readDataFromDatabase(final StatesCallback statesCallback) {
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
StateDetails stateDetails = dataSnapshot.getValue(StateDetails.class);
statesCallback.onCallback(stateDetails);
}

@Override
public void onCancelled(DatabaseError databaseError) {

}
});
}

@Override
protected void onDestroy() {
super.onDestroy();
timer.cancel();
}

@Override
public void onInitializationSuccess(YouTubePlayer.Provider provider, YouTubePlayer youTubePlayer, boolean b) {

readDataFromDatabase(new StatesCallback() {
@Override
public void onCallback(StateDetails stateDetails) {
mProgressBar.setVisibility(View.GONE);
coordinatorLayout.setVisibility(View.VISIBLE);
capital.setText(stateDetails.getCapital());
area.setText(stateDetails.getArea());
population.setText(stateDetails.getPopulation());
historyTextView.setText(stateDetails.getHistory());
languages.setText(stateDetails.getLanguages());
literacyRate.setText(String.valueOf(stateDetails.getLiteracyRate()));
images = stateDetails.getImages();
imageDetails = stateDetails.getImageDetails();
youTubeVideoLink = stateDetails.getYouTubeVideoLink();

Log.i("youtube video",youTubeVideoLink);
Picasso.get().load(images.get(0)).into(appbarImageView);
adapter = new CustomAdapter(DetailActivity.this, images, imageDetails);
viewPager.setAdapter(adapter);
circleIndicator.setViewPager(viewPager);
adapter.registerDataSetObserver(circleIndicator.getDataSetObserver());

}
});
if(!b) {
Log.i("Checking order","video link: "+youTubeVideoLink);
youTubePlayer.cueVideo(youTubeVideoLink);
}
}

@Override
public void onInitializationFailure(YouTubePlayer.Provider provider, YouTubeInitializationResult youTubeInitializationResult) {
if(youTubeInitializationResult.isUserRecoverableError()) {
youTubeInitializationResult.getErrorDialog(DetailActivity.this,RECOVERY_DIALOG_REQUEST);
} else {
String errorMessage = getString(R.string.youTubeErrorMessage)+youTubeInitializationResult;
Toast.makeText(DetailActivity.this,errorMessage,Toast.LENGTH_SHORT).show();
}

}
}

这是日志详细信息:

I/Checking order: video link: null
I/youtube video: QR19-rUVjNQ

我想将动态视频分配给 YouTubePlayer,其链接通过 youTubeVideoLink 字符串存储在数据库中。我已经尝试过列出的方法 here

但这并没有帮助。那么,有没有办法让初始化的youTubeVideoLink发送到onInitializationSuccess()方法呢?谢谢。

最佳答案

您不能简单地创建一个全局变量并在 onDataChange() 之外使用它的值,因为此方法具有异步行为。如果您想在 onDataChange() 方法之外使用来自数据库的值,您有两种选择。第一个是将所需的对象作为参数传递给您的类中定义的方法,或者,如果您想要以更复杂的方式,则深入了解现代 API 的异步世界并查看我答案的最后部分在这个 post ,其中我已经一步步解释了如何实现这一目标。欲了解更多信息,您还可以看看这个 video

编辑:

databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
StateDetails stateDetails = dataSnapshot.getValue(StateDetails.class);
methodThatDoesSomething(stateDetails); //Method call

}

@Override
public void onCancelled(DatabaseError databaseError) {}
});

private void methodThatDoesSomething(StateDetails stateDetails) {
//Do what you need to do with your stateDetails object
}

关于java - 从 FirebaseDatabase 为全局变量赋值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50873078/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com