Recycler View Showing Single Item

Recycler view showing single item

Don't use match_parent for height for your item view. One item fills whole screen vertically so you don't see another.

Only one item displayed in RecyclerView

My_item.xml has width and height of matchparent ,change the height to wrap content.

Recyclerview is displaying only single item

It was a really silly mistake from my side, I was also using a drawer in MainActivity which was overlapping the recyclerview (bad coding by me) because of which I was unable to scroll and it appeared like recyclerview displayed only single item.
Sorry and thanks, everyone.

Recycler View shows identical items

I'll recommend you to directly use the value of position for targetValue, inside onBindViewHolder while setting the value of text.

Recycle view showing single item from firebase realtime database

The adapters from FirebaseUI are designed to show a single child view for each direct child node at the path where you attach the adapter in the database.

In your case, you attach the adapter to /Ingredient, so it creates a single view for each child node of /Ingredient, which is just one child node for hgO9joLhmfh4Wjn7xYpyqcYmNOB3. In that child node you then read handle both its Garlic and Onion properties, but you're setting their values on the same view holder - so you end up only seeing the values from Onion.


If you want to show all ingredients for /Ingredient/hgO9joLhmfh4Wjn7xYpyqcYmNOB3, you should attach the adapter to that path and simplify its handling to:

FirebaseRecyclerOptions<fridgeItem> options =
new FirebaseRecyclerOptions.Builder<fridgeItem>()
.setQuery(fridgeRef.child("hgO9joLhmfh4Wjn7xYpyqcYmNOB3") , fridgeItem.class)
.build();

FirebaseRecyclerAdapter<fridgeItem, fridgeViewHolder> adapter
= new FirebaseRecyclerAdapter<fridgeItem, fridgeViewHolder>(options) {
@Override
protected void onBindViewHolder(@NonNull fridgeViewHolder holder, int position, @NonNull fridgeItem model) {

// String itemName = "Garlic"; TODO: br> String itemExpiry = model.getExpiry();

holder.ingredName.setText(itemName);
holder.ingredExpiry.setText(itemExpiry);
}

@NonNull
@Override
public fridgeViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {

View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.ingredientrecycle, parent, false);
fridgeViewHolder viewHolder = new fridgeViewHolder(view);
return viewHolder;
}
};

myIngredientList.setAdapter(adapter);
adapter.startListening();

You'll still need to get the itemName from the database here. If that's not in your fridgeItem class yet, you can get it with something like getRef(position).getKey() iirc.


If you want to show all ingredients for all users, that doesn't fit with the logic of the adapters in FirebaseUI, and you're probably better off creating your own adapter.

  1. Attach a listener to /Ingredients as you already do.
  2. Loop over the getChildren() of the data snapshot to get the snapshot for each user.
  3. Loop over the children of each user snapshot to get to the individual ingredients.
  4. Add the data for each ingredient to an array list.
  5. Create an adapter on that array list

Only one item displaying in recycler view

Change height of LinearLayout to wrap_content in itemlist.xml

android:layout_height="wrap_content"

Two recycler view on same fragment, holder or db brings wrong data when I select item

i solved my problem but i don't know what i changed on background
i just seperate my one function to two function in MovieListesiViewmodel and gave a parameter and its worked like a miracle

 private fun verileriInternettenAl(denemeInt: Int){
filmYukleniyor.value = true
disposable.add(
filmApiServis.getData(denemeInt)
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(object: DisposableSingleObserver<GetMoviesResponse>(){
override fun onSuccess(t: GetMoviesResponse) {
filmler.value= t.results
filmHataMesaji.value=false
filmYukleniyor.value=false
sqliteSakla(t.results)
// Toast.makeText(getApplication(),"Filmler internetten yüklendi",Toast.LENGTH_LONG).show()
}
override fun onError(e: Throwable) {
filmHataMesaji.value=true
filmYukleniyor.value=false
e.printStackTrace()
}
})
)
}
fun verileriInternettenAl2(){
disposable2.add(
filmApiServis.getVizyondakiler()
.subscribeOn(Schedulers.newThread())
.observeOn(AndroidSchedulers.mainThread())
.subscribeWith(object: DisposableSingleObserver<NowPlaying>(){
override fun onSuccess(t: NowPlaying) {
dizi.value= t.results
filmHataMesaji.value=false
filmYukleniyor.value=false
diziSqLiteSakla(t.results)
}
override fun onError(e: Throwable) {
filmHataMesaji.value=true
filmYukleniyor.value=false
e.printStackTrace()
}
})
)
}

How to flow Data from One RecyclerView to Another RecyclerView when an item in first RecyclerView is clicked?

You can add a list of your recommendations in another RecyclerView
Then in the ViewHolder of each item of the RecyclerView add Callback to listen on click events when a user clicks one of the item like the following


public class ViewHolderClass extends RecyclerView.ViewHolder {

private TextView input_text;
private final Callback callback; // custom callback

public ViewHolderClass(@NonNull View itemView, Callback callback) {
super(itemView);
this.callback = callback;
input_text = itemView.findViewById(R.id.input_text);

// now add onClickListener of the itemView to fire custom callback
itemView.setOnClickListener(view -> {
this.callback.onItemClick(getAdapterPosition());
});
}

// this is my custom callback for return click event
public interface Callback {
void onItemClick(int position);
}
}

Now inside your adapter add the callback from viewholder


public class AutoAdapter extends RecyclerView.Adapter<AutoAdapter.ViewHolderClass> {
private Context context;
private List<Texts> list;
private AutoAdapterCallback callback;

public AutoAdapter(Context context, List<Texts> list) {
this.context = context;
this.list = list;
}

public setCallback(AutoAdapterCallback callback) {
this.callback = callback;
}

@NonNull
@Override
public ViewHolderClass onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.text_list,parent,false);
ViewHolderClass viewHolderClass = new ViewHolderClass(view, new ViewHolderClass.Callback() {
@Override
public void onItemClick(int position) {
// forward callback to adapter callback
if (callback != null) {
// get actual item from its position
final Texts texts = getItemByPosition(position);
// send to adapter callback
callback.onItemClick(texts);
}
});

return viewHolderClass;
}

@Override
public void onBindViewHolder(@NonNull ViewHolderClass holder, @SuppressLint("RecyclerView") int position) {
holder.input_text.setText(list.get(position).getText());

// I haven't used this inteady I added callback on viewholder side
// holder.input_text.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View view) {
// // my stack
// }
// });
}

@Override
public int getItemCount() {
return list.size();
}

// return Texts object by given position
public Texts getItemByPosition(int position) {
return this.list.get(position);
}

// add adapter callback to be called by viewholder
public interface AdapterCallback {
void onItemClick(Texts texts);
}
}

After that, now on your Activity you can easily listen on any item when a user clicks and get its corresponding Texts object from list as following:


//code ...

// here

//Auto texts
autoAdapter = new AutoAdapter(getApplicationContext(),list);

//set callback to listen for click events
autoAdapter.setCallback(new AutoAdapter.AutoAdapterCallback() {
@Override
public void onItemClick(Texts texts) {
// you can get your clicked item here
// now you can put texts object to another RecyclerView :)
}
});

auto_texts.setAdapter(autoAdapter);

//code ...


Related Topics



Leave a reply



Submit