我有問題讓我的firebase上的.child項目顯示在我的android應用程序的listview上。從firebase獲取孩子值到android應用程序
當我使用下面的代碼:
String value = dataSnapshot.child("messages").getValue(String.class);
list.add(value);
adapter.notifyDataSetChanged();
我能夠檢索數據,但是當一個變化的火力發,所有的數據被刪除,並顯示新的條目。
我希望能夠從數據庫中檢索.child條目例如
fDatabase.child("messages").child("message").addChildEventListener(new ChildEventListener()
我只希望它顯示,更新和改變孩子的消息。
有人能幫助我,與數據庫的連接是好的,應用程序不會崩潰。由於不活動而斷開連接。
難道這是firebase的權限問題嗎?
我的火力規則是當前設置的公衆對此我知道斷
{
"rules": {
"messages":{
".read": true,
".write":true,
}
}
}
有什麼建議?
我的代碼
public class MainActivity extends AppCompatActivity {
DatabaseReference fDatabase;
ListView listview;
ArrayList<String> list = new ArrayList<>();
ArrayAdapter <String> adapter;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line, list);
listview.setAdapter(adapter);
fDatabase = FirebaseDatabase.getInstance().getReference();
fDatabase.child("messages").child("message").addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.child("messages").getValue(String.class);
adapter.add((String)dataSnapshot.child("messages").getValue());
list.add(value);
adapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(DataSnapshot dataSnapshot, String s) {
adapter.clear();
adapter.add((String)dataSnapshot.child("messages").getValue());
String value = dataSnapshot.child("messages").getValue(String.class);
list.add(value);
adapter.notifyDataSetChanged();
}
@Override
public void onChildRemoved(DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(DataSnapshot dataSnapshot, String s) {
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
}
我已經看過了這個問題,但無法得到它的工作要麼
Get child ID from Firebase android
當我改變我的代碼事件偵聽器,我回來空:空: null
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listview = (ListView) findViewById(R.id.listview);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line);
listview.setAdapter(adapter);
fDatabase = FirebaseDatabase.getInstance().getReference();
fDatabase.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot messageSnapshot : dataSnapshot.getChildren()) {
adapter.clear();
String temp = (String) messageSnapshot.child("temp").getValue();
String humi = (String) messageSnapshot.child("humi").getValue();
String status = (String) messageSnapshot.child("status").getValue();
adapter.add(humi + " : " + temp + " : " + status);
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
數據庫結構
條jmproject-*****
humi: "77"
status: "true"
temp: "24c"
數據庫規則
{
"rules": {
".read": true,
".write":true,
}
}
檢查此庫由firebase提供。 [使用FirebaseUI來填充ListView](https://github.com/firebase/FirebaseUI-Android/blob/master/database/README.md#using-firebaseui-to-populate-a-listview) – Saurabh