0

I want to display data in ListView that logged in user stored in firebase using his mail. e.g. login user's mail is 123@mail.com and the ListView display all items in block block mean the complete details of user(s) or item(s) stored in firebase. e.g there is a book, firebase contain it's bookname, content, description, doi, author name etc.what i want is that if author is logged in only his book will show with book name, description, content and doi( book reference is just an example). what my problem is that my listview display all items about job including those belongs to other users i want user t just edit his own published jobs. So what firebase query should i write to attain this and here is my code.

firebase

Firebase

java Class

public class WorkActivity extends AppCompatActivity {

    ListView lv;
    Button ShowButton;
    // Define a String ArrayList for the job
    private ArrayList<String> jobdes=new ArrayList<>();

    // Define an ArrayAdapter for the list
    private ArrayAdapter<String> arrayAdapter;

    // Declaring String variable ( In which we are storing firebase serverURL).
    public static final String Firebase_Server_URL = "https://jobaps70.firebaseio.com/";
    public static final String Firebase_Server_URL1 = "https://jobaps70.firebaseio.com/jobs";

    // Declaring String variables to store name & phone number get from EditText.

    String PriceHolder, JobDescriptionHolder;
    FirebaseHelper helper;

    // Declaring Firebase object.
    Firebase firebase,firebase1;

    // Creating FirebaseAuth.
    FirebaseAuth firebaseAuth ;

    // Creating FirebaseAuth.
    FirebaseUser firebaseUser;

    // Creating Boolean variable that holds EditText is empty or not status.
    Boolean EditTextStatus ;


    TextView ShowDataTextView ;


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

      //        Actionbar

    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setIcon(R.drawable.logo);
    getSupportActionBar().setBackgroundDrawable(new 
    ColorDrawable(getResources().getColor(R.color.Snow)));

    ShowButton = (Button)findViewById(R.id.show);

    // Adding MainActivity context into Firebase context.
    Firebase.setAndroidContext(WorkActivity.this);

    // Passing firebase Server URL into firebase object.
    firebase = new Firebase(Firebase_Server_URL);
    firebase1=new Firebase(Firebase_Server_URL1);

    firebaseAuth = FirebaseAuth.getInstance();

    // On activity start check whether there is user previously logged in or not.
    if(firebaseAuth.getCurrentUser() == null){

        // Finishing current Profile activity.
        finish();

        // If user already not log in then Redirect to LoginActivity .
        Intent intent = new Intent(WorkActivity.this, LoginActivity.class);
        startActivity(intent);

        // Showing toast message.
        Toast.makeText(WorkActivity.this, "Please Log in to continue", 
        Toast.LENGTH_LONG).show();

    }

    // Adding firebaseAuth current user info into firebaseUser object.
    firebaseUser = firebaseAuth.getCurrentUser();

    // Getting logged in user email from firebaseUser.getEmail() method and set into TextView.
    setTitle( firebaseUser.getEmail());
    setTitleColor(R.color.DarkGray);
    firebase.child("jobs");

    // Associate the jobs' list with the corresponding ListView
    lv = (ListView) findViewById(R.id.lvshow);

    // Set the ArrayAdapter to the ListView
    arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, jobdes);
    lv.setAdapter(arrayAdapter);

    // Attach a ChildEventListener to the teacher database, so we can retrieve the job entries
    firebase.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {

            // Get the value from the DataSnapshot and add it to the jobs' list
            JobDetails jo = (JobDetails) dataSnapshot.getValue(JobDetails.class);
            String joString = String.valueOf(jo);
            arrayAdapter.add(joString);
 //

            // Notify the ArrayAdapter that there was a change
            arrayAdapter.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(DataSnapshot dataSnapshot, String s) {

        }

        @Override
        public void onCancelled(FirebaseError databaseError) {

        }
    });
}

}

JobDetail

 public class JobDetails {

    private String price;
    private String jobDescription;
    private String jobSpinner;
    public String email;
    public JobDetails() {
        // This is default constructor.
    }

    public String getPrice() {

        return price;
    }

    public void setPrice(String pri) {

        this.price = pri;
    }

    public String getjobDescription() {

        return jobDescription;

    }

    public void setjobDescription(String jobd) {

        this.jobDescription = jobd;
    }

    public void setjob(String jry) {

        this.jobSpinner = jry ;

    }

    public String getjob() {

        return jobSpinner;

    }

    public void setemail(String jel) {

        this.email = jel ;

    }

    public String getemail() {

        return email;

    }
   @Override
   public String toString() {
    return this.jobSpinner + ": " + this.jobDescription+ ": " +price;
}

}

