I am developing an app the will utilize the data from a real-time database from firebase, and i want to present that data on a recyclerView.
I created a recyclerView with an adapter and all that's necessary for one to work, but im not being able to get the data from the database.
The code i currently have on my adapter is:
public class Adapter extends RecyclerView.Adapter<Adapter.MyViewHolder>{
Context context;
ArrayList<User> list;
public Adapter(Context context, ArrayList<User> list) {
this.context = context;
this.list = list;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View v = LayoutInflater.from(context).inflate(R.layout.item,parent, false);
return new MyViewHolder(v);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
User user = list.get(position);
holder.nome.setText(user.getNome());
holder.local.setText(user.getLocal());
holder.preco.setText(user.getPreco());
}
@Override
public int getItemCount() {
return list.size();
}
public static class MyViewHolder extends RecyclerView.ViewHolder{
TextView nome, local, preco;
public MyViewHolder(@NonNull View itemview){
super(itemview);
nome = itemview.findViewById(R.id.textView1);
local = itemview.findViewById(R.id.textView2);
preco = itemview.findViewById(R.id.textView3);
}
}
}
And this the code i have on the List
public class Userlist extends AppCompatActivity {
RecyclerView recyclerView;
DatabaseReference database;
Adapter adapter;
ArrayList<User> list;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_userlist);
recyclerView = findViewById(R.id.userlist);
//database = FirebaseDatabase.getInstance();
database = FirebaseDatabase.getInstance().getReference("Esp");
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
list = new ArrayList<>();
adapter = new Adapter(this,list);
recyclerView.setAdapter(adapter);
final Context context = this;
database.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot snapshot) {
if (snapshot.exists()){
Toast.makeText(context, "Dados recebidos da Firebase!", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(context, "Nao ha dados recebidos!", Toast.LENGTH_SHORT).show();
}
for (DataSnapshot dataSnapshot : snapshot.getChildren()){
User user = dataSnapshot.getValue(User.class);
list.add(user);
}
adapter.notifyDataSetChanged();
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) { throw databaseError.toException(); }
});
}
}
I tried to change variables, change the path to the database, and still no success. I have checked the permissions on firebase and both read and write are set to true.
This is how i have my data on the realtime database
{
"Esp": {
"andre": {
"local": "Porto",
"nome": "Andre",
"preco": "23"
},
"andre2": {
"local": "Gandra",
"nome": "Andre2",
"preco": "23"
}
}
}
My user class
public class User {
String nome, local, preco;
public String getNome() {
return nome;
}
public String getLocal() {
return local;
}
public String getPreco() {
return preco;
}
}