Answer the question
In order to leave comments, you need to log in
How to use data from FireBase database in RecyclerAdapter?
I have already looked at some information about the RecyclerAdapter, but unfortunately most of the sources are in English and I and the translator do not understand some aspects. So here is my database
RecyclerAdapter classes in it:
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = inflater.inflate(R.layout.coupon,parent,false);
return new ViewHolder(view);
public void onBindViewHolder(ViewHolder viewHolder, int position) {
Coupons coupons = coupons2.get(position);
viewHolder.imageView.setImageResource(coupons.getLogo());
viewHolder.nameView.setText(coupons.getName());
viewHolder.sumView.setText(coupons.getSum());
}
public int getItemCount(){
return coupons2.size();
}
static class ViewHolder extends RecyclerView.ViewHolder{
final ImageView imageView;
final TextView nameView,sumView;
ViewHolder(View v){
super(v);
imageView = (ImageView) v.findViewById(R.id.logo);
nameView = (TextView) v.findViewById(R.id.name);
sumView = (TextView)v.findViewById(R.id.sum);
}
class RecyclerAdapter extends RecyclerView.Adapter<RecyclerAdapter.ViewHolder> {
private LayoutInflater inflater;
private List<Coupons> coupons2;
RecyclerAdapter(Context context, List<Coupons> coupons2){
this.coupons2 = coupons2;
this.inflater = LayoutInflater.from(context);
}
@Override
public RecyclerAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType){
View view = inflater.inflate(R.layout.coupon,parent,false);
return new ViewHolder(view);
}
@Override
public void onBindViewHolder(ViewHolder viewHolder, int position) {
Coupons coupons = coupons2.get(position);
viewHolder.imageView.setImageResource(coupons.getLogo());
viewHolder.nameView.setText(coupons.getName());
viewHolder.sumView.setText(coupons.getSum());
}
@Override
public int getItemCount(){
return coupons2.size();
}
static class ViewHolder extends RecyclerView.ViewHolder{
final ImageView imageView;
final TextView nameView,sumView;
ViewHolder(View v){
super(v);
imageView = (ImageView) v.findViewById(R.id.logo);
nameView = (TextView) v.findViewById(R.id.name);
sumView = (TextView)v.findViewById(R.id.sum);
}
}
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_library);
setInitialData();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
RecyclerAdapter adapter = new RecyclerAdapter(this,couponss);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("Сoupons");
FirebaseRecyclerOptions<Coupons> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<Coupons>()
.setQuery(query, Coupons.class)
.build();
List<Coupons> couponss = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_library);
setInitialData();
RecyclerView recyclerView = (RecyclerView) findViewById(R.id.list);
RecyclerAdapter adapter = new RecyclerAdapter(this,couponss);
recyclerView.setAdapter(adapter);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
DatabaseReference rootRef = FirebaseDatabase.getInstance().getReference();
Query query = rootRef.child("Сoupons");
FirebaseRecyclerOptions<Coupons> firebaseRecyclerOptions = new FirebaseRecyclerOptions.Builder<Coupons>()
.setQuery(query, Coupons.class)
.build();
}
private void setInitialData(){
couponss.add(new Coupons("Лента","-500",R.drawable.aaaaaaaaaaa));
}
public class Coupons {
private String name;
private int logo;
private String sum;
public Coupons(String name, String sum, int logo) {
this.name = name;
this.sum = sum;
this.logo = logo;
}
public String getName() {
return this.name;
}
public void setName(String name) {
this.name = name;
}
public String getSum(){
return this.sum;
}
public void setSum(String sum){
this.sum = sum;
}
public int getLogo(){
return this.logo;
}
public void setLogo(int logo){
this.logo = logo;
}
@Exclude
public Map<String, Object> toMap() {
HashMap<String, Object> result = new HashMap<>();
result.put("name", name);
result.put("sum", sum);
result.put("logo", logo);
return result;
}
Answer the question
In order to leave comments, you need to log in
It is not enough to simply update the data in the list. You also need to notify the adapter about this. In the setInitialData method. Pull the notifyDatasetChanged method.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question