Firebase Data Desc Sorting in Android

前端 未结 15 2120
醉话见心
醉话见心 2020-11-22 07:51

I am storing data in Firebase storage.

Object Comment with attribute timestamp. When I push data from device to Firebase I\'m populating

相关标签:
15条回答
  • 2020-11-22 08:07

    It is very simple,Use -1,-2,-3, etc to store data in the firebase database.Now the data will be displayed in the reverse order in recyclerView.

    -3
    -2
    -1
    
    0 讨论(0)
  • 2020-11-22 08:08

    If you are doing it on web then you can push values in an array and then print the stored values in reverse order.

    var a=[]; //declaring an array a.
    var num=snapshot.numChildren(); //storing number of children in var num.
    a.push(snapshot.val()); // pushing data in the array.
    if (a.length==num){ //checking if length of array is same as number of children.
    a.reverse(); //reversing the array elements.
    for(i=0; i<num; i++){
    a[i].name;//this will sort the data in descending order.
    }
    }
    
    0 讨论(0)
  • 2020-11-22 08:08

    As I understand there is no descending method, for default it is ascending only, but you can reverse it manually. For more information read an official documantation about sorting and filtering data of Realtime Database https://firebase.google.com/docs/database/android/lists-of-data#sorting_and_filtering_data

    0 讨论(0)
  • 2020-11-22 08:09

    one good solution I find if you are using recycler view to render that data...

    mLayoutManager = new LinearLayoutManager(getActivity());
    mLayoutManager.setReverseLayout(true);
    mLayoutManager.setStackFromEnd(true);
    
    // And set it to RecyclerView
    mRecyclerView.setLayoutManager(mLayoutManager);
    

    it will reverse the data rendering...

    private static class ChatMessageViewHolder extends RecyclerView.ViewHolder {
             TextView messageText;
             TextView nameText;
    
             public ChatMessageViewHolder(View itemView) {
                 super(itemView);
                 nameText = (TextView)itemView.findViewById(android.R.id.text1);
                 messageText = (TextView) itemView.findViewById(android.R.id.text2);
             }
         }
    
         FirebaseRecyclerViewAdapter<ChatMessage, ChatMessageViewHolder> adapter;
         ref = new Firebase("https://<yourapp>.firebaseio.com");
    
         RecyclerView recycler = (RecyclerView) 
    
         findViewById(R.id.messages_recycler);
             recycler.setHasFixedSize(true);
    
             //////////////////////////////////////////////////////////
             mLayoutManager = new LinearLayoutManager(getActivity());
             mLayoutManager.setReverseLayout(true);
             mLayoutManager.setStackFromEnd(true);
             recycler.setLayoutManager(mLayoutManager);
             /////////////////////////////////////////////////////////
    
             adapter = new FirebaseRecyclerViewAdapter<ChatMessage, ChatMessageViewHolder>(ChatMessage.class, android.R.layout.two_line_list_item, ChatMessageViewHolder.class, mRef) {
             public void populateViewHolder(ChatMessageViewHolder chatMessageViewHolder, ChatMessage chatMessage) {
                 chatMessageViewHolder.nameText.setText(chatMessage.getName());
                 chatMessageViewHolder.messageText.setText(chatMessage.getMessage());
             }
         };
         recycler.setAdapter(mAdapter);
    
    0 讨论(0)
  • 2020-11-22 08:13

    I have solved problem by extending FirebaseListAdapter and overriding getItem method:

    public abstract class ReverseFirebaseListAdapter<T> extends FirebaseListAdapter<T>  {
    
    public ReverseFirebaseListAdapter(Activity activity, Class<T> modelClass, int modelLayout, Query ref) {
        super(activity, modelClass, modelLayout, ref);
    }
    
    public ReverseFirebaseListAdapter(Activity activity, Class<T> modelClass, int modelLayout, DatabaseReference ref) {
        super(activity, modelClass, modelLayout, ref);
    }
    
    @Override
    public T getItem(int position) {
      return super.getItem(getCount() - (position + 1));
    }
    

    }

    0 讨论(0)
  • 2020-11-22 08:14

    add a column namely timestamp with values (-1*System.currentTimeMillis()) and while fetching data use orderByChild("timestamp").limitToFirst(15) or orderByChild("timestamp").limitToLast(15) according to your requirement. it is smartway to get data in sorted manner, as firebase has no rule for descending order.

    0 讨论(0)
提交回复
热议问题