java–Firebase实时数据库在查询之间按字搜索?

java–Firebase实时数据库在查询之间按字搜索?,第1张

概述在我的应用程序中,我在Firebase中有一个包含标题,图像,描述,搜索的实时数据库(一个额外的字段,未在应用程序中显示但使用的包含带有小写字母的标题文本,以使搜索不区分大小写).目前,当我以小的情况或大写或两者的混合方式对同一类型进行查询时,它搜索完美,但是当我在查询之间键入一

在我的应用程序中,我在Firebase中有一个包含标题,图像,描述,搜索的实时数据库(一个额外的字段,未在应用程序中显示但使用的包含带有小写字母的标题文本,以使搜索不区分大小写).目前,当我以小的情况或大写或两者的混合方式对同一类型进行查询时,它搜索完美,但是当我在查询之间键入一些单词时它不起作用例如,如果查询是“这是长的”标题“当我输入”长“它没有显示任何内容.我帮他解决了这个问题.

我用过firebase ui库:

implementation 'com.firebaseui:firebase-ui-database:4.2.0'

实时数据库截图:


这是我的活动代码:

package com.blogspot.atifsoftwares.firebaseproject;import androID.content.DialogInterface;import androID.content.Intent;import androID.content.SharedPreferences;import androID.graphics.Bitmap;import androID.graphics.drawable.BitmapDrawable;import androID.graphics.drawable.Drawable;import androID.support.annotation.NonNull;import androID.support.v4.vIEw.MenuItemCompat;import androID.support.v7.app.Actionbar;import androID.support.v7.app.AlertDialog;import androID.support.v7.app.AppCompatActivity;import androID.os.Bundle;import androID.support.v7.Widget.linearlayoutmanager;import androID.support.v7.Widget.RecyclerVIEw;import androID.support.v7.Widget.SearchVIEw;import androID.vIEw.LayoutInflater;import androID.vIEw.Menu;import androID.vIEw.MenuItem;import androID.vIEw.VIEw;import androID.vIEw.VIEwGroup;import androID.Widget.ImageVIEw;import androID.Widget.TextVIEw;import com.firebase.ui.database.FirebaseRecyclerAdapter;import com.firebase.ui.database.FirebaseRecyclerOptions;import com.Google.firebase.database.DatabaseReference;import com.Google.firebase.database.FirebaseDatabase;import com.Google.firebase.database.query;import java.io.ByteArrayOutputStream;public class postsListActivity extends AppCompatActivity {    linearlayoutmanager mLayoutManager; //for sorting    SharedPreferences mSharedPref; //for saving sort settings    RecyclerVIEw mRecyclerVIEw;    FirebaseDatabase mFirebaseDatabase;    DatabaseReference mRef;    FirebaseRecyclerAdapter<Model, VIEwHolder> firebaseRecyclerAdapter;    FirebaseRecyclerOptions<Model> options;    @OverrIDe    protected voID onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentVIEw(R.layout.activity_posts_List);        //Actionbar        Actionbar actionbar = getSupportActionbar();        //set Title        mSharedPref = getSharedPreferences("SortSettings", MODE_PRIVATE);        String mSorting = mSharedPref.getString("Sort", "newest"); //where if no settingsis selected newest will be default        if (mSorting.equals("newest")) {            mLayoutManager = new linearlayoutmanager(this);            //this will load the items from bottom means newest first            mLayoutManager.setReverseLayout(true);            mLayoutManager.setStackFromEnd(true);        } else if (mSorting.equals("oldest")) {            mLayoutManager = new linearlayoutmanager(this);            //this will load the items from bottom means oldest first            mLayoutManager.setReverseLayout(false);            mLayoutManager.setStackFromEnd(false);        }        //RecyclerVIEw        mRecyclerVIEw = findVIEwByID(R.ID.recyclerVIEw);        mRecyclerVIEw.setHasFixedSize(true);        //send query to FirebaseDatabase        mFirebaseDatabase = FirebaseDatabase.getInstance();        mRef = mFirebaseDatabase.getReference("Data");        showData();    }    //show data    private voID showData(){        options = new FirebaseRecyclerOptions.Builder<Model>().setquery(mRef, Model.class).build();        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Model, VIEwHolder>(options) {            @OverrIDe            protected voID onBindVIEwHolder(@NonNull VIEwHolder holder, int position, @NonNull Model model) {                holder.setDetails(getApplicationContext(), model.getTitle(), model.getDescription(), model.getimage());            }            @NonNull            @OverrIDe            public VIEwHolder onCreateVIEwHolder(@NonNull VIEwGroup parent, int vIEwType) {                //Inflating layout row.xml                VIEw itemVIEw = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);                VIEwHolder vIEwHolder = new VIEwHolder(itemVIEw);                //item click Listener                vIEwHolder.setonClickListener(new VIEwHolder.ClickListener() {                    @OverrIDe                    public voID onItemClick(VIEw vIEw, int position) {                        //get data from firebase at the position clicked                        String mTitle = getItem(position).getTitle();                        String mDesc = getItem(position).getDescription();                        String mImage = getItem(position).getimage();                        //pass this data to new activity                        Intent intent = new Intent(vIEw.getContext(), PostDetailActivity.class);                        intent.putExtra("Title", mTitle); // put Title                        intent.putExtra("description", mDesc); //put description                        intent.putExtra("image", mImage); //put image url                        startActivity(intent); //start activity                    }                    @OverrIDe                    public voID onItemLongClick(VIEw vIEw, int position) {                        //Todo implement you on long click functionality here                    }                });                return vIEwHolder;            }        };        //set layout as linearLayout        mRecyclerVIEw.setLayoutManager(mLayoutManager);        firebaseRecyclerAdapter.startListening();        //set adapter to firebase recycler vIEw        mRecyclerVIEw.setAdapter(firebaseRecyclerAdapter);    }    //search data    private voID firebaseSearch(String searchText) {        //convert string entered in SearchVIEw to lowercase        String query = searchText.tolowerCase();        query firebaseSearchquery = mRef.orderByChild("search").startAt(query).endAt(query + "\uf8ff");        options = new FirebaseRecyclerOptions.Builder<Model>().setquery(firebaseSearchquery, Model.class).build();        firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Model, VIEwHolder>(options) {            @OverrIDe            protected voID onBindVIEwHolder(@NonNull VIEwHolder holder, int position, @NonNull Model model) {                holder.setDetails(getApplicationContext(), model.getTitle(), model.getDescription(), model.getimage());            }            @NonNull            @OverrIDe            public VIEwHolder onCreateVIEwHolder(@NonNull VIEwGroup parent, int vIEwType) {                //Inflating layout row.xml                VIEw itemVIEw = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, parent, false);                VIEwHolder vIEwHolder = new VIEwHolder(itemVIEw);                //item click Listener                vIEwHolder.setonClickListener(new VIEwHolder.ClickListener() {                    @OverrIDe                    public voID onItemClick(VIEw vIEw, int position) {                        //get data from firebase at the position clicked                        String mTitle = getItem(position).getTitle();                        String mDesc = getItem(position).getDescription();                        String mImage = getItem(position).getimage();                        //pass this data to new activity                        Intent intent = new Intent(vIEw.getContext(), PostDetailActivity.class);                        intent.putExtra("Title", mTitle); // put Title                        intent.putExtra("description", mDesc); //put description                        intent.putExtra("image", mImage); //put image url                        startActivity(intent); //start activity                    }                    @OverrIDe                    public voID onItemLongClick(VIEw vIEw, int position) {                        //Todo implement you on long click functionality here                    }                });                return vIEwHolder;            }        };        //set layout as linearLayout        mRecyclerVIEw.setLayoutManager(mLayoutManager);        firebaseRecyclerAdapter.startListening();        //set adapter to firebase recycler vIEw        mRecyclerVIEw.setAdapter(firebaseRecyclerAdapter);    }    //load data into recycler vIEw onStart    @OverrIDe    protected voID onStart() {        super.onStart();        if (firebaseRecyclerAdapter !=null){            firebaseRecyclerAdapter.startListening();        }    }    @OverrIDe    public boolean onCreateOptionsMenu(Menu menu) {        //inflate the menu; this adds items to the action bar if it present        getMenuInflater().inflate(R.menu.menu, menu);        MenuItem item = menu.findItem(R.ID.action_search);        SearchVIEw searchVIEw = (SearchVIEw) MenuItemCompat.getActionVIEw(item);        searchVIEw.setonqueryTextListener(new SearchVIEw.OnqueryTextListener() {            @OverrIDe            public boolean onqueryTextsubmit(String query) {                firebaseSearch(query);                return false;            }            @OverrIDe            public boolean onqueryTextChange(String newText) {                //Filter as you type                firebaseSearch(newText);                return false;            }        });        return super.onCreateOptionsMenu(menu);    }    @OverrIDe    public boolean onoptionsItemSelected(MenuItem item) {        int ID = item.getItemID();        //handle other action bar item clicks here        if (ID == R.ID.action_sort) {            //display alert dialog to choose sorting            showSortDialog();            return true;        }        if (ID == R.ID.action_add) {            //start Add Post Activity            startActivity(new Intent(postsListActivity.this, AddPostActivity.class));            return true;        }        return super.onoptionsItemSelected(item);    }    private voID showSortDialog() {        //options to display in dialog        String[] sortoptions = {" Newest", " oldest"};        //create alert dialog        AlertDialog.Builder builder = new AlertDialog.Builder(this);        builder.setTitle("Sort by") //set Title                .setIcon(R.drawable.ic_action_sort) //set icon                .setItems(sortoptions, new DialogInterface.OnClickListener() {                    @OverrIDe                    public voID onClick(DialogInterface dialog, int which) {                        // The 'which' argument contains the index position of the selected item                        // 0 means "Newest" and 1 means "oldest"                        if (which == 0) {                            //sort by newest                            //Edit our shared preferences                            SharedPreferences.Editor editor = mSharedPref.edit();                            editor.putString("Sort", "newest"); //where 'Sort' is key & 'newest' is value                            editor.apply(); // apply/save the value in our shared preferences                            recreate(); //restart activity to take effect                        } else if (which == 1) {                            {                                //sort by oldest                                //Edit our shared preferences                                SharedPreferences.Editor editor = mSharedPref.edit();                                editor.putString("Sort", "oldest"); //where 'Sort' is key & 'oldest' is value                                editor.apply(); // apply/save the value in our shared preferences                                recreate(); //restart activity to take effect                            }                        }                    }                });        builder.show();    }}

解决方法:

when I type some word that is in between the query it doesn’t work

这是因为Firebase不支持本机索引或搜索数据库属性中的文本字段.另外,下载整个节点以搜索客户端的字段根本不实用.要启用Firebase实时数据库数据的全文搜索,建议您使用第三方搜索服务,如Algolia或Elasticsearch.

This是关于它如何与Cloud Firestore一起使用的示例,但您可以将其与Firebase实时数据库一起使用.

总结

以上是内存溢出为你收集整理的java – Firebase实时数据库在查询之间按字搜索?全部内容,希望文章能够帮你解决java – Firebase实时数据库在查询之间按字搜索?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1118568.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存