Receive all the items stored in my database (Firebase)

1

This is how I generate item's with a different name automatically in my database of Firebase and send it to it.

public class MainActivity extends AppCompatActivity {
    private String FIREBASE_URL = "miurl";
    // genero un item distinto
    private String FIREBASE_CHILD = "item_"+ (int) System.currentTimeMillis();
    EditText nombre;
    Firebase firebase;

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

        nombre = (EditText) findViewById(R.id.nombre);

        ButterKnife.bind(this);
        Firebase.setAndroidContext(this);
        firebase = new Firebase(FIREBASE_URL).child(FIREBASE_CHILD);
    }

    // envio a firebase

    @OnClick(R.id.button)
    public void writeToFirebase() {
        firebase.setValue("Nombre:" + " " + nombre.getText());
    }
}

So far so good, but now when I want to receive the data in another clase , I only receive the item that I assign in private String FIREBASE_CHILD = "item";

public class Main2Activity extends AppCompatActivity {
    private String FIREBASE_URL = "miurl";
    // solo recibo el item que asigne aquí
    private String FIREBASE_CHILD = "item";
    @Bind(R.id.editText)
    TextView editText;
    Firebase firebase;


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

        ButterKnife.bind(this);
        Firebase.setAndroidContext(this);
        firebase = new Firebase(FIREBASE_URL).child(FIREBASE_CHILD);

        firebase.addValueEventListener(new ValueEventListener() {
            @Override
            public void onDataChange(DataSnapshot snapshot) {
                if (snapshot.getValue() != null) {
                    // recibo en mi textview
                    editText.setText(snapshot.getValue().toString());
                    Log.e(getLocalClassName(), snapshot.getValue().toString());
                }
            }

            @Override
            public void onCancelled(FirebaseError error) {
            }
        });
    }
}

How can I receive all item's in different TextView ?

    
asked by UserNameYo 22.05.2017 в 16:41
source

1 answer

1

I use this code to go through all the elements of one of the "children" of my database in firebase. Specifically the son is called photos and once established "the connection", I take all the children of this and then I add them to a list:

DataSnapshot fotos = dataSnapshot.child("fotos");
                Iterable<DataSnapshot> fotoData = fotos.getChildren();

                for (DataSnapshot datos : fotoData) {
                    String c = (String) datos.getValue();
                    Foto foto = new Foto(c);
                    lista.add(foto);
                }

I hope it serves you.

Greetings.

    
answered by 22.05.2017 / 17:17
source