I find difficult to display my communities node in ListView. This is my tree in Firebase : My Firebase tree
I would like to display in ListView in Android List of three items containing the three community (community1, 2 , 3) with their details MessageSentCount, MessageReceivedCount.
I'm newbie in Android also in Firebase, I followed many tutorials and while trying the application crashes My class :
DatabaseReference dref;
ListView listView;
ArrayList<String> list = new ArrayList<>();
ArrayAdapter<String> adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analyze);
listView =(ListView)findViewById(R.id.listview);
adapter = new ArrayAdapter<String>(this, android.R.layout.simple_dropdown_item_1line,list);
listView.setAdapter(adapter);
dref = FirebaseDatabase.getInstance().getReference("clusters");
dref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String value = dataSnapshot.getValue(String.class);
list.add(value);
adapter.notifyDataSetChanged();
}
My layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ListView
android:id="@+id/listview"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"/>
<Button
android:id="@+id/button_discover"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:text="@string/button_discover"
android:onClick="discoverYourself"/>
</LinearLayout>
I have checked many tutorials but haven't find the solution. Kindly help me out.
Thank you :)
hi , please try to add Log message when child added
public void onChildAdded(DataSnapshot dataSnapshot, String s) { String value = dataSnapshot.getValue(String.class); Log.e("value",value) list.add(value); adapter.notifyDataSetChanged(); }
to be sure that there is some child and the problem is on your listview
then try to change : adapter.notifyDataSetChanged();
by adapter.notifyItemInserted(list.size()-1);
also try to change this on your xml : android:layout_height="0dip"
by : android:layout_height="wrap_content"
to fix converting exception:
dref = FirebaseDatabase.getInstance().getReference("clusters/Community");
dref.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(DataSnapshot dataSnapshot, String s) {
String name = dataSnapshot.child("name").getValue(String.class);
int messagesSentCount = dataSnapshot.child("messagesSentCount").getValue(Integer.class);
list.add(name);
adapter.notifyItemInserted(list.size()-1);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.