Fragment isn't displaying RecyclerView [duplicate] - android

This question already has answers here:
How to implement Firebase Recycler Adapter in newer version of Android 3.1 and higher?
(2 answers)
Closed 3 years ago.
So, I watched some tutorial on how to do the RecyclerView mainly this. I tweaked the code to fit my needs. But when i try to ran it, the RecyclerView isn't displaying on the fragment. My guess is the problem lies in adapter and viewholder part, because i'm not sure how to implement it yet. Can anyone help me?
The Fragment.java
public class JobListFragment extends Fragment {
RecyclerView mRecyclerView;
FirebaseDatabase firebaseDatabase;
DatabaseReference databaseReference;
public JobListFragment(){
}
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
((MainHomeActivity)getActivity()).setActionBarTitle("Job List");
View view = inflater.inflate(R.layout.fragment_job_list, container, false);
firebaseDatabase = FirebaseDatabase.getInstance();
databaseReference = firebaseDatabase.getReference().child("Data");
mRecyclerView = view.findViewById(R.id.recycleView);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
mRecyclerView.setHasFixedSize(true);
return view;
}
#Override
public void onStart() {
super.onStart();
FirebaseRecyclerOptions<Model> options =
new FirebaseRecyclerOptions.Builder<Model>()
.setQuery(databaseReference, Model.class).build();
FirebaseRecyclerAdapter adapter = new FirebaseRecyclerAdapter<Model, ViewHolder>(options) {
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext())
.inflate(R.layout.row, viewGroup, false);
return new ViewHolder(view);
}
#Override
protected void onBindViewHolder(#NonNull ViewHolder holder, int position, #NonNull Model model) {
holder.setDetails(model.getTitlept(), model.getDescpt(), model.getImagept());
}
};
mRecyclerView.setAdapter(adapter);
}
}
ViewHolder.java
public class ViewHolder extends RecyclerView.ViewHolder {
View mView;
public ViewHolder(View itemView){
super(itemView);
mView = itemView;
}
public void setDetails(String title, String description, String image){
TextView mTitleTv = mView.findViewById(R.id.rTitleTv);
TextView mDescTv = mView.findViewById(R.id.rDescIv);
ImageView mImageIv = mView.findViewById(R.id.rImageIv);
mTitleTv.setText(title);
mDescTv.setText(description);
Picasso.get().load(image).into(mImageIv);
}
}
Model.java
public class Model {
String titlept, imagept, descpt;
public Model(){}
public String getTitlept() {
return titlept;
}
public void setTitlept(String titlept) {
this.titlept = titlept;
}
public String getImagept() {
return imagept;
}
public void setImagept(String imagept) {
this.imagept = imagept;
}
public String getDescpt() {
return descpt;
}
public void setDescpt(String descpt) {
this.descpt = descpt;
}
}

Can you write the problem a little bit more clearly? Picture not displayed? #AdhityoPutro

Related

Why RecylerView.notifyDataSetChanged() stop working after adding Navigation Components?

