Sort Firebase data for Android project

If your firebase data contains same keys at each position, as shown in image below,

then you can follow the steps below for sorting the data before displaying it in an app created in Sketchware.

1. In your Sketchware project, add a ListView listview1.

2. Create a CustomView custom.xml. In this View add TextViews for displaying data at each position in Firebase database.

3. In properties of listview1 select custom.xml as customView.

4. Add a new List Map maplist.

5. Add a FirebaseDB component with any name, and with data location same as in your Firebase database. (fdb: results)

6. Add ListView onBindCustomView event. In this event use blocks for displaying the data from List Map maplist in ListView listview1.
7. Save and run the project.

8. Delete the installed app.

9. Export source code of this project. Unzip it in a folder on sdcard.

10. Open the project in AIDE.

11. In app level build.gradle file, in dependencies, change all 'implementation' to 'compile' and change the version of all these libraries to '+'. After that save the file. It will download the required libraries.

12. Now open MainActivity.java, and find the code
private void initializeLogic(){
}

Inside this put the following code:
com.google.firebase.database.Query query = fdb.orderByChild("name");

ValueEventListener _valueEventListener = new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
try { maplist = new ArrayList<>();
for (DataSnapshot _data : dataSnapshot.getChildren()) {
HashMap<String, Object> _map = _data.getValue(new GenericTypeIndicator<HashMap< String, Object>>() {});
maplist.add(_map);
}
listview1.setAdapter(new Listview1Adapter(maplist));
((BaseAdapter)listview1.getAdapter()).notifyDataSetChanged(); 
} catch (Exception e){
showMessage(e.toString()); }
}
@Override
public void onCancelled(DatabaseError databaseError) { }
};

query.addValueEventListener(_valueEventListener);

Here fdb is FirebaseDB component and name is the key according to which data is to be sorted alphabetically and numerically.

13. Save and run the project. The app will display the data sorted alphabetically for key 'name'. To sort according to any other key, just change the key in the code.

Watch the video below

Comments

  1. hi Sanjeev,
    we haven't seen your tutorial for a long time, we really miss it...
    We've just tried your firebase tutorials..
    the question is,

    how to make "security rules" to our firebase

    so that each user can only access "their" data?
    because, every time I change it with rules: {auth id = uid blah3 (as in firebase documentation) I can't access or write my data ...

    Because we don't want our data to be read by the world ..

    ReplyDelete
  2. hey Sanjiv can you help me i tried many times to download firebase storage data but i cant. can you define me about block to use download image from firebase storage.

    ReplyDelete
  3. But after edit the code of the app in Aide how will i import it to sketchware for make some updates

    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