如何实现对Android Firebase数据库的搜索按钮?

时间:2018-06-20 05:43:16

标签: java android firebase firebase-realtime-database android-search

我花了很多时间来实现搜索我的Android Firebase数据库。
查询:query = dateRef.orderByChild("phone").equalTo(searchPhoneNumber);
我不知道如何在EditText值中设置searchPhoneNumber。但是我知道EditText和一个用于搜索电话号码的按钮。但是我需要Java代码。例如:如果在同一页面中获得输出后,如果我在EditText中输入电话号码并按下按钮,那么
在这里,我附上了我的Firebase屏幕截图和代码。

Firebase数据库屏幕截图

enter image description here

java

public class ViewProduction extends AppCompatActivity {

private RecyclerView mRecyclerView;
private Adapater mAdapter;

private ProgressBar mprogress;

private DatabaseReference mDatabaseRef;
private List<Datastore> mUploads;


EditText edit;
Button btnsearch;



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


    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);


    edit =(EditText)findViewById(R.id.edit);

    btnsearch = (Button)findViewById(R.id.btnsearch);



    ActionBar actionBar = getSupportActionBar();
    actionBar.setTitle("Production Details");

    mRecyclerView = findViewById(R.id.recyclerj);
    mRecyclerView.setHasFixedSize(true);

    mRecyclerView.setLayoutManager(new LinearLayoutManager(this));

    mprogress = findViewById(R.id.progress);

    mUploads = new ArrayList<>();

    mDatabaseRef = FirebaseDatabase.getInstance().getReference("Rajadriving");

    btnsearch.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // called search() method on button click.
            search();
        }
    });

public void search(){
    edit.addTextChangedListener(new TextWatcher() {
        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
        }

        @Override
        public void afterTextChanged(Editable s) {

            DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();

            DatabaseReference dateRef = rootRef.child("Rajadriving").child("9-6-2018");
            Query query = dateRef.orderByChild("phone").equalTo(s.toString());

            query.addListenerForSingleValueEvent(new ValueEventListener() {
                @Override
                public void onDataChange(DataSnapshot dataSnapshot) {
                    for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                        Datastore upload = postSnapshot.getValue(Datastore.class);
                        mUploads.add(upload);
                    }
                    mAdapter = new Adapater(ViewProduction.this, mUploads);

                    mRecyclerView.setAdapter(mAdapter);
                    mAdapter.notifyDataSetChanged();
                    mprogress.setVisibility(View.INVISIBLE);
                }

                @Override
                public void onCancelled(DatabaseError databaseError) {
                    Toast.makeText(ViewProduction.this, databaseError.getMessage(),Toast.LENGTH_SHORT).show();
                    mprogress.setVisibility(View.INVISIBLE);
                }
            });
        }
    }
}





@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == android.R.id.home){
        this.finish();
    }
    return super.onOptionsItemSelected(item);
}

}

xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.udayaj.rajadriving.ViewProduction">

<EditText
    android:id="@+id/edit"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignEnd="@+id/progress"
    android:layout_alignParentTop="true"
    android:layout_alignRight="@+id/progress"
    android:hint="enter phone" />

<Button
    android:id="@+id/btnsearch"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_above="@+id/recyclerj"
    android:layout_marginLeft="21dp"
    android:layout_marginStart="21dp"
    android:layout_toEndOf="@+id/edit"
    android:layout_toRightOf="@+id/edit"
    android:text="Search" />


    <ProgressBar
        android:id="@+id/progress"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"/>


    <android.support.v7.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@+id/table01"
        android:scrollbars="vertical"
        android:id="@+id/recyclerj">
    </android.support.v7.widget.RecyclerView>

</RelativeLayout>

2 个答案:

答案 0 :(得分:2)

我希望这对您有用。

使用Textwatcher()实现搜索功能。如何实现如下。

btnsearch.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
       // called search() method on button click.
       search();
    }
});

public void search(){
        edit.addTextChangedListener(new TextWatcher() {
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void afterTextChanged(Editable s) {

                DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();

                DatabaseReference dateRef = rootRef.child("Rajadriving").child("9-6-2018");
                Query query = dateRef.orderByChild("phone").equalTo(s.toString());

                query.addListenerForSingleValueEvent(new ValueEventListener() {
                    @Override
                    public void onDataChange(DataSnapshot dataSnapshot) {
                        for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
                            Datastore upload = postSnapshot.getValue(Datastore.class);
                            mUploads.add(upload);
                        }
                        mAdapter = new Adapater(ViewProduction.this, mUploads);

                        mRecyclerView.setAdapter(mAdapter);
                        mAdapter.notifyDataSetChanged();
                        mprogress.setVisibility(View.INVISIBLE);
                    }

                    @Override
                    public void onCancelled(DatabaseError databaseError) {
                        Toast.makeText(ViewProduction.this, databaseError.getMessage(),Toast.LENGTH_SHORT).show();
                        mprogress.setVisibility(View.INVISIBLE);
                    }
                });
            }
        }
    }

答案 1 :(得分:0)

要解决此问题,请更改以下代码行:

Query query = dateRef.orderByChild("phone").equalTo("searchPhoneNumber");

Query query = dateRef.orderByChild("phone").equalTo(searchPhoneNumber);

看,没有引号吗?您需要在变量searchPhoneNumber保留的值之后而不是在searchPhoneNumber String 之后(显然不是电话号码)搜索数据库。