如何在Firebase Android中检索POJO

时间:2016-11-13 23:56:21

标签: android firebase firebase-realtime-database

public class MainActivity extends AppCompatActivity {

    private Firebase mRef;
    private EditText mName,mEmail,mAge;
    private Button mAddStud;


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

        mName = (EditText) findViewById(R.id.editTextName);
        mEmail = (EditText) findViewById(R.id.editTextEmail);
        mAge = (EditText) findViewById(R.id.editTextAge);
        mAddStud = (Button) findViewById(R.id.addStudents);


        mRef = new Firebase(Constants.FIREBASE_URL);

        mAddStud.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Student student = new Student(mName.getText().toString(),mEmail.getText().toString(), Integer.parseInt(mAge.getText().toString()));
                mRef.child("newStudent").setValue(student);
            }
        });

    }
}

显示

public class Display extends AppCompatActivity {

    private Firebase mRef;
    private TextView mDisplay;

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

        mDisplay = (TextView) findViewById(R.id.textViewDisplay);

        mRef = new  Firebase("https://firecrestapp.firebaseio.com/newStudent");

        mRef.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                Student value = dataSnapshot.getValue(Student.class);
                mDisplay.setText(value.getName()+" "+value.getEmailId()+" "+value.getAge() );

            }

            @Override
            public void onCancelled(FirebaseError firebaseError) {

            }
        });

    }
}

我收到此错误

  

com.firebase.client.FirebaseException:无法退回键入

我该如何检索数据?

0 个答案:

没有答案