Reputation: 51
I have gone through most of the topics related to it and couldn't find the proper solution.
I am getting an error "E/RecyclerView: No adapter attached; skipping layout" when retrieving the data from firebase database to my fragments using recycler view.
Please see the code below:
Resources :
Fragment file
import android.os.Bundle;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import java.util.List;
public class TempFragment extends Fragment {
private RecyclerView rcv;
private ArrayList<TempModel> tempData;
private DatabaseReference databaseReference;
public TempFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_temp, container, false);
rcv = (RecyclerView) v.findViewById(R.id.recycle_template);
rcv.setLayoutManager(new GridLayoutManager(getContext(),3));
rcv.setHasFixedSize(true);
fireAdapter();
return v;
}
public void fireAdapter(){
tempData = new ArrayList<>();
databaseReference = FirebaseDatabase.getInstance().getReference().child("images");
databaseReference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()){
TempModel tempModel = postSnapshot.getValue(TempModel.class);
tempData.add(tempModel);
}
TempAdapter tempAdapter = new TempAdapter(getContext(),tempData);
rcv.setAdapter(tempAdapter);
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
Toast.makeText(getContext(),databaseError.getMessage(),Toast.LENGTH_SHORT).show();
}
});
}
}
Adapter
public class TempAdapter extends RecyclerView.Adapter<TempAdapter.MyViewHol> {
private Context context;
private ArrayList<TempModel> data ;
public TempAdapter(Context context, ArrayList<TempModel> data) {
this.context = context;
this.data = data;
}
@NonNull
@Override
public MyViewHol onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view ;
LayoutInflater layoutInflater = LayoutInflater.from(context);
view = layoutInflater.inflate(R.layout.cardview_item,parent,false);
return new MyViewHol(view);
}
@Override
public void onBindViewHolder(@NonNull MyViewHol holder, int position) {
holder.textView.setText(data.get(position).getName());
Picasso.get().load(data.get(position).getImageUrl()).fit().centerCrop().into(holder.imageView);
// holder.imageView.setImageResource(data.get(position).getThumbnail());
}
@Override
public int getItemCount() {
return data.size();
}
public static class MyViewHol extends RecyclerView.ViewHolder{
TextView textView ;
ImageView imageView;
public MyViewHol(@NonNull View itemView) {
super(itemView);
textView = (TextView) itemView.findViewById(R.id.temp_title);
imageView = (ImageView)itemView.findViewById(R.id.temp_img);
}
}
}
Model
public class TempModel {
private String name;
private String imageUrl;
public TempModel(){
}
public TempModel(String name, String imageUrl) {
this.name = name;
this.imageUrl = imageUrl;
}
public String getName() {
return name;
}
public String getImageUrl() {
return imageUrl;
}
public void setName(String name) {
this.name = name;
}
public void setImageUrl(String imageUrl) {
this.imageUrl = imageUrl;
}
}
I know there are few things i'm missing on it. It will be great if anyone help on it.
Upvotes: 0
Views: 167
Reputation: 2966
You have to set Adapter inside onCreateView
then when new data arrives simply call adapter to notifyDataSetChaged
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_temp, container, false);
youradapter = new TempAdapter(getContext(),new ArrayList<TempModel>());
rcv = (RecyclerView) v.findViewById(R.id.recycle_template);
rcv.setAdapter();
rcv.setLayoutManager(new GridLayoutManager(getContext(),3));
rcv.setHasFixedSize(true);
fireAdapter();
return v;
}
Then inside onDataChange
callback
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()){
TempModel tempModel = postSnapshot.getValue(TempModel.class);
tempData.add(tempModel);
}
TempAdapter tempAdapter = new TempAdapter(getContext(),tempData);
yourAdapter.setData(tempData);
}
Then inside your TempAdapter
class create setData
function
public void setData(List<TempModel> list) {
if (data != null) data.clear();
if (data == null) data = new ArrayList<>();
data.addAll(list);
notifyDataSetChanged();
}
Upvotes: 1
Reputation: 21
Use Debugger to check if you are getting data from firebase database? Or try to Init your Adaptor in OnCreateview method and use notifydatasetchanged()
Upvotes: 2