I am having issues getting my .child items which are on my firebase displaying on my listview on my android application.
When I use the following code:
String value = dataSnapshot.child("messages").getValue(String.class);
list.add(value);
adapter.notifyDataSetChanged();
I am able to retrieve the data, but when a change is made to the firebase, all the data is removed and it displays the new entry.
I want to be able to retrieve .child entries from the database for example
fDatabase.child("messages").child("message").addChildEventListener(new ChildEventListener()
I would only want it to display, update and change the child messages.
Can somebody help me out, the connection to database is fine, the app is not crashing. just disconnecting due to inactivity.
Could this be a permissions thing on firebase?
My firebase rules are currently set public which i am aware off
{
"rules": {
"messages":{
".read": true,
".write":true,
}
}
}
Any advice ?
My code
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) {
}
});
}
}
I have looked at this question but cant get it working either
Get child ID from Firebase android
When i change my code to event listener, i get back null : null : 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) {
}
Database structure
jmproject-*****
humi: "77"
status: "true"
temp: "24c"
Database rules
{
"rules": {
".read": true,
".write":true,
}
}