简体   繁体   中英

How to get specific data of child node by searching from firebase database in android studio - java?

im working with firebase database. i want to get specific data after searching from "data" node. Below im sharing my database Screenshot and java function that i have already tried. I'm working on it from two days. but i can't solve it. please help me. Thanks in advance.

private void getSearchedData(String selectedItem) {

    reference = FirebaseDatabase.getInstance().getReference().child("Category");
    Query query = reference.orderByChild("data/dataName").equalTo(selectedItem);

    ArrayList<ChannelData> channelDataArrayList = new ArrayList<>();

    query.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot snapshot) {

            for (DataSnapshot requestSnapshot : snapshot.getChildren()) {
                DataSnapshot channelsSnapshot = requestSnapshot.child("data");

                for (DataSnapshot ds : channelsSnapshot.getChildren()) {
                    ChannelData channelData = new ChannelData(ds.child("dataName").getValue(String.class),
                            ds.child("dataImage").getValue(String.class));
                    channelDataArrayList.add(channelData);

                    searchAdapter adapter = new searchAdapter(channelDataArrayList, getApplicationContext());
                    rvSearchedChannel.setAdapter(adapter);
                }
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Toast.makeText(MainActivity2.this, "Error: " + error, Toast.LENGTH_SHORT).show();
        }
    });

}

And this is my database structure. i want to get "dataImage", "dataName" and "dataLink" after searching "dataName".

Firebase 数据库结构图

if you are using edit text for as a search box. add addTextChangedListener on edit text and you will get 3 methods on afterTextChanged method creat a function and pass it editable.tostring you get it in function.

and function code is:

FirebaseDatabase.getInstance().getReference().child("Category").child("data/dataName").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.hasChildren())
            {
                list.clear();
                for (DataSnapshot dss : dataSnapshot.getChildren())
                {

                    Register register = dss.getValue(Register.class);
                    if (register.getUsername().toLowerCase().contains(s.toLowerCase())){
                        list.add(register);
                    }
                    if (Objects.equals(register.getId(), FirebaseAuth.getInstance().getCurrentUser().getUid())){
                        list.remove(register);
                    }

                }
                SearchUserAdapter adapter = new SearchUserAdapter(list, getContext());

                LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
                search_rv.setLayoutManager(linearLayoutManager);
                search_rv.setAdapter(adapter);

       
                adapter.notifyDataSetChanged();
            }
        }

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

        }
    });

Queries in the Firebase Realtime Database search the direct child nodes of the path you run them at for a value at a fixed path that you specify.

In your example, the path of the value in the JSON is not fixed as you have relevant nodes in data/01/dataName and data/02/dataName , which isn't possible. Since the path that you order/filter on ( data/dataName ) doesn't even exist under Category/01 , so you won't get any results.

If you want to allow this query on the database, you'll need to change the data structure (or add an additional one) where the values you want to order/filter on are at a fixed path under the direct child nodes of the path you query. So you'll have to lose a level of your JSON structure, but can keep the information by for example duplicating the categoryName value into each child node:

CategoryData: {
  "0101": {
    CategoryName: "Pakistani",
    dataName: "News",
    ...
  },
  "0102": {
    CategoryName: "Pakistani",
    dataName: "Dramas",
    ...
  },
  "0201": {
    CategoryName: "India",
    dataName: "news",
    ...
  },
  "0202": {
    CategoryName: "India",
    dataName: "movies",
    ...
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM