从Firebase获得特定价值

从Firebase获得特定价值

本文介绍了从Firebase获得特定价值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个Firebase数据库:

I have this firebase database:

已使用以下代码创建:

private DatabaseReference mDatabase;

private EditText tbfirstname;
private EditText tblastname;
private EditText tbemail;

private Button btnSubmit;

private String str_firstname;
private String str_lastname;
private String str_email;


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


    mDatabase = FirebaseDatabase.getInstance().getReference().child("Users");



    //GUI DECLARATIONS
    tbfirstname     =   (EditText) findViewById(R.id.tb_firstname);
    tblastname      =   (EditText) findViewById(R.id.tb_lastname);
    tbemail         =   (EditText) findViewById(R.id.tb_email);
    btnSubmit       =   (Button) findViewById(R.id.btn_register);


    btnSubmit.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            //HANDLES VALUES FROM TB TO STR
            str_firstname   = tbfirstname.getText().toString().trim();
            str_lastname    = tblastname.getText().toString().trim();
            str_email       = tbemail.getText().toString().trim();

            HashMap<String, String> dataMap = new HashMap<String, String>();
            dataMap.put("Firstname", str_firstname);
            dataMap.put("Lastname", str_lastname);
            dataMap.put("Email", str_email);

            mDatabase.push().setValue(dataMap).addOnCompleteListener(new OnCompleteListener<Void>() {
                @Override
                public void onComplete(@NonNull Task<Void> task) {
                    if(task.isSuccessful()){
                        Toast.makeText(MainActivity.this,"Registered Successfully!",Toast.LENGTH_LONG).show();

                        tbfirstname.setText("");
                        tblastname.setText("");
                        tbemail.setText("");
                    } else {
                        Toast.makeText(MainActivity.this, "There was an Error. Try Again!", Toast.LENGTH_LONG).show();
                    }
                }
            });


        }
    });



}

它实际上是一个简单的应用程序,可让用户注册一些数据.我想做的是创建一个search textbox,它将根据用户在该文本框中输入的内容在数据库中查找特定数据,并返回一个值.

例如,我将搜索[email protected],如果数据库中有一封电子邮件具有相同的值,我希望它返回其根值,即L4JyRA77YKldmMWM-C7.如果某种原因没有所说的记录,我希望它返回false之类的东西.

要求:
我真的是Android和Firebase的初学者,因此,如果您可以使代码对新手友好,那将是非常有用的.谢谢!

It's actually a simple app that let users register some data. What I wanna do is I want to create a search textboxthat will locate specific data in the database based on what the user has entered in that textbox and returns a value.

For example I'll search [email protected], if there is an email in the database that has the same value, I want it to return its root value namely L4JyRA77YKldmMWM-C7. If somehow there is no said record, I want it to return with false or something.

Requirements:
I'm really a beginner in Android and Firebase so if you could make the code newbie-friendly, that'll really be a great help. Thanks!

推荐答案

首先,您需要从Firebase数据库List<User>

first of all you need to fetch all records from firebase database List<User>

创建列表List<User> temp = new ArrayList();

您可以在temp中添加特定的可搜索用户详细信息-temp.add(users.get(i));

you can add particular searchable user detail in temp - temp.add(users.get(i));

现在您可以像这样email = temp.get(i).getEmailId();

Now you can get useremail like this email = temp.get(i).getEmailId();

FirebaseDatabase.getInstance().getReference().child("Your table name").orderByChild("email").equalTo(your searchable emailid ).addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        Iterator<DataSnapshot> dataSnapshots = dataSnapshot.getChildren().iterator();
        List<User> users = new ArrayList<>();
        while (dataSnapshots.hasNext()) {
            DataSnapshot dataSnapshotChild = dataSnapshots.next();
            User user = dataSnapshotChild.getValue(User.class);
            users.add(user);
        }
        String userids = "";
        List<User> temp = new ArrayList();
        try {
            for (int i = 0; i < users.size(); i++) {
                if (users.get(i).getEmailid().equals("your searchable email")) {
                    temp.add(users.get(i));
                    //Here you can find your searchable user
                    Log.e("temp", "+" + temp.get(i).getFirebaseId());
                    email = temp.get(i).getEmailId();
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            Log.e("Logs", e.toString());
        }
    }
    @Override
    public void onCancelled(DatabaseError databaseError) {

    }
});

这篇关于从Firebase获得特定价值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 13:15