Show information FireBase

0

Hi, I have a PPP where I keep information about a notice in FireBase, when I have it and keep it fairly well and will keep me on each user sending notices.

Now I need to show all that information on a different screen, I tried to do it with a ListView but it does not do anything, any idea of where the error might be? Or some way to make it better?

public class VerAviso extends MenuAvisos
{

    List<Aviso> avisos;
    ListView list;


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

        FirebaseDatabase database = FirebaseDatabase.getInstance();
        FirebaseUser user = firebaseAuth.getInstance().getCurrentUser();

        final ArrayAdapter<Aviso> adapter;

        list = (ListView)findViewById(R.id.listview);

        adapter = new ArrayAdapter<Aviso>(this, android.R.layout.simple_list_item_1);

        list.setAdapter(adapter);


        database.getReference("Aviso").child(user.getUid()).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                avisos.clear();
                for(DataSnapshot snapshot :
                        dataSnapshot.getChildren()){

                    Aviso aviso2 = snapshot.getValue(Aviso.class);
                    avisos.add(aviso2);

                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}

If necessary, the method which I use to store the Data is this.

    private void saveInformation()
    {
        //getting current user
        FirebaseUser user = firebaseAuth.getInstance().getCurrentUser();

        Aviso avisos = new Aviso();
        //Getting values from database
        avisos.setAviso(aviso1.getText().toString());
        avisos.setDescripcion(textDes.getText().toString());
        avisos.setUbicacion(textubi.getText().toString());

        FirebaseDatabase database = FirebaseDatabase.getInstance();

        final DatabaseReference myRef = database.getReference("Aviso").child(user.getUid());
        Aviso aviso2 = new Aviso(avisos.getAviso(),avisos.getDescripcion(),avisos.getUbicacion());
        myRef.push().setValue(aviso2);

        //displaying a success toast
        Toast.makeText(this, "Guardando informacion del aviso, espera...", Toast.LENGTH_LONG).show();
    }
    
asked by Cristian Prieto Beltran 26.04.2017 в 15:09
source

2 answers

1

You need to pass your list of objects to the ArrayAdapter in your constructor:

But first declare the lista :

avisos = new ArrayList<>();
adapter = new ArrayAdapter<Aviso>(this, android.R.layout.simple_list_item_1, avisos);
    
answered by 27.04.2017 / 03:44
source
1

One way is using RecyclerView

To do it with RecyclerView you need to make 2 xml first 1 with The recyclerview the other with the xml of what each row will have

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.RecyclerView
    android:id="@+id/rvAvisos"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Rows:

<?xml version="1.0" encoding="utf-8"?>

<android.support.v7.widget.CardView
    android:layout_width="match_parent"
    android:layout_height="100dp">
    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:gravity="center">
        <TextView
            android:id="@+id/tvAviso"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="AVISO"
            android:textSize="18dp"
            android:layout_marginBottom="5dp"/>
        <TextView
            android:id="@+id/tvDescripcion"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Descripción"
            android:textSize="14dp"/>
        <TextView
            android:id="@+id/tvUbicacion"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ubicacion"
            android:textSize="12dp"
            android:layout_gravity="right"
            android:layout_marginEnd="15dp"/>
    </LinearLayout>
</android.support.v7.widget.CardView>

Then you need an Adapter, which will receive the list of objects and take care that according to the size of the list of objects, pass object by object putting the information in each row.

public class AdaptadorAvisos extends RecyclerView.Adapter<AdaptadorAvisos.AvisosviewHolder> {
List<Aviso> avisos;



public AdaptadorAvisos(List<Aviso> avisos) {
    this.avisos = avisos;


}

@Override
public AvisosviewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_avisos, parent, false);
    AvisosviewHolder holder = new AvisosviewHolder(v);


    return holder;
}

@Override
public void onBindViewHolder(AvisosviewHolder holder, int position) {
    Aviso aviso = avisos.get(position);
    holder.tvAviso.setText(aviso.get(position).getAvisos);
    holder.tvDescripcion.setText(aviso.get(position).getDescripcion);
    holder.tvUbicacion.setText(aviso.get(position).getUbicacion);

}

@Override
public int getItemCount() {
    return  avisos.size();
}

public static class AvisosviewHolder extends RecyclerView.ViewHolder  {


    TextView tvAviso;
    TextView tvDescripcion;
    TextView tvUbicacion;



    public AvisosviewHolder(View itemView) {
        super(itemView);
        tvAviso = (TextView) itemView.findViewById(R.id.tvAviso);
        tvDescripcion = (TextView ) itemView.findViewById(R.id.tvDescripcion);
        tvUbicacion = (TextView ) itemView.findViewById(R.id.tvUbicacion);


    }


}

}

Finally the Main where you fill the list of objects and send the list to the adapter and the adapter you send it to the RecyclerView

    public class VerAviso extends AppCompatActivity
{
    FirebaseDatabase database = FirebaseDatabase.getInstance();
    FirebaseUser user = firebaseAuth.getInstance().getCurrentUser();
    List<Aviso> avisos;
    RecyclerView rv;
    Adapter adapter;


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

        avisos = new ArrayList<>();

        rv=(RecyclerView) findViewById(R.id.rvAvisos);
        rv.setLayoutManager(new LinearLayoutManager(this));
        adapter = new Adapter(avisos);
        rv.setAdapter(adapter);

        database.getReference("Aviso").child(user.getUid()).addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot dataSnapshot) {
                avisos.clear();
                for(DataSnapshot snapshot :
                        dataSnapshot.getChildren()){

                    Aviso aviso2 = snapshot.getValue(Aviso.class);
                    avisos.add(aviso2);

                }
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onCancelled(DatabaseError databaseError) {

            }
        });
    }
}
    
answered by 26.04.2017 в 17:16