I'm developing a android bill management application, and want to retrieve some specified data to an android listview.

I used valueEventListener for this task but still couldn't able to retrieve data. How can I get those data to a listview using the valueEventListener?

XML

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    tools:context=".SubmitReading">


    <android.support.v7.widget.Toolbar
        android:id="@+id/sr_toolbar1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimary"
        android:minHeight="?attr/actionBarSize"
        android:theme="?attr/actionBarTheme"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="12dp"
        android:layout_marginBottom="8dp"
        android:fontFamily="@font/notosans_bold"
        android:text="Submit a Reading"
        android:textColor="@color/colorAccent"
        android:textSize="70px"
        app:layout_constraintBottom_toBottomOf="@+id/sr_toolbar1"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="1.0" />

    <ImageButton
        android:id="@+id/bt_submit_a_reading_back"
        android:layout_width="52dp"
        android:layout_height="56dp"
        android:background="@null"
        android:foreground="?attr/selectableItemBackground"
        android:src="@drawable/ic_arrow_back_black_24dp"
        app:layout_constraintEnd_toStartOf="@+id/textView3"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="@+id/sr_toolbar1"
        app:layout_constraintTop_toTopOf="parent" />

    <LinearLayout
        android:id="@+id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="16dp"
        android:layout_marginTop="16dp"
        android:layout_marginEnd="16dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/sr_toolbar1">

        <SearchView
            android:id="@+id/search_address"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:queryHint="Search here">

        </SearchView>

    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="68dp"
        android:layout_marginEnd="8dp"
        android:orientation="horizontal"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/linearLayout">

        <ListView
            android:id="@+id/list_address"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

        </ListView>

    </LinearLayout>


</android.support.constraint.ConstraintLayout>

Java

package com.ceb.meterreaderassistant;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;



import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;


import java.util.ArrayList;

public class SubmitReading extends AppCompatActivity {

    private ListView listView;
    private FirebaseDatabase cebDb;
    private DatabaseReference dbRef;


    private ArrayList<String>  arrayList = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_submit_reading);

        cebDb = FirebaseDatabase.getInstance();
        dbRef = cebDb.getReference("clients");

        listView = (ListView)findViewById(R.id.list_address);
        final ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
        listView.setAdapter(arrayAdapter);


    }
}

I expect the output of the query to get clientAddress data in a listview, Currently I'm not getting the required output

enter image description here

Answer

You need to do the following:

cebDb = FirebaseDatabase.getInstance();
dbRef = cebDb.getReference("clients");

dmyRef.addValueEventListener( new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot){
    if(dataSnapshot.exists()){
      for(DataSnapshot ds : dataSnapshot.getChildren()){
         String address = ds.child("clientAddress").getValue(String.class);
       }
    }
}

@Override
public void onCancelled(DatabaseError databaseError) {

   }
});

You need to attach ValueEventListener and then iterate inside the direct children to be able to retrieve the client address.

  • 1
Reply Report
      • 1
    • Small node: I prefer .getValue(String.class) over .getValue().toString() as: 1) the latter will throw a NullPointerException if the property doesn't exist, 2) the former will throw an exception if the property exists, but is of a different type.
      • 2
    • Lol @FrankvanPuffelen you always tell me this note, I keep forgetting to change it. Changed it now, thank you!

Warm tip !!!

This article is reproduced from Stack Exchange / Stack Overflow, please click

Trending Tags

Related Questions