Reputation: 121
I have an app that offers odd jobs to people. There is a section where one can see the jobs available to him near him. When the user clicks this option, jobs available in his location should show up. I have a firebase database with two nodes, Job and Users in which the user's location and job's location are added. How do I display jobs available in his location? For example, the user lives in Dubai, and he wants to see the jobs available in Dubai. I want to be able to extract the current user's location and check if it matches with the location of the job. This is what I have tried but it doesn't work.
Home_NearYou.java:
package com.example.oddsynew;
import android.os.Bundle;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.Query;
import com.google.firebase.database.ValueEventListener;
import java.util.ArrayList;
public class Home_NearYou extends AppCompatActivity {
DatabaseReference myRef;
FirebaseAuth mAuth;
RecyclerView nearYouList;
ArrayList<Job> list;
HomeAdapter adapter;
Query query;
Users user;
FirebaseUser u;
String userID;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home__near_you);
try {
mAuth = FirebaseAuth.getInstance();
u = mAuth.getCurrentUser();
userID = u.getUid();
myRef = FirebaseDatabase.getInstance().getReference("Users").child(userID).child("state");
nearYouList = (RecyclerView) findViewById(R.id.nearYou);
nearYouList.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<Job>();
adapter = new HomeAdapter(Home_NearYou.this, list);
nearYouList.setAdapter(adapter);
query = FirebaseDatabase.getInstance().getReference("Jobs")
.orderByChild("location")
.equalTo(myRef.toString());
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot ds : dataSnapshot.getChildren()) {
Job j = ds.getValue(Job.class);
list.add(j);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(Home_NearYou.this, "Error", Toast.LENGTH_SHORT).show();
}
});
}catch (Exception e){
e.printStackTrace();
}
}
}
Job.java:
package com.example.oddsynew;
public class Job {
String job_name, recruiter_name, location, job_charge, add_pref, start_date, end_date, start_time, end_time, tasks, job_desc, prof_pic, job_tasks;
public Job() {
}
public Job(String job_name, String recruiter_name, String location, String job_charge, String add_pref, String start_date,
String end_date, String start_time, String end_time, String tasks, String job_desc, String prof_pic, String job_tasks) {
this.job_name = job_name;
this.recruiter_name = recruiter_name;
this.location = location;
this.job_charge = job_charge;
this.add_pref = add_pref;
this.start_date = start_date;
this.end_date = end_date;
this.start_time = start_time;
this.end_time = end_time;
this.tasks = tasks;
this.job_desc = job_desc;
this.prof_pic = prof_pic;
this.job_tasks = job_tasks;
}
public Job(String prof_pic) {
this.prof_pic = prof_pic;
}
public String getJob_name() {
return job_name;
}
public void setJob_name(String job_name) {
this.job_name = job_name;
}
public String getRecruiter_name() {
return recruiter_name;
}
public void setRecruiter_name(String recruiter_name) {
this.recruiter_name = recruiter_name;
}
public String getLocation() {
return location;
}
public void setLocation(String location) {
this.location = location;
}
public String getJob_charge() {
return job_charge;
}
public void setJob_charge(String job_charge) {
this.job_charge = job_charge;
}
public String getProf_pic() {
return prof_pic;
}
public void setProf_pic(String prof_pic) {
this.prof_pic = prof_pic;
}
public String getAdd_pref() {
return add_pref;
}
public void setAdd_pref(String add_pref) {
this.add_pref = add_pref;
}
public String getStart_time() {
return start_time;
}
public void setStart_time(String start_time) {
this.start_time = start_time;
}
public String getEnd_time() {
return end_time;
}
public String getStart_date() {
return start_date;
}
public void setStart_date(String start_date) {
this.start_date = start_date;
}
public String getEnd_date() {
return end_date;
}
public void setEnd_date(String end_date) {
this.end_date = end_date;
}
public void setEnd_time(String end_time) {
this.end_time = end_time;
}
public String getTasks() {
return tasks;
}
public void setTasks(String tasks) {
this.tasks = tasks;
}
public String getJob_desc() {
return job_desc;
}
public void setJob_desc(String job_desc) {
this.job_desc = job_desc;
}
public String getJob_tasks() {
return job_tasks;
}
public void setJob_tasks(String job_tasks) {
this.job_tasks = job_tasks;
}
}
Users.java:
package com.example.oddsynew;
public class Users {
private String fname;
private String lname;
private String email;
private String pass;
private String state;
private String city;
private String age;
public Users() {
}
public String getFname() {
return fname;
}
public String getLname() {
return lname;
}
public String getEmail() {
return email;
}
public String getPass() {
return pass;
}
public void setFname(String fname) {
this.fname = fname;
}
public void setLname(String lname) {
this.lname = lname;
}
public void setEmail(String email) {
this.email = email;
}
public void setPass(String pass) {
this.pass = pass;
}
public String getState() {
return state;
}
public void setState(String state) {
this.state = state;
}
public String getCity() {
return city;
}
public void setCity(String city) {
this.city = city;
}
public String getAge() {
return age;
}
public void setAge(String age) {
this.age = age;
}
}
Job Database:
User Database:
UPDATE: This is an example of how I insert data into Firebase.
SignUp2.java:
package com.example.oddsynew;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
public class SignUp2 extends AppCompatActivity {
EditText state, city, age;
Button submitBtn;
FirebaseAuth mAuth;
DatabaseReference myRef;
Users user;
String firstName, lastName, _email, _pass, state_, city_, age_;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sign_up2);
state = (EditText) findViewById(R.id.state);
city = (EditText) findViewById(R.id.city);
age = (EditText) findViewById(R.id.age);
submitBtn = (Button) findViewById(R.id.submitBtn);
user = new Users();
myRef = FirebaseDatabase.getInstance().getReference().child("Users");
mAuth = FirebaseAuth.getInstance();
/*if(mAuth.getCurrentUser() != null){
startActivity(new Intent(getApplicationContext(), Home.class));
Toast.makeText(SignUp2.this, "Welcome Back",Toast.LENGTH_LONG).show();
finish();
}*/
submitBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
firstName = getIntent().getStringExtra("fName");
lastName = getIntent().getStringExtra("lName");
_email = getIntent().getStringExtra("email");
_pass = getIntent().getStringExtra("pass");
state_ = state.getText().toString().trim();
city_ = city.getText().toString().trim();
age_ = age.getText().toString();
user.setFname(firstName);
user.setLname(lastName);
user.setEmail(_email);
user.setPass(_pass);
user.setState(state_);
user.setCity(city_);
user.setAge(age_);
if(state_.matches("") || city_.matches("") || age_.matches("")){
Toast.makeText(SignUp2.this, "Please fill all fields",Toast.LENGTH_LONG).show();
}
else{
myRef.push().setValue(user);
Toast.makeText(SignUp2.this, "Data Inserted",Toast.LENGTH_LONG).show();
}
mAuth.createUserWithEmailAndPassword(_email, _pass).addOnCompleteListener(new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
if(task.isSuccessful()){
Toast.makeText(SignUp2.this, "User Created",Toast.LENGTH_LONG).show();
startActivity(new Intent(getApplicationContext(), Home.class));
}else{
Toast.makeText(SignUp2.this, "Error",Toast.LENGTH_LONG).show();
}
}
});
}
});
}
}
Upvotes: 0
Views: 103
Reputation: 80914
In your Users
node you are not using the userID
, those ids in the picture are ids generated by the push()
method. Therefore first change the structure to use userID
instead of push()
, then you can do this:
mAuth = FirebaseAuth.getInstance();
u = mAuth.getCurrentUser();
userID = u.getUid();
myRef = FirebaseDatabase.getInstance().getReference("Users").child(userID);
myRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
String state = dataSnapshot.child("state").getValue(String.class);
query = FirebaseDatabase.getInstance().getReference("Jobs")
.orderByChild("location")
.equalTo(state);
query.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
if(dataSnapshot.exists()){
//do whatever you want
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(Home_NearYou.this, "Error", Toast.LENGTH_SHORT).show();
}
});
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(Home_NearYou.this, "Error", Toast.LENGTH_SHORT).show();
}
});
So first retrieve the state
from the user node. The only way to retrieve data is to use addValueEventListener
. After retrieving the state
, perform a different query for the jobs
node that checks if the location
is equal to the state
retrieved, attach a valueEventListener
and use the method exists()
to check if you get any data from that query.
Upvotes: 1