0

I have one listview and it get data from Firebase, the problem is that it show the last element added on the down and the old element on the top of the listview, im trying to change the order.

This is my Activity Code:

package com.example.appcamping.Gestion;


public class EnviarMostrarGastos extends AppCompatActivity {

ListView listview;
ArrayList<String> arrayList = new ArrayList<>();
ArrayAdapter<String> arrayAdapter;
Module2 module2;
private EditText mEditPrecio;
private EditText mEditNombreGasto;
private DatabaseReference mDatabaseGastos;
private Button mEnviarGastos;
private String mFechaGasto;
//----------------MOSTRAR GASTOS
private DatabaseReference databaseReference;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_enviar_mostrar_gastos);

    mFechaGasto = new Date().toString();
    mEditNombreGasto = (EditText) findViewById(R.id.editNombreGasto);
    mEditPrecio = (EditText) findViewById(R.id.editPrecio);
    mDatabaseGastos = FirebaseDatabase.getInstance().getReference();
    mEnviarGastos = findViewById(R.id.btnEnviarGastos);
    mEnviarGastos.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            String key = mDatabaseGastos.child("Gastos").push().getKey();
            String nombregasto = mEditNombreGasto.getText().toString();
            String precio = mEditPrecio.getText().toString();
            String fechagasto = mFechaGasto;

            mDatabaseGastos.child("Gastos").child(key).child("NombreGasto").setValue(nombregasto);
            mDatabaseGastos.child("Gastos").child(key).child("Precio").setValue(precio);
            mDatabaseGastos.child("Gastos").child(key).child("FechaGasto").setValue(fechagasto);
        }
    });

    //-----------------MOSTRAR GASTOS--------------
    databaseReference= FirebaseDatabase.getInstance().getReference("Gastos");
    listview=(ListView) findViewById(R.id.listMostrarGastos);
    arrayAdapter= new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, arrayList);
    listview.setAdapter(arrayAdapter);
    databaseReference.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {
            String value=dataSnapshot.getValue(Gastos.class).toString();
            arrayList.add(value);
            arrayAdapter.notifyDataSetChanged();
        }

        @Override
        public void onChildChanged(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onChildRemoved(@NonNull DataSnapshot dataSnapshot) {

        }

        @Override
        public void onChildMoved(@NonNull DataSnapshot dataSnapshot, @Nullable String s) {

        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });

}

}

This is my xml: enter image description here

and this is how show the elements: enter image description here

1 Answers1

0

If you are trying to reverse the order, you can use the following code before adapter.notifydatasetchanged()

Collections.reverse(arrayList);
Usman Zafer
  • 1,261
  • 1
  • 10
  • 15