I implemented Navigation from Android Jetpack, and then ALL RecyclerViews in my app stopped updating after the data changing. In debug mode I saw that datas really changed and notifyDataSetChanged() is invoked, but RVs actually not updated (new items not added).
I've tried to use other methods, such as notifyItemInserted() and so on. Tried to notify the adapter outside. Nothing helps.
Fragment:
...
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.profnastil_fragment, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
...
ImageButton addRoofBtn = view.findViewById(R.id.roof_add_btn);
addRoofBtn.setOnClickListener(v -> {
...
Square roof = new Square(width, height);
mViewModel.addRoof(roof);
});
mViewModel = ViewModelProviders.of(this).get(ProfnastilViewModel.class);
mProfnastilAdapter = new SquaresAdapter(mViewModel, SquaresAdapter.SQUARE_TYPE_ROOF);
RecyclerView roofRv = view.findViewById(R.id.roof_list);
RecyclerView.LayoutManager wallsLayoutManager = new LinearLayoutManager(getContext());
roofRv.setLayoutManager(wallsLayoutManager);
roofRv.setHasFixedSize(true);
roofRv.setAdapter(mProfnastilAdapter);
mViewModel.getRoofs().observe(this, roofs -> mProfnastilAdapter.setSquares(roofs));
}
Adapter:
...
public SquaresAdapter(SquareViewModelBase model, String squareType) {
mModel = model;
mSquares = new ArrayList<>();
setHasStableIds(true);
mSquareType = squareType;
}
public static class SquareHolder extends RecyclerView.ViewHolder {
View view;
TextView heightText;
TextView widthText;
TextView squareText;
private SquareHolder(#NonNull View itemView) {
super(itemView);
view = itemView;
heightText = itemView.findViewById(R.id.square_height);
widthText = itemView.findViewById(R.id.square_width);
squareText = itemView.findViewById(R.id.result_square);
}
}
#Override
public int getItemCount() {
if (mSquares == null) return 0;
return mSquares.size();
}
#Override
public long getItemId(int position) {
return position;
}
#NonNull
#Override
public RecyclerView.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_square, parent, false);
return new SquareHolder(itemView);
}
#Override
public void onBindViewHolder(#NonNull RecyclerView.ViewHolder viewHolder, final int position) {
final Square square = mSquares.get(position);
SquareHolder vh = (SquareHolder) viewHolder;
vh.heightText.setText(StringUtils.format(square.getHeight()));
vh.widthText.setText(StringUtils.format(square.getWidth()));
vh.squareText.setText(StringUtils.format(square.getSquare()));
vh.view.findViewById(R.id.item_del_btn).setOnClickListener(v -> {
mModel.removeSquare(square, mSquareType);
});
}
public void setSquares(List<Square> newSquares) {
mSquares.clear();
mSquares.addAll(newSquares);
notifyDataSetChanged();
}
ViewModel:
...
private MutableLiveData<List<Square>> mRoofs = new MutableLiveData<>();
public void addRoof(Square roof) {
List<Square> roofs = mRoofs.getValue();
if (roofs == null) roofs = new ArrayList<>();
roofs.add(roof);
mRoofs.setValue(roofs);
}
Before I implemented the Navigation graph, everything worked correctly. What happens and how to make it work?
In the end adding view.requireLayout() at the Fragment helped me:
mViewModel.getRoofs().observe(this, roofs -> {
mProfnastilAdapter.setSquares(roofs));
view.requireLayout();
};
But I still don't understand what happened and why it helped me =)
I will be grateful if someone will explain to me.

RecyclerView shows wrong views

RecyclerView shows first items after some correct items. And one of incorrect items can change own data several times... It is really strange and i dont know why, in another app i wrote the same code and all works nice. I tried to find the resolve, but did not found nothing what helps. You can see video to understand what is going on.
video
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable
ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_car_list, container, false);
mRecyclerView = view.findViewById(R.id.car_recycler_view);
mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
updateUI();
return view;
}
private class CarHolder extends RecyclerView.ViewHolder implements
View.OnClickListener {
private Car mCar;
public CarHolder(#NonNull final View itemView) {
super(itemView);
itemView.setOnClickListener(this);
mProducerTextView = itemView.findViewById(R.id.producerCardTextView);
mModelTextView = itemView.findViewById(R.id.modelCardTextView);
mPriceTextView = itemView.findViewById(R.id.priceCardTextView);
}
public void bind(Car car) {
mCar = car;
mProducerTextView.setText(getString(R.string.producer_params,
mCar.getProducer()));
mModelTextView.setText(getString(R.string.model_params,
mCar.getModel()));
mPriceTextView.setText(getString(R.string.price_params,
mCar.getPrice()));
}
#Override
public void onClick(View v) {
mListCallBack.onCarSelected(mCar);
}
}
private class CarAdapter extends RecyclerView.Adapter<CarHolder> {
private List<Car> mCars;
public CarAdapter(List<Car> cars) {
mCars = cars;
}
#NonNull
#Override
public CarHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(R.layout.list_item_car, viewGroup, false);
return new CarHolder(view);
}
#Override
public void onBindViewHolder(#NonNull CarHolder carHolder, int i) {
Car car = mCars.get(i);
carHolder.bind(car);
}
#Override
public int getItemCount() {
return mCars.size();
}
public void setCars(List<Car> cars) {
this.mCars = cars;
}
}
public void updateUI(){
List<Car> cars = CarLab.getCarLab(getActivity()).getCars();
if (mAdapter == null) {
mAdapter = new CarAdapter(cars);
mRecyclerView.setAdapter(mAdapter);
} else {
mAdapter.setCars(cars);
mAdapter.notifyDataSetChanged();
}
}
may be you can try your RecyclerView not to recyle items using:
mRecyclerView.getRecycledViewPool().setMaxRecycledViews(0,0);
in your onCreate() method below mRecyclerView.setLayoutManager(...);
The problem occurs when you use the field variable mCar (which manipulates by all bind events) to update your viewHoleder, just simply use the car variable you pass to the bind to update views.
The other problem with your code is inside onClick, again you should not use the field variable mCar, instead get the item it needs like this:
#Override
public void onClick(View v) {
Car car = mCars.get(getAdapterPosition());
mListCallBack.onCarSelected(car);
}

RecyclerView In Fragment Not Working (Unable to add data to it)

