【问题标题】:How to do a search a data in my firebase database [Android]如何在我的 firebase 数据库中搜索数据 [Android]
【发布时间】:2017-06-30 09:17:57
【问题描述】:

我想用 editText 做一个“搜索栏”,应用程序的目标是用户输入一个单词,然后我检查我的 firebase 数据库是否存在它的单词并且我是否在文本视图中显示它.

我的数据库:enter image description here

和我的代码(我想比较用户的输入和字段“nomP”的结果):

public class recherche extends AppCompatActivity {
    DatabaseReference bdd_Produit;
    List<Produit> lesP;
    EditText edtRecherche;
    TextView txtResultat;
    Button btnCherche;
    Button acc;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_recherche);
        edtRecherche = (EditText) findViewById(R.id.edtRecherche);
        btnCherche = (Button) findViewById(R.id.btnCherche);
        acc = (Button) findViewById(R.id.btnAcc);

        bdd_Produit = FirebaseDatabase.getInstance().getReference("produit");

        acc.setText("Accueil");
        btnCherche.setText("valide");

        //initialisation var
        lesP = new ArrayList<>();

        btnCherche.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
            bdd_Produit.orderByChild("nomP").equalTo(edtRecherche.getText().toString()).addListenerForSingleValueEvent(
                    new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            //data will be available on dataSnapshot.getValue();
                            txtResultat.setText(dataSnapshot.getValue().toString());
                        }

                        @Override
                        public void onCancelled(DatabaseError databaseError) {

                        }
                    });
        }
    });

    }

提前感谢您的帮助,如果您需要我忘记的其他信息,我在这里! :)

【问题讨论】:

  • 问题在哪里,你到底需要什么
  • 我需要在我的数据库中搜索用户的输入,如果输入存在我想在文本视图中显示它

标签: android firebase firebase-realtime-database


【解决方案1】:

然后您需要为 Produit 创建模型类

bdd_Produit.addListenerForSingleValueEvent(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        for (DataSnapshot snapshot : dataSnapshot.getChildren()) {
            if (!TextUtils.isEmpty(edtRecherche.getText())){
                if (snapshot.getValue(Produit.class).getNomP().equals(edtRecherche.getText().toString()))
                    //produit founded
                else
            }
        }
    }

    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

【讨论】:

    猜你喜欢
    • 2018-02-06
    • 1970-01-01
    • 2021-11-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多