[JAVA] I want to use FireBase to display a timeline like Twitter

I want to display a timeline like Twitter

Since the data acquisition process of Firebase is an asynchronous process, when displaying the image and database data in a specific order like the Twitter timeline, if you display it without thinking, the image will shift, so the data will be recursively displayed. The recommended way is to get it.

java.Timeline.java


public class Timeline extends AppCompatActivity{

     //List to put the fetched database data
     private ArrayList<String> getDate = new ArrayList<>();
    
     //Get image reference
     private ArrayList<StorageReference> spaceRef = new ArrayList<>();

     //Create DatabaseReference object
     private DatabaseReference mDatabase;

     //FireBase storage
     private FirebaseStorage storage = FirebaseStorage.getInstance();

     //storage
     private StorageReference storageRef = storage.getReference();

    //Count variable
    private int Count = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

          //Get reference to Database
          mDatabase = FirebaseDatabase.getInstance().getReference();

          //FireBase Events
          mDatabase.addValueEventListener(new ValueEventListener() {
               @Override
               public void onDataChange(@NonNull DataSnapshot dataSnapshot) {

                //Data you want to get
                Object getObject = postSnapshot.child(/*Data you want to get*/).getValue();

                //Null check of acquired data
                if(getObject != null) {

                       //Store in list
                       getDate.add(getObject .toString());

                       //Get image reference
                       spaceRef.add(storageRef.child(/*Reference image*/));

                       //Processing of the image you want to go to
                       getPhoto();

                }
          }
     }
    //Image acquisition
    private void getPhoto() {
        int Listsize = spaceRef.size();
        //Avoid looking off the list
        if(Count < Listsize) {
            final long ONE_MEGABYTE = 1024 * 1024;
            //Storage event
            spaceRef.get(Count).getBytes(ONE_MEGABYTE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
                @Override
                public void onSuccess(byte[] bytes) {
                      //Thumbnail image acquisition
                      Bitmap bit = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);


                      /*Put the process to add to the list view*/

                      Count++;

                      getPhoto();

                }
            }).addOnFailureListener(new OnFailureListener() {
                @Override
                public void onFailure(@NonNull Exception exception) {
                }
            });
     }
}

The original list view has already been created, and I wrote it for those who are stumbling one step further, so it is quite appropriate (I think that the processing actually used is not helpful), but for the time being I managed to do it. .. Also, since it is java, I personally think that it is better to check null. My friend published it on Github, so I'll put a link on it. StutdentTimeline.java and TeacherStudent.java actually use this method. https://github.com/wasabimochi/ivycon When I feel like it, I'll write it neatly and post it (I'm not saying that). Please comment if you have any questions. see you.

Recommended Posts

I want to use FireBase to display a timeline like Twitter
I want to use FormObject well
I want to use swipeback on a screen that uses XLPagerTabStrip
I want to develop a web application!
I want to write a nice build.gradle
I want to display background-ground-image on heroku.
I want to use DBViewer with Eclipse 2018-12! !!
I want to write a unit test!
I want to use @Autowired in Servlet
I tried to use Selenium like JQuery
I want to display a PDF in Chinese (Korean) with thin reports
I want to use arrow notation in Ruby
[Ruby] I want to do a method jump!
I want to use java8 forEach with index
I want to simply write a repeating string
I want to use Clojure's convenient functions in Kotlin
I want to call a method of another class
I want to do something like "cls" in Java
I want to use NetBeans on Mac → I can use it!
[Swift] I implemented a Twitter timeline in UITableView (Part 2)
I want to use fish shell in Laradock too! !!
I want to use ES2015 in Java too! → (´ ・ ω ・ `)
[Swift] I implemented a Twitter timeline in UITableView (Part 1)
I want to display the name of the poster of the comment
I want to monitor a specific file with WatchService
I want to define a function in Rails Console
[Swift] I want to do something like C's sprinftf
I want to add a reference type column later
I want to click a GoogleMap pin in RSpec
I want to connect to Heroku MySQL from a client
I want to create a generic annotation for a type
I want to add a delete function to the comment function
[Swift] I implemented a Twitter timeline in UITableView (Part 3)
I want to use PowerMock in a class that combines parameterized tests and ordinary tests
[Rails] I want to display the link destination of link_to in a separate tab
[Beginner] I want to modify the migration file-How to use rollback-
[Java] I want to convert a byte array to a hexadecimal number
I want to find a relative path in a situation using Path
I want to use screen sharing on the login screen on Ubuntu 18
I want to convert characters ...
I want to implement a product information editing function ~ part1 ~
I want to make a specific model of ActiveRecord ReadOnly
I want to make a list with kotlin and java!
I want to call a method and count the number
I want to make a function with kotlin and java!
I want to create a form to select the [Rails] category
I want to use the sanitize method other than View.
[Rails] I want to display "XX minutes ago" using created_at!
Even in Java, I want to output true with a == 1 && a == 2 && a == 3
I want to give a class name to the select attribute
I want to create a Parquet file even in Ruby
Implemented a strong API for "I want to display ~~ on the screen" with simple CQRS
Study Java: Use Timer to create something like a bomb timer
I want to recursively search for files under a specific directory
I want to create a chat screen for the Swift chat app!
I want to make a button with a line break with link_to [Note]
I want to add a browsing function with ruby on rails
I tried to write code like a type declaration in Ruby
[Ruby] I want to put an array in a variable. I want to convert to an array
I want to display the images under assets/images in the production environment
I want to extract between character strings with a regular expression