I want to know that is it possible to assign value to a global variable using FirebaseDatabase methods. I know that the FirebaseDatabase methods are asynchronous and executed in background without blocking the main thread. But I am stuck in a case where the value is needed outside the onDataChange() method. Here is my java class file:
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();
}
}
}
Here's the log details:
I/Checking order: video link: null
I/youtube video: QR19-rUVjNQ
I want to assign a dynamic video to the YouTubePlayer who's link is stored in the database through the youTubeVideoLink string. I have tried the method listed
here
But that didn't help. So,Is there any way so that the initialized youTubeVideoLink is sent to the onInitializationSuccess() method? Thank You.