监听器onDataChange Android Firebase

时间:2018-04-07 05:15:40

标签: android firebase firebase-realtime-database

我想让它存储私有字符串keyy,但它不保存,会发生什么?如果它显示数据我用textview尝试过。

DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Customers");
ValueEventListener eventListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for(DataSnapshot nodo : dataSnapshot.getChildren()) {

            String key = nodo.getKey();
            Log.d("TAG", key);
            keyy = key;
            mSetting.setText(key); // yes, show data
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {}

};
myRef.addListenerForSingleValueEvent(eventListener);
mSetting.setText(keyy); // No,show data

2 个答案:

答案 0 :(得分:1)

我希望这能解决你的问题

List<String> keyList = new ArrayList();    
    DatabaseReference myRef = FirebaseDatabase.getInstance().getReference().child("Users").child("Customers");
        ValueEventListener eventListener = new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                for(DataSnapshot nodo : dataSnapshot.getChildren()) {

                    String key = nodo.getKey();
                    keyList.add(key);

                }
               done(keyList);
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {}

        };
        myRef.addListenerForSingleValueEvent(eventListener);

        public void done(List k)
    {
      //show keys here
    }

答案 1 :(得分:1)

这样做

class Popup extends React.Component {
  constructor(props) {
        super(props);

        this.state = {
          submitWrapper: '0px',
          submitDetails: '0px',
          submitButton: 'none',
          opacity: '0'
        }
  }

  render() {
    return(
      <div id="submit-message"
           style={{height: `${this.state.submitWrapper}`}}>
          <h1 style={{opacity: `${this.state.opacity}`}}> GOOD JOB </h1>
          <hr style={{opacity: `${this.state.opacity}`}} />
            <div className="submit-details"
                 style={{height: `${this.state.submitDetails}`}}>
            </div>
            <button style={{display: `${this.state.submitButton}`}}
                    onClick={this.state.submitWrapper = '0px'}> Finished </button>
      </div>
    )
  }
}
export default Popup;