Android Development Recovering Firebase Data

Asked

Viewed 582 times

0

inserir a descrição da imagem aquiinserir a descrição da imagem aquiHello guys I come again for answers, I have a bank in firebase that has some movies and series and even tv shows, wanted to separate in genres these attributes, Example: whatever serial gets fixed in Series and whatever movie gets fixed in Movies etc... but this I wanted to put in cards using recyclerview to list and in that recyclerview a separation of each genre using horizontally. I wanted a light, someone could help me?

public class SalaActivity extends AppCompatActivity implements ICRUDAction {

private MyAdapter appListCenterAdapter, appListStartAdapter;
private List<ConteudoSala> conteudoSala, conteudoSala1;


private String name;
private String a;
private ProgressDialog progressDialog;

//Private recyclerview mRecyclerView;

private boolean mHorizontal;

public RecyclerView recyclerView;

public RecyclerView recyclerView2;

DatabaseReference mDatabase = FirebaseDatabase.getInstance().getReference(DATABASE_PATH_ANIME);

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.sala_lista);
    ButterKnife.bind(this);

    progressDialog = new ProgressDialog(this);
    progressDialog.setMessage("Por favor, espere...");
    progressDialog.show();

    getAnime();
    getFilmes();
    request_user_name();

}
public void getAnime(){
    conteudoSala = new ArrayList<>();
    recyclerView = (RecyclerView)findViewById(R.id.recyclerView);




    mDatabase.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

            Log.i("onDataChange", snapshot.toString());

            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                ConteudoSala produto = postSnapshot.getValue(ConteudoSala.class);
                conteudoSala.add(produto);
            }

            LinearLayoutManager layoutManagerCenter
                    = new LinearLayoutManager(SalaActivity.this, LinearLayoutManager.HORIZONTAL, false);
            recyclerView.setLayoutManager(layoutManagerCenter);
            SnapHelper snapHelperCenter = new LinearSnapHelper();
            snapHelperCenter.attachToRecyclerView(recyclerView);

            appListCenterAdapter = new MyAdapter(SalaActivity.this, conteudoSala);

            recyclerView.setAdapter(appListCenterAdapter);

            appListCenterAdapter.notifyDataSetChanged();
        }
        @Override
        public void onCancelled(DatabaseError databaseError) {
            progressDialog.dismiss();
        }
    });
}
public void getFilmes() {
    conteudoSala1 = new ArrayList<>();

    recyclerView2 = (RecyclerView)findViewById(R.id.recyclerView2);

    DatabaseReference mDatabase2 = FirebaseDatabase.getInstance().getReference(DATABASE_PATH_FILME);
    mDatabase2.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot snapshot) {

            progressDialog.dismiss();
            Log.i("onDataChange", snapshot.toString());
            for (DataSnapshot postSnapshot : snapshot.getChildren()) {
                ConteudoSala produto1 = postSnapshot.getValue(ConteudoSala.class);
                conteudoSala1.add(produto1);
            }

           appListStartAdapter = new MyAdapter(SalaActivity.this, conteudoSala1);

            LinearLayoutManager layoutManagerStart = new LinearLayoutManager(SalaActivity.this, LinearLayoutManager.HORIZONTAL, false);
            recyclerView2.setLayoutManager(layoutManagerStart);
            SnapHelper snapHelperStart = new StartSnapHelper();
            snapHelperStart.attachToRecyclerView(recyclerView2);

            recyclerView2.setAdapter(appListStartAdapter);

            appListStartAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            progressDialog.dismiss();
        }
    });
    progressDialog.hide();
}
  • Your question is very generic. What have you tried?

  • could show your firebase database structure?

  • ready galley posted my structure, is equal this in the image, like... these addFilme, addAnime, addSerie want to return in horizontal list form one below the other being that each movie, anime and serie will stay in their respective places, I was able to recover the data but in a way POG gambiarra and yes I have no idea to bring the organized data.

  • I was writing my reply and it was practically the same, I had not seen that you had edited kk

  • just need to organize the code.

  • for example, rename recyclerview and recyclerview2 to rvAnimes...rvSeries or recycler_animes, recycler_series to get less confused.

  • good idea, is more readable, but this way when I recover the data, only gets a recyclerview the rest some and could not persist the 2 recyclerview together, type a bug in the app and some recyclerviews ):

Show 2 more comments

1 answer

0


1. Create a reference and list of each category. (Series, Movies, Animes)

    public ArrayList<Objeto> filmes;
    public ArrayList<Objeto> animes;
    public ArrayList<Objeto> series;
    public DatabaseReference database;
    public Query query;


public void getAnimes()
{

    animes = new ArrayList<>();
    database = FirebaseDatabase.getInstance().getReference();
    query = database.child("projetofirebase-32c0b/addAnime");

    query.addChildEventListener(new ChildEventListener()
    {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName)
        {
            Objeto object = dataSnapshot.getValue(Objeto.class);
            animes.add(object);

        }

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

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

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

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

public void getFilmes()
{

    filmes = new ArrayList<>();
    database = FirebaseDatabase.getInstance().getReference();
    query = database.child("projetofirebase-32c0b/addFilme");

    query.addChildEventListener(new ChildEventListener()
    {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName)
        {
            Objeto object = dataSnapshot.getValue(Objeto.class);
            filmes.add(object);

        }

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

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

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

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}


public void getSeries()
{

    series = new ArrayList<>();
    database = FirebaseDatabase.getInstance().getReference();
    query = database.child("projetofirebase-32c0b/addSerie");

    query.addChildEventListener(new ChildEventListener()
    {

        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String previousChildName)
        {
            Objeto object = dataSnapshot.getValue(Objeto.class);
            series.add(object);

        }

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

        }

        @Override
        public void onChildRemoved(DataSnapshot dataSnapshot) {

        }

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

        }

        @Override
        public void onCancelled(DatabaseError databaseError) {

        }
    });

}

2. Create a class for object model

    public class Objeto
{
    String nome;
    String url;
    String valor;

    public Objeto ()
    {

    }
    public Objeto(String nome,
           String url,
           String valor)
    {
        this.nome = nome;
        this.url = url;
        this.valor = valor;
    }
}

3. Perform the functions getAnimes(), getFilmes(), getSeries()

4.Pass the anime, series and movies lists in your recyclerview

Browser other questions tagged

You are not signed in. Login or sign up in order to post.