按降序对 Firebase 数据进行排序。

huangapple 未分类评论45阅读模式
英文:

Sort Firebase data in descending order

问题

private Query reference; //数据库引用
private ListView ListView; //创建列表视图
private ArrayList<String> arrayList = new ArrayList<>(); //创建字符串数组列表

private ArrayAdapter<String> adapter; //创建数组适配器
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_high_scores);

    reference = FirebaseDatabase.getInstance().getReference("Highscores").orderByValue(); //引用数据库中的高分部分

    ListView = (ListView) findViewById(R.id.list_view); //在XML中找到列表视图

    adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList); //将适配器设置为列表
    Collections.reverse(arrayList); //反转数组列表

    ListView.setAdapter(adapter);

    reference.addChildEventListener(new ChildEventListener() { //为数据库添加子监听器
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            String value = dataSnapshot.getValue().toString(); //将数据库中的值转换为字符串
            arrayList.add(value); //将其添加到数组列表中
            adapter.notifyDataSetChanged(); //通知数据集已更改
        }
    });
}

数据库中的数据:

{
  "-M4FPEiJ_DVc9kaQrebz" : 0,
  "-M4FPIuMbArT6KjtrDtO" : 6,
  "-M4FPP5EdC1CaKgOVECX" : 17,
  "-M4FPTWg7JkKRvWakRkH" : 12,
  "-M4FPYPNVZIjhBM6gVWW" : 11,
  "-M4FPbL9I-fsQwRvVIFX" : 5
}

回答:

通过在数组列表的索引0处添加元素,不管如何都能修复此问题,所有其他元素都会被简单地向右移动,从而导致列表视图以降序显示。

String value = dataSnapshot.getValue().toString(); //将数据库中的值转换为字符串
arrayList.add(0, value); //将其添加到数组列表的索引0处
adapter.notifyDataSetChanged(); //通知数据集已更改
英文:

I want to create a highscores ListView with entries being displayed in descending order, with the use of orderByValue I am getting them sorted in ascending order however when using Collections.reverse() with my arraylist, the entries are still not reversed in the ListView.

 private Query reference; //database reference
private ListView ListView; //creating listview
private ArrayList&lt;String&gt; arrayList = new ArrayList&lt;&gt;();

private ArrayAdapter&lt;String&gt; adapter; //creating array adapter
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_high_scores);


    reference = FirebaseDatabase.getInstance().getReference(&quot;Highscores&quot;).orderByValue(); //reference the highscore section in database



    ListView = (ListView) findViewById(R.id.list_view); //find the list view in the xml


    adapter = new ArrayAdapter&lt;String&gt;(this, android.R.layout.simple_list_item_1, arrayList); // set the adapter to the list
    Collections.reverse(arrayList);

    ListView.setAdapter(adapter);

    reference.addChildEventListener(new ChildEventListener() { // add a child listener for the database
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

            String value = dataSnapshot.getValue().toString(); // convert the values in the databse to string
            arrayList.add(value); //add them to the arraylist
            adapter.notifyDataSetChanged(); //notify that the dataset has changed



        }

the data in the database is

{
"-M4FPEiJ_DVc9kaQrebz" : 0,

"-M4FPIuMbArT6KjtrDtO" : 6,

"-M4FPP5EdC1CaKgOVECX" : 17,

"-M4FPTWg7JkKRvWakRkH" : 12,

"-M4FPYPNVZIjhBM6gVWW" : 11,

"-M4FPbL9I-fsQwRvVIFX" : 5
}

Answer:

Managed to fix this by simply adding to the arraylist at 0 index no matter what, all other elements were simply shifted to the right leading to a listview in descending order.

            String value = dataSnapshot.getValue().toString(); // convert the values in the databse to string
            arrayList.add(0, value); //add them to the arraylist
            adapter.notifyDataSetChanged(); //notify that the dataset has changed

答案1

得分: 0

reference.addChildEventListener(new ChildEventListener() { 
    @Override
    public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
        String value = dataSnapshot.getValue().toString(); 
        arrayList.add(value); 
        Collections.reverse(arrayList);  
        adapter.notifyDataSetChanged(); 
    }
});
英文:

reference.addChildEventListener(new ChildEventListener() { // add a child listener for the database
@Override
public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        String value = dataSnapshot.getValue().toString(); // convert the values in the databse to string
        arrayList.add(value); //add them to the arraylist
        Collections.reverse(arrayList);  //add this line for short solution

        adapter.notifyDataSetChanged(); //notify that the dataset has changed



    }

huangapple
  • 本文由 发表于 2020年4月7日 00:34:58
  • 转载请务必保留本文链接:https://java.coder-hub.com/61064577.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定