I'm new here and I do not know where the question is exactly.
I'm doing a project with Android Studio and Firebase integrated, the thing is that I want to show Firebase data in a recycler view and I do not know why it does not show anything, only the blank window.
I have created the object with the firebase data, the adapter, the layout that will show the fireview and such but shows nothing
Deputy code:
Adapter.
public class Adapter extends RecyclerView.Adapter<Adapter.ComercioViewHolder>{
List<Comercio> lista_comercios;
public Adapter(List<Comercio> lista_comercios) {
this.lista_comercios = lista_comercios;
}
@NonNull
@Override
public ComercioViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int i) {
View v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_recycler, parent, false);
ComercioViewHolder holder = new ComercioViewHolder(v);
return holder;
}
@Override
public void onBindViewHolder(@NonNull ComercioViewHolder holder, int position) {
Comercio comercio = lista_comercios.get(position);
holder.textViewnombre.setText(comercio.getNombre());
holder.textViewdireccion.setText(comercio.getDireccion());
}
@Override
public int getItemCount() {
return lista_comercios.size();
}
public static class ComercioViewHolder extends RecyclerView.ViewHolder{
TextView textViewnombre, textViewdireccion;
public ComercioViewHolder(@NonNull View itemView) {
super(itemView);
textViewnombre = itemView.findViewById(R.id.textview_nombre);
textViewdireccion= itemView.findViewById(R.id.textview_direccion);
}
}
}
row_recycler.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="@+id/textview_nombre"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/textview_direccion"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
activity_main.
package com.moisescapel.lucenamarket2;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;
import com.moisescapel.lucenamarket2.Objetos.Adapter;
import com.moisescapel.lucenamarket2.Objetos.Comercio;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
RecyclerView rv ;
List<Comercio> lista_comercios;
Adapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
rv = (RecyclerView) findViewById(R.id.recycler);
rv.setLayoutManager(new LinearLayoutManager(this));
lista_comercios = new ArrayList<>();
FirebaseDatabase database = FirebaseDatabase.getInstance();
adapter = new Adapter(lista_comercios);
rv.setAdapter(adapter);
database.getReference().getRoot().addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
lista_comercios.removeAll(lista_comercios);
for (DataSnapshot snapShot:
dataSnapshot.getChildren()){
Comercio comercio = snapShot.getValue(Comercio.class);
lista_comercios.add(comercio);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
}
}
I would appreciate if you could help me