如何从Android Studio中的Firebase实时数据库中的ListView中首先获取最后添加的数据 [英] How to get the last added data on first in the listview from firebase realtime database in android studio
问题描述
我有一个列表视图,用于从Firebase实时数据库检索数据.但是问题是我想在列表视图的顶部显示最后添加的数据...这是一个告示板类型的项目,因此,我想在列表视图的上显示最新的通知...找不到一些网络上的好解决方案....
I have a list view to retrieve the data from firebase realtime database. But the problem is I want to show the last added data on the top of the listview...Its a notice board type project so, I want to show the latest notice on the to of the listview...could't find some good solutions on the web....
这是"mainactivity.java"的代码....
here are the codes of "mainactivity.java"....
package com.example.wrappo;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import com.google.firebase.database.ChildEventListener;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import java.util.ArrayList;
import java.util.Timer;
import java.util.TimerTask;
public class MainActivity extends AppCompatActivity {
ListView myListView;
ArrayList<String> myArrayList = new ArrayList<>();
DatabaseReference mRef;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ArrayAdapter<String> myArrayAdapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1, myArrayList);
myListView = (ListView) findViewById(R.id.listview1);
myListView.setAdapter(myArrayAdapter);
mRef = FirebaseDatabase.getInstance().getReference();
mRef.addChildEventListener(new ChildEventListener() {
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
String value = dataSnapshot.getValue(String.class);
myArrayList.add(value);
myArrayAdapter.notifyDataSetChanged();
}
@Override
public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
myArrayAdapter.notifyDataSetChanged();
}
@Override
public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {
}
@Override
public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
这是xml代码....
here is the xml code....
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/listview1"/>
</RelativeLayout>
推荐答案
之前已经讨论了很多次.我建议您查看关于最新动态"的问题和有关反向"的问题.
This has been covered quite a few times before already. I recommend checking out questions on 'newest on top' and on questions on 'reverse'.
最快的入门方法可能是将项目简单地添加到ArrayList
的开头而不是结尾:
The quickest way to get started might be to simply add the items to the start of your ArrayList
instead of to the end of it:
myArrayList.add(0, value);
这篇关于如何从Android Studio中的Firebase实时数据库中的ListView中首先获取最后添加的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!