java - 在回收者 View 中将 Firebase 的新数据显示到顶部

标签 java android firebase-realtime-database android-recyclerview

我正在制作一个新闻应用程序,其中我从 firebase 获取数据到 Recycler View,一切都很好,但我想将新数据获取到 recyclerview 的顶部,我该怎么做?

这就是我的 Firebase 数据的样子 -

{
  "News" : {
    "-M79ICsWRp7pL9q8cFqx" : {
      "description" : "कोरोना लॉकडाउन के बीच प्रधानमंत्री नरेंद्र मोदी आज एकबार फिर देश को संबोधित किए। राष्ट्र के नाम अपने संबंधन में उन्होंने कई अहम बातें कहीं। प्रधानमंत्री ने देश को कोरोना संकट से बाहर निकालने के लिए 20 लाख करोड़ रुपए के एक बड़े आर्थिक पैकेज की घोषणा की। साथ ही उन्होंने कहा कि जल्द ही लॉकडाउन के चौथे चरण के बारे में देश को बताया जाएगा।\n\nप्रधानमंत्री नरेंद्र मोदी इससे पहले भी कई मौके पर राष्ट्र को संबोधित कर चुके हैं। पीएम मोदी जनता कर्फ्यू, लॉकडाउन के पहले चरण की घोषणा, कोरोना वॉरियर्स के सम्मान में लोगों को ताली बजाने और दिया जलाने की अपील करने के लिए राष्ट्र को संबोधित किए थे।",
      "mImageUrl" : "https://firebasestorage.googleapis.com/v0/b/entertains.appspot.com/o/Upload_News%2F1589310703830.jpg?alt=media&token=9231b1ef-e995-4ebf-abf4-8781170e9269",
      "mName" : "PM नरेंद्र मोदी का देश के नाम संबोधन: कोरोना वायरस जीवन का हिस्सा बना रहेगा, पढ़ें प्रधानमंत्री के भाषण की 10 बड़ी बाते",
      "time" : "1"
    },
    "-M79IZn_ghOip8185CaU" : {
      "description" : "Google has been making a shift from Google Play Music to YouTube Music. While the transition would take some time to complete fully, the search giant has now made it easier for Google Play Music users to transfer their content and start using the YouTube Music service. A dedicated option has been provided on the YouTube Music app to let users to transfer content such as uploads, purchases, added songs, and albums, and playlists. A webpage has also been created for transferring podcasts from Google Play Music to Google Podcasts that is Google's own podcast app.",
      "mImageUrl" : "https://firebasestorage.googleapis.com/v0/b/entertains.appspot.com/o/Upload_News%2F1589310797913.webp?alt=media&token=d73d71ca-3acc-4076-afd3-c10696cbd11e",
      "mName" : "YouTube Music Makes It Easier to Transfer Content From Google Play Music",
      "time" : "2"
    },
    "-M79JACPm6KaXztLIDGa" : {
      "description" : "Nokia 9.3 PureView is one of the anticipated smartphones of 2020. While Nokia mobile brand licensee HMD Global hasn't yet announced any details about the new model, it has surfaced online — once again — with details such as 8K video recording support. The Nokia 9.3 PureView is already rumoured to come with a 120Hz display and a 108-megapixel primary camera. The new Nokia phone was initially expected to debut at Mobile World Congress (MWC) 2020 in Barcelona in February. However, since the trade show was cancelled due to the coronavirus outbreak, the handset didn't get its official announcement.",
      "mImageUrl" : "https://firebasestorage.googleapis.com/v0/b/entertains.appspot.com/o/Upload_News%2F1589310955332.jpg?alt=media&token=0e98bee6-7e7b-45fb-9aa9-0171a35bf5a6",
      "mName" : "Nokia 9.3 PureView",
      "time" : "3"
    },
    "-M7BX8jmcWvARw0fuivp" : {
      "description" : "India’s contact-tracing app Aarogya Setu is currently being used by 10 crore Indians. Launched on April 2, Aarogya Setu hit 50 million users in just 13 days of its launch, making it one of the most downloaded apps in the world in such a short time.\n\nAarogya Setu uses Bluetooth and GPS connectivity to help track the Covid-19 infection. On the app, users can check whether they’ve come in contact with a Covid-19 infected person and the app allows users to self-assess their symptoms as well.\n\nSince the launch, the app has undergone various updates, including a section to function as an e-pass for those who need to step out over the lockdown. The app also gets live updates on the Covid-19 infection as well.\n\nThe app has been made mandatory for all government employees and private employees over the last couple of weeks. The Railways Ministry announced early today morning that Aarogya Setu was mandatory for anyone who was going to avail the special train services. This rule might also be extended to all passengers who might be looking to fly post the lockdown.",
      "mImageUrl" : "https://firebasestorage.googleapis.com/v0/b/entertains.appspot.com/o/Upload_News%2F1589348172219.jpg?alt=media&token=757ff076-2936-440e-bef1-69245ff3d2b5",
      "mName" : "Aarogya Setu, India’s Covid-19 tracking app, now has 10 crore users",
      "time" : "4"
    },
    "-M7BXX7usuHT5h0By8my" : {
      "description" : "Support for Windows 7 ended in January, which means Microsoft wants holdouts to upgrade to Windows 10 to keep devices running securely and smoothly. If you have an older PC or laptop still running Windows 7, you can purchase the Windows 10 Home operating system on Microsoft's website for $139. But you don't necessarily have to shell out the cash: A free upgrade offer from Microsoft that technically ended in 2016 still works for many people. And if you find yourself spending more time at home due to the coronavirus pandemic, it may be a good time to try it out for yourself. \n\nWhen Windows 10 was first released in July 2015, Microsoft offered an unprecedented free upgrade offer for Windows 7, 8 and 8.1 users, good through July 2016. But in 2017, Ed Bott of CNET sister site ZDNet reported that the free upgrade tool was still functional. I tried it out in November 2019, and was able to upgrade a 2014 Dell OptiPlex 9020 desktop from Windows 7 Pro to Windows 10 Pro. As of May 2020, readers are still emailing me and commenting below, saying that it's worked for them as well.",
      "mImageUrl" : "https://firebasestorage.googleapis.com/v0/b/entertains.appspot.com/o/Upload_News%2F1589348272333.jpg?alt=media&token=d29240dd-4e2b-4f97-b6fd-87eb8ae45f2a",
      "mName" : "Windows 10 is still free to download. Here's how to get the upgrade",
      "time" : "5"
    },
    "-M7BdrN4GbbIzip4m7bp" : {
      "description" : "naman",
      "mImageUrl" : "https://firebasestorage.googleapis.com/v0/b/entertains.appspot.com/o/Upload_News%2F1589350190423.mp4?alt=media&token=ef4b2277-d6ec-48dc-b21c-643b1128f09e",
      "mName" : "naman",
      "time" : "6"
    },
    "-M7Bj5uhp7BdH2RYJMgV" : {
      "description" : "Microsoft hasn't been in a hurry to roll out 2004, given the many work and home disruptions resulting from the COVID-19 coronavirus pandemic. The product has been basically \"done\" for a while and Microsoft has continued to issue cumulative updates for it to testers ",
      "mImageUrl" : "https://firebasestorage.googleapis.com/v0/b/entertains.appspot.com/o/Upload_News%2F1589351566448.jpg?alt=media&token=b1ebafed-8ee8-422e-a234-7be5d9616b2d",
      "mName" : "Microsoft kicks off its Windows 10 May 2020 update rollout with its release to developers",
      "time" : "7"
    }
  }
}

