2017-06-11 18 views
0

クラスを使用してfirebaseデータベースに書き込もうとすると、多くの変数が含まれますが、正しく動作しますが、データを取得しようとするとアプリケーションがクラッシュします!! なぜ私は夢中になるでしょうか?firebaseはデータを毎回検索していますか?すべての試行?

> Javaコード

FirebaseDatabase database; 
    DatabaseReference myRef; 
    TextView text1, text2, text3; 
    EditText et1, et2, et3; 
    Button bSave, bRead; 


    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     database = FirebaseDatabase.getInstance(); 
     myRef = database.getReference(); 

     text1 = (TextView) findViewById(R.id.text1); 
     text2 = (TextView) findViewById(R.id.text2); 
     text3 = (TextView) findViewById(R.id.text3); 

     et1 = (EditText) findViewById(R.id.et1); 
     et2 = (EditText) findViewById(R.id.et2); 
     et3 = (EditText) findViewById(R.id.et3); 


     bSave = (Button) findViewById(R.id.bSave); 
     bSave.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       String str1 = et1.getText().toString(); 
       String str2 = et2.getText().toString(); 
       String str3 = et3.getText().toString(); 

       myRef.child("Posts").push().setValue(new text(str1,str2,str3)); 
      } 
     }); 


     bRead = (Button) findViewById(R.id.bRead); 
     bRead.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       myRef=database.getReference().child("Posts"); 
       myRef.addValueEventListener(new ValueEventListener() { 
        @Override 
        public void onDataChange(DataSnapshot dataSnapshot) { 

         //doesn't work 
         text t1 = dataSnapshot.getValue(text.class); 

         //doesn't work 
         /*for (DataSnapshot snapshot : dataSnapshot.getChildren()) { 
          text t1 = snapshot.getValue(text.class); 
         }*/ 

        } 

        @Override 
        public void onCancelled(DatabaseError databaseError) { 
         Log.e("MainActivity", "loadPost:onCancelled", databaseError.toException()); 
        } 
       }); 
      } 
     }); 


    } 

} 

> Text.class

public class text { 
    String text1,text2,text3; 


    public text(String text1, String text2, String text3) { 
     this.text1 = text1; 
     this.text2 = text2; 
     this.text3 = text3; 
    } 

    public String getText1() { 
     return text1; 
    } 

    public void setText1(String text1) { 
     this.text1 = text1; 
    } 

    public String getText2() { 
     return text2; 
    } 

    public void setText2(String text2) { 
     this.text2 = text2; 
    } 

    public String getText3() { 
     return text3; 
    } 

    public void setText3(String text3) { 
     this.text3 = text3; 
    } 
} 

>スクリーン

Screen

+0

空のテキストコンストラクタを追加するには、Firebaseが必要です。 公開テキスト(){ } – uguboz

+0

uguboz .. I Love You Man <3 私は魔法です! あなたは完璧ですすべての愛<3 –

+0

他人に見えるように答えとして投稿します。 – uguboz

答えて

0

Firebaseは空のコンストラクタを必要とするので、ここでは常にそれを保存してください。

public class text { 
    String text1,text2,text3; 

    public text() { } 

    public text(String text1, String text2, String text3) { 
     this.text1 = text1; 
     this.text2 = text2; 
     this.text3 = text3; 
    } 
関連する問題