Reputation: 3
public class ShowRecipe extends AppCompatActivity {
private String recipeName;
private ArrayList<Recipe> lst=new ArrayList<Recipe>();
ProgressDialog pg;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_recipe);
getInfo();
pg = ProgressDialog.show(this, "Loading", "please wait", true);
databasetry();
init();
}
public void init() {
TextView recipeNameTV = (TextView) findViewById(R.id.show_recipe_TV);
recipeNameTV.setText(""+lst.get(0).getRecipeName());
CheckBox cosher = (CheckBox) findViewById(R.id.checkBoxCosher_ShowRecipe);
CheckBox peanutes = (CheckBox) findViewById(R.id.checkBoxPeanues_ShowRecipe);
CheckBox gloten = (CheckBox) findViewById(R.id.checkBoxGluten_ShowRecipe);
Boolean c=lst.get(0).getCosher();
Boolean p=lst.get(0).getPeanut();
Boolean g=lst.get(0).getGluten();
cosher.setChecked(c);
peanutes.setChecked(p);
gloten.setChecked(g);
}
private void getInfo() {
Bundle extras = getIntent().getExtras();
if (extras != null) {
recipeName = extras.getString("recipeName");
}
}
public void databasetry(){
Firebase myFirebaseRef = new Firebase("https://recipe4u-f7470.firebaseio.com/");
myFirebaseRef.child("Recipes").child(""+recipeName).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Recipe currentUser = dataSnapshot.getValue(Recipe.class);
lst.add(currentUser);
Toast.makeText(ShowRecipe.this, "Name:"+currentUser.toString(), Toast.LENGTH_LONG).show();
pg.dismiss();
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(ShowRecipe.this, "ERROR", Toast.LENGTH_SHORT).show();
}
});
}
}
When i make a toast on the databasetry()
action it gives me everything , when is save it as a list and use it in init action it gives me an error .
FYI : i want to download the object from the Firebase by his name (works) i download it to the currentUser
(works) move to lst and toast toString() and than i want to use it in init()
and it gives me an error . i can't solve it so if you can give me the solution or tips about the code .
Thanks you all guys and girls ;)
Upvotes: 0
Views: 135
Reputation: 1421
If I understand correctly it looks like you want to set a value in onDataChange
then use that value in another block of code. If that's the case your challenge is that the call made to your Firebase
database is asynchronous, which means that you can't be sure when onDataChange
will be called and then know it is safe to use the value that was set in onDataChange
.
Try moving your call to init()
from onCreate
to onDataChange
.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_recipe);
getInfo();
pg = ProgressDialog.show(this, "Loading", "please wait", true);
databasetry();
}
public void databasetry(){
Firebase myFirebaseRef = new Firebase("yourFirebaseURL");
myFirebaseRef.child("Recipes").child(""+recipeName).addListenerForSingleValueEvent(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
Recipe currentUser = dataSnapshot.getValue(Recipe.class);
lst.add(currentUser);
Toast.makeText(ShowRecipe.this, "Name:"+currentUser.toString(), Toast.LENGTH_LONG).show();
pg.dismiss();
init();
}
@Override
public void onCancelled(FirebaseError firebaseError) {
Toast.makeText(ShowRecipe.this, "ERROR", Toast.LENGTH_SHORT).show();
}
});
}
Upvotes: 1