Here is my fragment
public class TransactionsFragment extends Fragment {
private FirebaseAuth mAuth;
private DatabaseReference mDBref;
FirebaseUser Fuser;
private String UID;
private RecyclerView recyclerView;
private RecyclerView.Adapter trpAdapter;
private List<TransGetterSetter> DataList;
String trans_id;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_transactions, container, false);
DataList = new ArrayList<>();
recyclerView = (RecyclerView) v.findViewById(R.id.RVList);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
TransactionRVAdapter trvAdapter = new TransactionRVAdapter(DataList, getContext());
recyclerView.setAdapter(trvAdapter);
mAuth = FirebaseAuth.getInstance();
mDBref = FirebaseDatabase.getInstance().getReference();
Fuser = mAuth.getInstance().getCurrentUser();
UID = Fuser.getUid();
mDBref.child("transactions").child(UID).addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
for (DataSnapshot chidSnap : dataSnapshot.getChildren()) {
trans_id = chidSnap.getKey();
DataList.add(new TransGetterSetter("" + trans_id));
}
}
#Override
public void onCancelled(#NonNull DatabaseError databaseError) {
}
});
trvAdapter.notifyDataSetChanged();
return v;
}
}
If i toast "trans_id" string im getting expected result.
Here is My Adapter class
public class TransactionRVAdapter extends RecyclerView.Adapter<TransactionRVAdapter.rvViewHolder> {
Context mContext;
List<TransGetterSetter> DataList = new ArrayList<>();
public class rvViewHolder extends RecyclerView.ViewHolder{
private TextView TransId;
public rvViewHolder(#NonNull View itemView) {
super(itemView);
TransId = (TextView) itemView.findViewById(R.id.TransIdTxt);
}
}
public TransactionRVAdapter( List<TransGetterSetter> DataList,Context mContext){
this.DataList = DataList;
this.mContext = mContext;
}
#NonNull
#Override
public rvViewHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View view = inflater.inflate(R.layout.fragment_transactions_item, viewGroup, false);
return new rvViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull rvViewHolder rvViewHolder, int i) {
rvViewHolder.TransId.setText(DataList.get(i).getTrans_id());
}
#Override
public int getItemCount() {
return DataList.size();
}
}
Here is my GetterSetter class
public class TransGetterSetter {
private String trans_id;
public TransGetterSetter() {
}
public TransGetterSetter(String trans_id) {
this.trans_id = trans_id;
}
public String getTrans_id() {
return trans_id;
}
public void setTrans_id(String trans_id) {
this.trans_id = trans_id;
}
}
Im able to get Data from firebase, but when i try to add it to recycler view it dosen't show any data. Recycler view is showing empty. I checked my code many time but haven't found solution.
You should call :
trvAdapter.notifyDataSetChanged();
After the for loop inside the (onDataChange) event listener.
Try to called ..
DataList.add(new TransGetterSetter("" + trans_id));
after this line called ..
trvAdapter.notifyDataSetChanged();

Data is not getting retried using firebaseUI [duplicate]

