问题
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....
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) {
}
});
}
}
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>
回答1:
This has been covered quite a few times before already. I recommend checking out questions on 'newest on top' and on questions on 'reverse'.
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);
来源:https://stackoverflow.com/questions/60711125/how-to-get-the-last-added-data-on-first-in-the-listview-from-firebase-realtime-d