简体   繁体   中英

Firebase data to RecyclerView shows only one item

I'm retrieving data from firebase and trying to display that in recycler view, every thing is fine but it only shows one item in the list (there are total 14 items)

comment if you do not understand the question

This is my main activity where i get datasnapshots from firebase

public class cakes_activity extends AppCompatActivity {
RecyclerView recyclerView;
ArrayList<Data> listdata;
private DatabaseReference mref;
private  NorthAdaptor north;
private final Context context = this;


public int counter = 0;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_cakes);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    EditText editText = (EditText) findViewById(R.id.editText) ;
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    //FIREBASE
    recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
    mref= FirebaseDatabase.getInstance().getReference("Cakes");
    mref.addChildEventListener(new ChildEventListener() {
        @Override
        public void onChildAdded(DataSnapshot dataSnapshot, String s) {
            Data data = dataSnapshot.getValue(Data.class);
            listdata = new ArrayList<>();
            listdata.add(data);
            north = new NorthAdaptor(context,listdata);
            recyclerView.setLayoutManager(new LinearLayoutManager(context));
            recyclerView.hasFixedSize();
            recyclerView.setAdapter(north);

        }

        @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) {



        }
    });

}

and the following code is my adaptor class:

public class NorthAdaptor extends RecyclerView.Adapter<NorthAdaptor.NorthHolder> {
    private LayoutInflater inflater;
    ArrayList<Data> northy;

    public NorthAdaptor(Context context,ArrayList<Data> northy)
    {
        inflater=LayoutInflater.from(context);
        this.northy=northy;

    }
    public NorthHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.each_item,parent,false);
        NorthHolder holder = new NorthHolder(view);
        return holder;

    }

    @Override
    public void onBindViewHolder(NorthHolder holder, int position) {
        holder.text2.setText(northy.get(position).getName());
        holder.text3.setText("₹."+northy.get(position).getRS());

    }

    @Override
    public int getItemCount() {

        return northy.size();
    }
    class NorthHolder extends RecyclerView.ViewHolder implements View.OnClickListener
    {

        TextView text,text2,text3;
        ImageView img;
        private final Context context;
        public NorthHolder(View itemview)
        {
            super(itemview);
            context = itemView.getContext();
            CardView card = (CardView) itemView.findViewById(R.id.card);
            text3=(TextView)itemView.findViewById(R.id.Rs);
            text2 = (TextView) itemView.findViewById(R.id.itemtext2);
            img = (ImageView) itemView.findViewById(R.id.itemimage);
            card.setOnClickListener(this);

        }
        @Override
        public void onClick(View view){
            Toast.makeText(
                    cakes_activity.this,
                    "Your message here",
                    Toast.LENGTH_SHORT
            ).show();

        }
    }
}

this is my java class to store the data received from firebase

public class Data {
private String ID,Name,image,RS;
public Data(){

}

public Data(String ID, String Name, String image, String RS) {
    this.ID = ID;
    this.Name = Name;
    this.image= image;
    this.RS = RS;
}

public String getId() {
    return ID;
}
public String getRS() {
    return RS;
}

public void setId(String id) {

    this.ID = id;
}

public String getName() {
    return Name;
}

public void setName(String Name) {
    this.Name = Name;
}

public String getimage() {
    return image;
}

public void setImg(String image) {
    this.image = image;
}

}

listdata = new ArrayList<>();

        north = new NorthAdaptor(context,listdata);
        recyclerView.setLayoutManager(new LinearLayoutManager(context));
        recyclerView.hasFixedSize();
        recyclerView.setAdapter(north);

you need to write this code in onCreate instead of onchildadded

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM