2017-03-13 7 views
0
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()アクションで乾杯をするとき。 FYIFirebaseオブジェクトのダウンロード - アンドロイドスタジオ

:私はcurrentUser(作品)にダウンロード(作品)彼の名前でFirebaseからオブジェクトをダウンロードしたいが(LSTとトーストのtoStringに移動)と私はinit()でそれを使用したい、それが与えられるよりも、私は誤りです。コードについての解決策やヒントを教えてもらえれば解決できません。

おかげで、あなたのすべての男と少女;)

答えて

1

私が正しく理解していれば、あなたは、コードの別のブロックにその値を使用し、その後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(); 
     } 
    }); 
} 
+0

Works! ありがとうございます;) –

+0

問題ないです、喜んで助けてください。 –

関連する問題