2

I'm working with firebase, I can get data from firebase with this code

String value1 = "Jack"
DatabaseReference data = FirebaseDatabase.getInstance().getReference().child("users");
Query personsQuery = data.orderByChild("desc").equalTo(value1);

one value works well, how can I get data with array value like this

String value1[] = {"Larry", "Moe", "Curly"};

Edit:

this code lists all names

    mPeopleRVAdapter = new FirebaseRecyclerAdapter<Productget, NewsViewHolder>(personsOptions) {
        @Override
        protected void onBindViewHolder(Product.NewsViewHolder holder, final int position, final Productget model) {
            holder.setTitle(model.getTitle());

        }

        @Override
        public Product.NewsViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

            View view = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.product_row, parent, false);

            return new Product.NewsViewHolder(view);
        }
    };

    mPeopleRV.setAdapter(mPeopleRVAdapter);

Can I filter this code for Larry, Moe and Curly?

ReyAnthonyRenacia
  • 17,219
  • 5
  • 37
  • 56
EagleH
  • 107
  • 2
  • 8

2 Answers2

4

You can try to get your data with a snapshot and store it into a map like this

Map<String, String> map = (Map<String, String>) dataSnapshot.getValue();

check this answer

Alex Mamo
  • 130,605
  • 17
  • 163
  • 193
  • I could not, can you help me a little more? – EagleH Jul 23 '18 at 22:14
  • Hello, in case you still have issues with reading the data, can you please be more specific about the issue, also check this : https://firebase.google.com/docs/database/android/read-and-write?authuser=0#basic_write) , about the filtering when you are creating your Firebase RecyclerAdapter you can set your model (in case you have one), your view, your holder and your filtering like this ref.orderByChild("group").equalTo(groupid), check this answer : https://stackoverflow.com/questions/41602568/filter-query-to-populate-firebaserecycleradapter – Jose De Jesus Avila Gomez Jul 25 '18 at 15:38
0

Firebase Realtime Database won't be able to do that out of the box, you may need to structure your data differently in other to get desired results, see https://firebase.google.com/docs/database/android/structure-data#best_practices_for_data_structure

For instance you could do composite keys (if you are trying to filter on different childs/fields at once). Also you could try to loop though those different conditions ("Larry", "Moe", "Curly"), download the entire parent node and filter your self; depends on what you are trying to achieve.

If you are ok to Fetch all data (all users), then you could filter by name using a HashMap for example:

First Create and populate the HashMap:

HashMap<Stirng, User> hashmap = new HashMap<>();
ValueEventListener userListener = new ValueEventListener() {
    @Override
    public void onDataChange(DataSnapshot dataSnapshot) {
        User user = dataSnapshot.getValue(User.class);
        hashmap.put(user.getName(), user);
    }
};
mPostReference.addValueEventListener(userListener);

Then on your adapter you could filter on your data:

//String value1[] = {"Larry", "Moe", "Curly"};
ArrayList<User> fullNameList = new ArrayList<>(); //filtered users array list
for (int i = 0; i<value1.length; i++){
    if(hashmap.containsKey(value1[i])){
        fullNameList.add(hashmap.get(value1[i]));
    }
}

Firestore on the other hand does support more complex queries:

https://firebase.google.com/docs/firestore/query-data/queries#compound_queries

memogarrido
  • 323
  • 1
  • 7
  • Your problem is filtering data or getting specific fields from the database?... If you want to filter it then you could download all users and filter them in the app see for example this answer: https://stackoverflow.com/a/17526663/1537389 But if you want to avoid to download all users and filter them with firebase SDK (not downloading all users) thats where a Firestore database would be more helpful. – memogarrido Jul 23 '18 at 22:51
  • It would be helpful to see your users node to see how you are structuring your data. – memogarrido Jul 23 '18 at 23:28
  • I can filter downloaded data like this if(full_name.equals("Moe")) { fullNameList.add(full_name); counter++; } but I can't filter this data with array like this String value1[] = {"Larry", "Moe", "Curly"}; – EagleH Jul 23 '18 at 23:47
  • Then I would recommend a HashMap containing the Names i.e. "Larry", "Moe", "Curly" ask keys and then just calling hashmap.containsKey("Moe"), but first you will need to populate that list, I'll edit the answer with an example. – memogarrido Jul 23 '18 at 23:56
  • where you able to do it?, did you try that same thing with Firestore? – memogarrido Jul 25 '18 at 16:44