XML

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.gumption.zeeshanahmed.jobaps.WorkActivity">
<TextView
    android:id="@+id/textdispjobs"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:text="@string/text_display_jobs"
    android:textSize="40sp"
    android:textAlignment="center"/>

<ListView
    android:id="@+id/lvshow"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_alignParentStart="true"
    android:layout_below="@+id/show" />


</RelativeLayout>

Currently it is displaying items from other users as well. It would be very helpful if someone could help me with this. Thank you!

1 Answers1

0

Using the getEmail() function with Firebase only returns the email if you set the email when the user first created their account. To set their email, you must do this:

FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
user.updateEmail("user@example.com");

If you want more on this, this is where I got this code from

Then, you can use the getEmail function. Otherwise, if you want to get the email from the JobDetails class, get the email in your childEventListener:

JobDetails jo = (JobDetails) dataSnapshot.getValue(JobDetails.class);
//This getEmail function is provided in the JobDetails class
String email = jo.getEmail();

EDIT:

Since you are wanting to get only the job that the current user has displayed/added, you will need to first get the user's email. Assuming that you set the user's information in the database based on their Uid, you can do something like this to get the current user:

private User curr_user;
private String mUserId;

mUserId = FirebaseAuth.getInstance.getUid();
DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference();


//Replace this path to wherever you stored your users
mDatabase.child("users").child(mUserId).addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            curr_user = dataSnapshot.getValue(User.class);
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

Now, you can loop through the children of jobs to get the jobs posted by the current user's email:

firebase.addChildEventListener(new ChildEventListener() {
    @Override
    public void onChildAdded(DataSnapshot dataSnapshot, String s) {

        // Get the value from the DataSnapshot and add it to the jobs' list
        JobDetails jo = (JobDetails) dataSnapshot.getValue(JobDetails.class);
        String joString = String.valueOf(jo);

        if (jo.getEmail() == curr_user.getEmail()) {
            arrayAdapter.add(joString);
        }

        // Notify the ArrayAdapter that there was a change
        arrayAdapter.notifyDataSetChanged();
    }

    @Override
    public void onChildChanged(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onChildRemoved(DataSnapshot dataSnapshot) {

    }

    @Override
    public void onChildMoved(DataSnapshot dataSnapshot, String s) {

    }

    @Override
    public void onCancelled(FirebaseError databaseError) {

    }
});
  • String email = jo.getEmail(); it only display emails from the block, i want to display the whole block's contents – Zeeshan Ahmad Dec 18 '17 at 09:02
  • Well in that case, you probably want to create a custom adapter that accepts the `jo` object in the constructor, then populates `TextView`s based on what you want. If you want more help for creating a ListView, check out this link: https://stackoverflow.com/questions/8166497/custom-adapter-for-list-view – Matthew Vanlandingham Dec 18 '17 at 09:06
  • the thing i wanted to display are displaying from data base my question is how to retrieve/display the blocks that current user stored. for example like in stack you can view the list of questions you wrote in activity bar in user profile! – Zeeshan Ahmad Dec 18 '17 at 10:19
  • I'm not really sure what you mean by block. If you mean the data that is stored by the JobDetails class, then your object `jo` contains all of that data. Just call your get methods on that object to retrieve it all. – Matthew Vanlandingham Dec 18 '17 at 19:33
  • block mean the complete details of user(s) or item(s) stored in firebase. e.g there is a book firebase contain it's bookname, content, description, doi, author name etc. – Zeeshan Ahmad Dec 19 '17 at 05:27
  • what i want is that if author is logged in only his book will show with book name, description, content and doi( this is just a reference). what my problem is that my display all items including those belongs to other users. – Zeeshan Ahmad Dec 19 '17 at 05:33
  • Ah okay. I can help you out with that. It makes more sense now. Could you post a screenshot of your database? – Matthew Vanlandingham Dec 19 '17 at 05:59
  • I'm confused again. Your screenshot of your database and your code are about jobs, but you're having issues retrieving a book in Firebase? I cannot help you if you don't give me the tools to do so. If you are having issues retrieving book details from Firebase, give the code to do so. If you are having issues retrieving job details from Firebase, rewrite your question in terms of the Job details. – Matthew Vanlandingham Dec 19 '17 at 06:52
  • sorry! i gave books as a reference/ example. and i updated my question – Zeeshan Ahmad Dec 19 '17 at 10:33
  • Okay I have updated my answer. Let me know if this helps or if you have anymore questions! – Matthew Vanlandingham Dec 19 '17 at 22:58