This question already has answers here:
FirebaseRecyclerAdapter unable to populate result [duplicate]
(1 answer)
FirebaseListAdapter not pushing individual items for chat app - Firebase-Ui 3.1
(2 answers)
Closed 5 years ago.
I am following a youtube video in which I was trying to retrieve data using FirebaseUI but I am not able to show any data in the recyclerview.
Here is the code
public class MemeFragment extends Fragment {
RecyclerView recyclerView;
FirebaseDatabase firebaseDatabase;
DatabaseReference myref;
FirebaseRecyclerAdapter<Datafileinfo, ShowDataViewHolder> firebaseRecyclerAdapter;
FirebaseRecyclerOptions<Datafileinfo> options;
public MemeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_meme, container, false);
firebaseDatabase = FirebaseDatabase.getInstance();
myref = FirebaseDatabase.getInstance().getReference("memes");
recyclerView = (RecyclerView) view.findViewById(R.id.recyc);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
options = new FirebaseRecyclerOptions.Builder<Datafileinfo>()
.setQuery(myref, Datafileinfo.class)
.build();
return view;
}
#Override
public void onStart() {
super.onStart();
firebaseRecyclerAdapter = new FirebaseRecyclerAdapter<Datafileinfo, ShowDataViewHolder>
(options) {
#Override
protected void onBindViewHolder(#NonNull ShowDataViewHolder holder, int position, #NonNull Datafileinfo model) {
holder.ImageTitle(model.getTitle());
holder.ImageUrl(model.getImage());
}
#Override
public ShowDataViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.custo, parent, false);
return new ShowDataViewHolder(view);
}
};
recyclerView.setAdapter(firebaseRecyclerAdapter);
}
public static class ShowDataViewHolder extends RecyclerView.ViewHolder {
private ImageView imageView;
private TextView textView;
public ShowDataViewHolder(View itemView) {
super(itemView);
imageView = (ImageView) itemView.findViewById(R.id.imageView);
textView = (TextView) itemView.findViewById(R.id.textView);
}
private void ImageTitle(String title) {
textView.setText(title);
}
private void ImageUrl(String url) {
Glide.with(itemView.getContext()).
load(url).
into(imageView);
}
}
}
and datafileinfo is the getter and setter class I have also taken the internet permission.Note I am able to get the single value from firebase but for multiple values, I am facing a problem this is the database.
where I am doing wrong any idea??
Note I have followed and gone through all the tutorials and similar post.
Create a separate Class for your ViewHolder
public class ShowDataViewHolder extends RecyclerView.ViewHolder{
private ImageView imageView;
private TextView textView;
public ShowDataViewHolder (View itemView) {
super(itemView);
imageView = (ImageView)itemView.findViewById(R.id.imageView);
textView = (TextView) itemView.findViewById(R.id.textView);
}
}
In your MenuFragment add
DatabaseReference mDatabaseMemes= //your reference to memes node
FirebaseRecyclerAdapter<yourModel, ShowDataViewHolder> adapter= new FirebaseRecyclerAdapter<yourModel, ShowDataViewHolder>(yourModel.class, R.layout.yourLayout,
ShowDataViewHolder.class,mDatabaseMemes) {
#Override
protected void populateViewHolder(ShowDataViewHolder viewHolder, yourModel model, int position) {
viewHolder.textView.setText(model.getTitle());
Glide.with(context)
.load(model.getImage())
.fitCenter()
.into(viewHolder.imageView);
}
};
recyclerView.setAdapter(adapter);
}
MAKE SURE your model class has String title and image same as your child names under memes node.

Need retrieve data from FirebaseDatabase to Fragment

I'm developing simple project. CategoryFragment is the first added fragment in MainActivity. I expect receive data from FireBase and display it with RecyclerView.
Below my code. The app doesn't fall during launching. There is no any error.
I guess I should use some method for my purpose, but I'm new in Firebase.
public class CategoryFragment extends Fragment {
public DatabaseReference mRootDB;
private RecyclerView mRecyclerView;
public FirebaseRecyclerAdapter<Category, CategoryViewHolder> adapter;
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mRootDB = FirebaseDatabase.getInstance().getReference().child("categories");
adapter = new FirebaseRecyclerAdapter <Category, CategoryViewHolder>(
Category.class,
R.layout.model_category,
CategoryViewHolder.class,
mRootDB)
{
#Override
protected void populateViewHolder(CategoryViewHolder viewHolder, Category model, int position) {
viewHolder.setCategory(model.getName());
}
};
mRecyclerView.setAdapter(adapter);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_category, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.category_RV);
return rootView;
}
public static class CategoryViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView category_CV;
public CategoryViewHolder(View itemView) {
super(itemView);
mView = itemView;
category_CV = (TextView) mView.findViewById(R.id.category_CV);
}
public void setCategory(String data){
category_CV.setText(data);
}
}
}
I would not override onActivityCreated() if I were you, you can create and return the RecyclerView in the onCreateView(). Are you sure you have data in Firebase? You can override parseSnapshot() like this and maybe put a break point to make sure that something is coming back, and if it does then you may want to check your RecyclerView custom layout.
public class CategoryFragment extends Fragment {
public DatabaseReference mRootDB;
private RecyclerView mRecyclerView;
public FirebaseRecyclerAdapter<Category, CategoryViewHolder> adapter;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.frag_category, container, false);
mRecyclerView = (RecyclerView) rootView.findViewById(R.id.category_RV);
mRootDB = FirebaseDatabase.getInstance().getReference().child("categories");
adapter = new FirebaseRecyclerAdapter <Category, CategoryViewHolder>(
Category.class,
R.layout.model_category,
CategoryViewHolder.class,
mRootDB)
{
#Override
protected Category parseSnapshot(DataSnapshot snapshot) {
Category category = super.parseSnapshot(snapshot);
if (category != null){
category.setId(snapshot.getKey());
}
return category;
}
#Override
protected void populateViewHolder(CategoryViewHolder viewHolder, Category model, int position) {
viewHolder.setCategory(model.getName());
}
};
mRecyclerView.setAdapter(adapter);
return rootView;
}
public static class CategoryViewHolder extends RecyclerView.ViewHolder {
View mView;
TextView category_CV;
public CategoryViewHolder(View itemView) {
super(itemView);
mView = itemView;
category_CV = (TextView) mView.findViewById(R.id.category_CV);
}
public void setCategory(String data){
category_CV.setText(data);
}
}
}

Categories

Resources