从firebase检索数据:FirebaseException:无法退回到键入?

时间:2017-08-07 14:01:49

标签: android firebase firebase-realtime-database

  

我正在构建一个从firebase检索数据的应用程序。要显示   活动中的数据,我移动到活动时收到错误   从firebase获取数据。

以下是“人物对象”的获取者和设定者

<li class="mix app">
                        <a href="/home.html" data-largesrc="img/portfolio/portx1.jpg" data-title="Azuki bean" data-description="Swiss chard pumpkin bunya nuts maize plantain aubergine napa cabbage soko coriander sweet pepper water spinach winter purslane shallot tigernut lentil beetroot.">
                            <img src="img/portfolio/portx1.jpg" alt="Meghna">
                            <div class="hover-mask">
                                <h3>Azuki bean</h3>
                                <span>
                                    <i class="fa fa-plus fa-2x"></i>
                                </span>
                            </div>
                        </a>
                    </li>

主类的Java代码..

public class Person {

//name and address string
private String name;
private String address;

public Person() {
  /*Blank default constructor essential for Firebase*/
}
//Getters and setters
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getAddress() {
    return address;
}

public void setAddress(String address) {
    this.address = address;
}

这是我的firebase的屏幕截图.. enter image description here

应用程序的崩溃日志

public class Main2Activity extends AppCompatActivity {
private EditText editTextName;
private EditText editTextAddress;
private TextView textViewPersons;
private Button buttonSave;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    buttonSave = (Button) findViewById(R.id.buttonSave);
    buttonSave = (Button) findViewById(R.id.buttonSave);
    editTextName = (EditText) findViewById(R.id.editTextName);
    editTextAddress = (EditText) findViewById(R.id.editTextAddress);

    textViewPersons = (TextView) findViewById(R.id.textViewPersons);

    Firebase.setAndroidContext(this);

    //Click Listener for button
    buttonSave.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Creating firebase object
            Firebase ref = new Firebase(Config.FIREBASE_URL);

            //Getting values to store
            String name = editTextName.getText().toString().trim();
            String address = editTextAddress.getText().toString().trim();

            //Creating Person object
            Person person = new Person();

            //Adding values
            person.setName(name);
            person.setAddress(address);

            //Storing values to firebase
            ref.child("Person").setValue(person);

            //Value event listener for realtime data update
            ref.child("Person").addValueEventListener(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot snapshot) {
                    for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                        //Getting the data from snapshot
                        Person person = postSnapshot.getValue(Person.class);

                        //Adding it to a string
                        String string = "Name: "+person.getName()+"\nAddress: "+person.getAddress()+"\n\n";

                        //Displaying it on textview
                        textViewPersons.setText(string);
                    }
                }

                @Override
                public void onCancelled(FirebaseError firebaseError) {
                    System.out.println("The read failed: " + firebaseError.getMessage());
                }
            });

        }
    });

}

I /处理:发送信号。 PID:22852 SIG:9 申请已终止。

2 个答案:

答案 0 :(得分:0)

要显示名称和地址,请使用以下代码:

ref.child("Person").addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot snapshot) {
    String name = postSnapshot.child("name").getValue(String.class);
    String address = postSnapshot.child("address").getValue(String.class);
    String string = "Name: "+name+"\nAddress: "+address+"\n\n";

    //Displaying it on textview
    textViewPersons.setText(string);
}

@Override
public void onCancelled(FirebaseError firebaseError) {
    System.out.println("The read failed: " + firebaseError.getMessage());
}
});

答案 1 :(得分:0)

当您将此人写入数据库时​​,请将其写为:

//Storing values to firebase
ref.child("Person").setValue(person);

这会在数据库中写入一个人,也会在控制台的屏幕截图中显示。

当您读取数据时,您可以:

ref.child("Person").addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot snapshot) {
        for (DataSnapshot postSnapshot : snapshot.getChildren()) {
            Person person = postSnapshot.getValue(Person.class);

            String string = "Name: "+person.getName()+"\nAddress: "+person.getAddress()+"\n\n";

            //Displaying it on textview
            textViewPersons.setText(string);
        }
    }

所以你循环覆盖Person节点的子节点。由于您只将一个人写入数据库,因此不需要该循环,实际上会破坏代码。要正确执行此操作,请删除循环:

public void onDataChange(DataSnapshot snapshot) {
    Person person = postSnapshot.getValue(Person.class);

    String string = "Name: "+person.getName()+"\nAddress: "+person.getAddress()+"\n\n";

    //Displaying it on textview
    textViewPersons.setText(string);

}

顺便说一句,您使用的是相当旧版本的Firebase。我建议升级到更新版本,即使只是因为您更有可能获得帮助。要升级,请查看此处:https://firebase.google.com/support/guides/firebase-android