这就是我的代码的样子 -

 mDatabasereferemce = FirebaseDatabase.getInstance().getReference("News");
        mDatabasereferemce.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
                    Upload upload = postsnapshot.getValue(Upload.class);
                    mUploads.add(upload);


                }

                ImageAdapter imageAdapter = new ImageAdapter(getContext(), mUploads);
                mRecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
                mRecyclerview.setAdapter(imageAdapter);



            }

我该怎么做才能将新数据排序到顶部?

最佳答案

嗨,你可以这样做

 mDatabasereferemce = FirebaseDatabase.getInstance().getReference("News");
    mDatabasereferemce.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            for (DataSnapshot postsnapshot : dataSnapshot.getChildren()) {
                Upload upload = postsnapshot.getValue(Upload.class);
                //mUploads.add(index,upload);
                mUploads.add(0,upload);
            }

            ImageAdapter imageAdapter = new ImageAdapter(getContext(), mUploads);
            mRecyclerview.setLayoutManager(new LinearLayoutManager(getActivity()));
            mRecyclerview.setAdapter(imageAdapter);

        }

关于java - 在回收者 View 中将 Firebase 的新数据显示到顶部,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61771422/

相关文章:

java - 从firebase中获取Json作为字符串在java中不起作用

android - 将 Firebase Persistence Enabled 设置为 true 后无法检索新数据?

JAVA-数组列表

Android Studio 无法识别 9patch 生成的图像

java - JPanel 以额外宽度绘制

java - 嵌套对象访问父属性

java - 为什么无法识别 onPageFinished 方法?

cordova - Firebase:在使用 .once() 检索所有子节点后监听它们的变化?

java - java中以下条件的模式

java - JLS 是否将静态方法和静态变量视为类成员?