WLDN
WLDN

Reputation: 1

How to retreive specific rows from Firebase?

I'm new to Firebase, so I've created a ListView that will display all the data from my database.

Wwhat I want is when I click any row from the list, then it'll open new activity with only the data that I've clicked. Is that possible?

Here is my code:

listView = findViewById(R.id.listView);
ArrayList<String> list = new ArrayList<>();
ArrayAdapter adapter = new ArrayAdapter<String>(this, R.layout.list_job, list);
listView.setAdapter(adapter);

DatabaseReference reference = FirebaseDatabase.getInstance().getReference().child("postJobs");
reference.addValueEventListener(new ValueEventListener() {
    @Override
    public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
        list.clear();
        for(DataSnapshot snapshot : dataSnapshot.getChildren()){
            PostJob postJob = snapshot.getValue(PostJob.class);
            String header = postJob.getSpecial() + " : " + postJob.getCompName();
            String position = "\n" + postJob.getPosition();
            String location = "\n" + postJob.getLocation();
            String salary = "\nRM" + postJob.getSalaryMin() + " - RM" + postJob.getSalaryMax();
            list.add(header + position + location + salary);
        }
        adapter.notifyDataSetChanged();

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {

                //Open Apply Job
                switchToViewJob();
                overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
            }
        });
    }

    @Override
    public void onCancelled(@NonNull DatabaseError databaseError) {

    }
});

My Database:

My Database

Upvotes: 0

Views: 103

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 599641

The int i argument that is passed to your onItemClick implementation is the position of the item that was clicked. So what you'll need to do is keep a mapping from the position of each item in the list to the information you want to pass to the activity, for example by keeping an array/list of all child DataSnapshot objects, and then looking up the object by its position/index in that list.

Upvotes: 1

Related Questions