Reputation: 15
I've been programming this part for a whole week, and couldn't find a solution. Firebase database looks like: Firebase database structure
FIREBASE -> Students (Database) ->
{0}
|- ISIC: 1234567
|- name: "Vladimir"
|- password: "vlad"
|- indeks: "111/11"
|- meals
|-{0} - amount: 15
| name: breakfast
|-{1} - amount: 2
| name: lunch
{1}
|- ISIC: 98765
|- name: "John"
|- password: "doe"
|- indeks: 999/99
|- meals
|-{0} - amount: 18
| name: breakfast
|-{1} - amount: 20
| name: lunch
,...,
{n} (nodes)
|- (Data)
|- (Data)
When I click the button for Login, where Username is indeks and password is typed, I would like first to query the database to find the Student which has that indeks (by username) and check if the password of that student is exactly like in database.
My code looks like:
EditText userName;
EditText pass;
Button logIn;
DatabaseReference dbStudents;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
userName = (EditText) findViewById(R.id.username);
pass = (EditText) findViewById(R.id.password);
logIn = (Button) findViewById(R.id.login);
dbStudents =
FirebaseDatabase.getInstance().getReference("students");
logIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//some code which I don't know
}
});
}
My trouble is how to retrieve the entire object Student from Firebase and use it after the login is successful.
Have been using: Query, addListenerForSingleValueEvent and orderBy().getData()
, but couldn't my problem of finding the value by entered String, and retrieving as an object Student.
THANKS IN ADVANCE! :)
UPDATE!!
public void getKey() {
dbStudents = FirebaseDatabase.getInstance().getReference("students");
dbStudents.orderByChild("indeks").equalTo(userName.getText().toString())
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
key = snapshot.getKey();
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
With this I can get the key of node (student) which contains "indeks" value (entered as username), but now I need to get retrieve as object Student using the key (String) from Firebase Database. How can I do it?
UPDATE 2!!
Meal class:
public class Meal {
private int amount;
private String name;
public Meal() {
}
public Meal(int amount, String name) {
this.amount = amount;
this.name = name;
}
public int getAmount() {
return amount;
}
public String getName() {
return name;
}
public void setAmount(int amount) {
this.amount = amount;
}
public void setName(String name) {
this.name = name;
}
@Override
public String toString() {
return "Meal{" +
"amount=" + amount +
", name='" + name + '\'' +
'}';
}
}
Student class:
public class Student {
private int ISIC;
private String born;
private String contact;
private String description;
private String imagePath;
private String indeks;
private String issued;
private Meal[] meals;
private String name;
private String password;
private String studies;
private String validity;
public Student() {
}
public Student(int ISIC, String born, String contact, String description, String imagePath,
String indeks, String issued, Meal[] meals, String name, String password,
String studies, String validity) {
this.ISIC = ISIC;
this.born = born;
this.contact = contact;
this.description = description;
this.imagePath = imagePath;
this.indeks = indeks;
this.issued = issued;
this.meals = meals;
this.name = name;
this.password = password;
this.studies = studies;
this.validity = validity;
}
public int getISIC() {
return ISIC;
}
public String getBorn() {
return born;
}
public String getContact() {
return contact;
}
public String getDescription() {
return description;
}
public String getImagePath() {
return imagePath;
}
public String getIndeks() {
return indeks;
}
public String getIssued() {
return issued;
}
public Meal[] getMeals() {
return meals;
}
public String getName() {
return name;
}
public String getPassword() {
return password;
}
public String getStudies() {
return studies;
}
public String getValidity() {
return validity;
}
@Override
public String toString() {
return "Student{" +
"ISIC=" + ISIC +
", born='" + born + '\'' +
", contact='" + contact + '\'' +
", description='" + description + '\'' +
", imagePath='" + imagePath + '\'' +
", indeks='" + indeks + '\'' +
", issued='" + issued + '\'' +
", meals=" + meals +
", name='" + name + '\'' +
", password='" + password + '\'' +
", studies='" + studies + '\'' +
", validity='" + validity + '\'' +
'}';
}
public void setISIC(int ISIC) {
this.ISIC = ISIC;
}
public void setBorn(String born) {
this.born = born;
}
public void setContact(String contact) {
this.contact = contact;
}
public void setDescription(String description) {
this.description = description;
}
public void setImagePath(String imagePath) {
this.imagePath = imagePath;
}
public void setIndeks(String indeks) {
this.indeks = indeks;
}
public void setIssued(String issued) {
this.issued = issued;
}
public void setMeals(Meal[] meals) {
this.meals = meals;
}
public void setName(String name) {
this.name = name;
}
public void setPassword(String password) {
this.password = password;
}
public void setStudies(String studies) {
this.studies = studies;
}
public void setValidity(String validity) {
this.validity = validity;
}
}
Upvotes: 1
Views: 961
Reputation: 13129
You need to do this approach in order to get each user password, but instead of saving 0, 1, 2 , 3 i would suggest saving userID as main node for each user, so you can get from the user you need the password , this method instead will get you all the passwords from all the users, replace the autogenerated 0 , 1 , 2 ,3 with mAuth.getCurrentUser().getUid();
logIn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String uid = mAuth.getCurrentUser().getUid();
mRef.child("students").child(uid).addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try {
for(DataSnapshot snap : dataSnapshot.getChildren())
YourPojoClass obj = snap.getValue(YourPojoClass.class);
String password =obj.getPassword();
//here compare your local password inserted in your editText with the one pulled from firebase
}
}catch (Exception ex){
//error
}
}
@Override
public void onCancelled(DatabaseError error) {
//cancelled
}
});
}
remember, YourPojoClass.class will be a class with setters and getters wiht the exact name of your database variables
Exactly this is YourPojo.class
public class Student {
private int ISIC;
private String indeks;
private Meal[] meals;
private String name;
private String password;
}
just remember to do setters and getters for it !
UPDATE !
So , now since you get that key you can access to the data inside it
public void getKey() {
dbStudents = FirebaseDatabase.getInstance().getReference("students");
dbStudents.orderByChild("indeks").equalTo(userName.getText().toString())
.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for(DataSnapshot snapshot : dataSnapshot.getChildren()){
key = snapshot.getKey();
Student std = snapshot.getValue(Student.class);
String password = std.getPassword();
String name = std.getName();
//... And so on with the other data
}
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
Upvotes: 1