Firebase Query to search data with value of a key

The example in this post uses Firebase Query to search for a specific username, entered by user in an EditText, in a list of all users.

1. The data to be searched is shown in image below. We will search the value of key "username".


2. In View area of your project, add an EditText edittext1, a Button button1, and a ListView listview1.

3. Create a Custom View user.xml. In this page, add an ImageView imageview1 and a TextView textview1 to display the details retrieved.

The ImageView here has width and height 40 and scale type CENTER_CROP. The linear containing them has Gravity center_vertical.

4. For listview1 select user.xml as CustomView.

5. Create a FirebaseDb component (user) specifying the data location (users) to be searched.

6. Add a String variable query, and a List Map result_users.

7. In onCreate event use FirebaseDb stop listening.

8. In button1 onClick, set String query to text in  edittext1. Then use code to search for query in the FirebaseDb component.
The code used is
user.orderByChild("username").equalTo(query).addValueEventListener(valuelistener2);

Here user is FirebaseDb component, username is the key whose value is searched, valuelistener2 is a ValueEventListener which gets a snapshot of the result of this query.

Note: This code gets the values of username which are exactly equal to the characters entered in edittext1, and it is case sensitive. And changes in case of letters or any extra or missing spaces do not lead to the desired result.

9. Create a More Block extra. Here put } in beginning and { in end. Use codes and blocks as shown in image below.
i. Define valuelistener2 with onDataChange method.
}
ValueEventListener valuelistener2 = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot _param1) {

ii. Clear List result_users.

iii. In onDataChange method, for each child (_data) of DataSnapshot (_param1), get the value in the form of a Map variable (_map) and add it to list result_users.
GenericTypeIndicator <HashMap<String, Object>> _ind = new GenericTypeIndicator<HashMap< String, Object>>() {};
try {
for (DataSnapshot _data : _param1.getChildren()) {
HashMap <String, Object> _map= _data.getValue(_ind);
result_users.add(_map);
}

iv. Display result_users in listview1.

v. Close try{ with } and catch Exception. Then close onDataChange method and add onCancelled method. Nothing to do in onCancelled method.
} catch (Exception e) {
showMessage(e.toString()); }
}

@Override
public void onCancelled(DatabaseError databaseError) { }
};
{

10. In onBindCustomView, use blocks to display the values of keys in the List result_users. See image below.

The url used to display a default profile pic if no pic is uploaded by user:

11. Save and run the project.

Comments

  1. Really Great!
    Please, what codes are required to enable a sound/voice record option in a website in sketchware app? Like, if the site has a voice note option, it works on browsers but the function not working in webview.

    e.g. The website has a sound/voice record option but this does not work when webview is embedded in sketchware app. Please help out

    ReplyDelete
    Replies
    1. Watch Out The Tutorials of
      ''Aaura Parti''
      He Told Everything in Detail

      Delete
  2. I need a help plz. Is there any way to execute lua or cpp on app? I have the lua source & I want to use it on sketchware. How can I do this?

    ReplyDelete
  3. Brother how to create firebase app store in sketchware

    ReplyDelete
  4. Ascending key value and Descending key value please 🥺

    ReplyDelete
  5. Hello, how to count number of data using specific value, inexample, count number of children in a specific class using data on firebase?

    ReplyDelete

Post a Comment

Popular posts from this blog

Simple car racing android game in Sketchware

Creating a Drawing View in Sketchware

Enable Fullscreen for Youtube videos in WebView

How to enable upload from webview in Sketchware?

List of Calendar Format symbols valid in Sketchware