尝试获取特定节点上可用但无法从Firebase数据库获取数据的数据

时间:2019-07-20 11:44:18

标签: android firebase firebase-realtime-database

我想获取扇区10节点上可用的数据,并希望在recycler_view中显示每个节点(每个节点中可用的数据,即Adress,First_name,Phone_no),但是我无法获取数据。我正在使用push()函数在数据库中创建新条目。

this is my database

这是我正在使用的代码,

for(DataSnapshot ds:dataSnapshot.getChildren()){
     getinfo info=new getinfo();
     info.setName(ds.child("maid").child("area").child("gurgaon").child("sector_10").child(uID).getValue(getinfo.class).getName());
     info.setAge(ds.child("maid").child("area").child("gurgaon").child("sector_10").child(uID).getValue(getinfo.class).getAge());
     info.setSex(ds.child("maid").child("area").child("gurgaon").child("sector_10").child(uID).getValue(getinfo.class).getSex());

通过使用此代码,我可以获取扇区10上可用的特定节点。 这是我的fetch_info Java文件

public fetch_info() {
}

public fetch_info(String first_Name, String adress, String phone_no) {
    First_Name = first_Name;
    Adress = adress;
    Phone_no = phone_no;
}

public String getFirst_Name() {
    return First_Name;
}

public void setFirst_Name(String first_Name) {
    First_Name = first_Name;
}

public String getAdress() {
    return Adress;
}

public void setAdress(String adress) {
    Adress = adress;
}

public String getPhone_no() {
    return Phone_no;
}

public void setPhone_no(String phone_no) {
    Phone_no = phone_no;
}

}

`

1 个答案:

答案 0 :(得分:0)

要获取class SmsReceiver : BroadcastReceiver(){ override fun onReceive(context: Context?, intent: Intent) { val extras = intent.extras try { if(extras != null){ var sms: Array<Any> = extras.getString("pdus") as Array<Any> for(i in sms.indices){ val format = extras.getString("format") var smsMessage = if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.M){ SmsMessage.createFromPdu(sms[i] as ByteArray,format) }else{ SmsMessage.createFromPdu(sms[i] as ByteArray) } var phoneNumber = smsMessage.originatingAddress val messageText = smsMessage.messageBody.toString() Toast.makeText(context,"$phoneNumber:(Private)\n" + "messageText: $messageText",Toast.LENGTH_SHORT).show() } } }catch (ex:Exception){ //your error handling code here //here, consider adding Log.e("SmsReceiver", ex.localizedMessage) //this log statement simply prints errors to your android studio terminal and will help with debugging, alternatively leave it out if (context != null){ Toast.makeText(context!!,ex.localizedMessage, Toast.LENGTH_SHORT).show() } } } } 的所有子节点,请结合使用Firebase的侦听器和const fbConfig = {}; const rrfConfig = { userProfile: "users", useFirestoreForProfile: true }; // Initialize firebase instance firebase.initializeApp(fbConfig); const rootReducer = combineReducers({ firebase: firebaseReducer, firestore: firestoreReducer }); // Create store with reducers and initial state const initialState = {}; const store = createStore(rootReducer, initialState); const rrfProps = { firebase, config: rrfConfig, dispatch: store.dispatch, createFirestoreInstance }; class App extends Component { render() { return <div>HOMR</div>; } } ReactDOM.render( <Provider store={store}> <ReactReduxFirebaseProvider {...rrfProps}> <App /> </ReactReduxFirebaseProvider> </Provider>, document.getElementById("root") ); 的方法来遍历数据。

类似的东西:

maid/area/gurgaon/sector_10

主要区别:

  • 我使用一种简短得多的语法来引用DataSnapshot
  • 我将侦听器附加在JSON树的较低位置,以便您检索更少的数据。
  • 我在DatabaseReference root = FirebaseDatabase.getInstance().getReference(); DatabaseReference sectorRef = root.child("maid/area/gurgaon/sector_10"); sectorRef.addListenerForSingleValueEvent(new ValueEventListener() { @Override public void onDataChange(DataSnapshot dataSnapshot) { for (DataSnapshot childSnapshot: dataSnapshot.getChildren()) { System.out.println(childSnapshot.getKey()); // -Lk4..., -Lk8... System.out.println(childSnapshot.child("Address").getValue(String.class)); // dghvbb, kjetr... } } @Override public void onCancelled(DatabaseError databaseError) { throw databaseError.toException(); } } 上使用循环来迭代sector_10的动态生成的子节点。