I have a class that generates a feed of photos downloaded from firebase, and I have another class for comments, I would like to retrieve these photos from the feed in the comments for a preview:
First class:
public class FeedFragment extends Fragment {
private RecyclerView recyclerFeed;
private AdapterFeed adapterFeed;
private List<Feed> listaFeed = new ArrayList<>();
private ValueEventListener valueEventListenerFeed;
private DatabaseReference feedRef;
private String idUsuarioLogado;
public FeedFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_feed, container, false);
//Configurações iniciais
idUsuarioLogado = UsuarioFirebase.getIdentificadorUsuario();
feedRef = ConfiguracaoFirebase.getFirebase()
.child("feed")
.child("postagens");
//Inicializar componentes
recyclerFeed = view.findViewById(R.id.recyclerFeed);
//Configura recyclerview
adapterFeed = new AdapterFeed(listaFeed, getActivity() );
recyclerFeed.setHasFixedSize(true);
recyclerFeed.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerFeed.setAdapter( adapterFeed );
return view;
}
private void listarFeed(){
valueEventListenerFeed = feedRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for ( DataSnapshot ds: dataSnapshot.getChildren() ){
listaFeed.add( ds.getValue(Feed.class) );
}
Collections.reverse( listaFeed );
adapterFeed.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
public void onStart() {
super.onStart();
listarFeed();
}
@Override
public void onStop() {
super.onStop();
feedRef.removeEventListener( valueEventListenerFeed );
}
}
Class comments
public class CommentsActivity extends AppCompatActivity {
private EditText editComentario;
private RecyclerView recyclerComentarios;
private String idPostagem;
private Usuario usuario;
private AdapterComentario adapterComentario;
private List<Comentario> listaComentarios = new ArrayList<>();
private DatabaseReference firebaserRef;
private DatabaseReference comentariosRef;
private ValueEventListener valueEventListenerComentarios;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_comentarios);
//Inicializa componentes
editComentario = findViewById(R.id.editComentario);
recyclerComentarios = findViewById(R.id.recyclerComentarios);
//Configuracoes iniciais
usuario = UsuarioFirebase.getDadosUsuarioLogado();
firebaserRef = ConfiguracaoFirebase.getFirebase();
//Configura toolbar
Toolbar toolbar = findViewById(R.id.toolbarPrincipal);
toolbar.setTitle("Comentários");
setSupportActionBar( toolbar );
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close_black_24dp);
//Configura recyclerview
adapterComentario = new AdapterComentario(listaComentarios, getApplicationContext() );
recyclerComentarios.setHasFixedSize( true );
recyclerComentarios.setLayoutManager(new LinearLayoutManager(this));
recyclerComentarios.setAdapter( adapterComentario );
//Recupera id da postagem
Bundle bundle = getIntent().getExtras();
if( bundle != null ){
idPostagem = bundle.getString("idPostagem");
}
}
private void recuperarComentarios(){
comentariosRef = firebaserRef.child("comentarios")
.child( idPostagem );
valueEventListenerComentarios = comentariosRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
listaComentarios.clear();
for(DataSnapshot ds: dataSnapshot.getChildren()){
listaComentarios.add( ds.getValue(Comentario.class) );
}
adapterComentario.notifyDataSetChanged();
}
@Override
public void onCancelled(DatabaseError databaseError) {
}
});
}
@Override
protected void onStart() {
super.onStart();
recuperarComentarios();
}
@Override
protected void onStop() {
super.onStop();
comentariosRef.removeEventListener( valueEventListenerComentarios );
}
public void salvarComentario(View view){
String textoComentario = editComentario.getText().toString();
if( textoComentario != null && !textoComentario.equals("") ){
Comentario comentario = new Comentario();
comentario.setIdPostagem( idPostagem );
comentario.setIdUsuario( usuario.getId() );
comentario.setNomeUsuario( usuario.getNome() );
comentario.setCaminhoFoto( usuario.getCaminhoFoto() );
comentario.setComentario( textoComentario );
if(comentario.salvar()){
Toast.makeText(this,
"Comentário salvo com sucesso!",
Toast.LENGTH_SHORT).show();
}
}else {
Toast.makeText(this,
"Insira o comentário antes de salvar!",
Toast.LENGTH_SHORT).show();
}
//Limpa comentário digitado
editComentario.setText("");
}
@Override
public boolean onSupportNavigateUp() {
finish();
return false;
}
}