如何根据指定条件从数据库列表中检索项目

时间:2019-03-28 09:23:02

标签: android firebase firebase-realtime-database

我正在开发一个Android帐单管理应用程序,并希望将某些指定的数据检索到Android ListView。

我为此任务使用了valueEventListener,但仍然无法检索数据。如何使用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);


    }
}

我希望查询的输出能够在列表视图中获取clientAddress数据,目前我没有得到所需的输出

enter image description here

1 个答案:

答案 0 :(得分:1)

您需要执行以下操作:

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) {

   }
});

您需要附加ValueEventListener,然后在直接子级中进行迭代才能检索客户地址。