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();
}
});
}
}
当我对这个databasetry()
动作敬酒时,它给了我一切,当将它保存为一个列表并在 init 动作中使用它时,它给了我一个错误。
仅供参考:我想以他的名字(作品)从 Firebase 下载对象,我将它下载到currentUser
(作品)移动到 lst 和 toast toString() 然后我想在其中使用它init()
,它给了我一个错误。我无法解决它,所以如果你能给我解决方案或关于代码的提示。
谢谢你们所有的男孩和女孩;)
如果我理解正确,看起来您想在其中设置一个值,onDataChange
然后在另一个代码块中使用该值。如果是这种情况,您面临的挑战是对Firebase
数据库的调用是异步的,这意味着您无法确定何时onDataChange
将被调用,然后知道使用onDataChange
.
尝试将您的呼叫init()
从onCreate
转到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();
}
});
}
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句