App Screen
I want to automatically get the sum of the items once added in the recycler view and at the same time get the sum of the items when the Quantity changes and when an item from the recyclerView is removed. I am new in Android, and I cannot think of any way to achieve this. I am thinking something like the code below, but I don't know how to implement it.
TextView ProductPrice = itemView.findViewById(R.id.productPricelbl);
TextView QTY = itemView.findViewById(R.id.Quantity);
Int total;
private void GetTotal(){
total += Integer.parseInt(ProductPrice.getText().toString()) * Integer.parseInt(QTY .getText().toString())
}
I am using a custom adapter for my recycler view with the code below
public class ListViewAdapter extends RecyclerView.Adapter<DemoVH>{
ArrayList<ProductListViewModel> productList;
Context mContext;
public ListViewAdapter(ArrayList<ProductListViewModel> productList, Context context) {
this.productList = productList;
this.mContext = context;
}
#NonNull
#Override
public DemoVH onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recycler_view_row,parent,false);
return new DemoVH(view).linkadapter(this);
}
#Override
public void onBindViewHolder(#NonNull DemoVH holder, int position) {
final ProductListViewModel productListViewModel = productList.get(position);
holder.Product_Name.setText(productListViewModel.getProductName());
holder.Product_Barcode.setText(productListViewModel.getProductBarcode());
holder.Product_Price.setText(productListViewModel.getProductPrice());
}
#Override
public int getItemCount() {
return productList.size();
}
}
class DemoVH extends RecyclerView.ViewHolder{
TextView Product_Name;
TextView Product_Barcode;
TextView Product_Price;
TextView Product_QTY;
ImageView addQTY;
ImageView lessQTY;
private ListViewAdapter myAdapter;
int Quantity = 1;
public DemoVH(#NonNull View itemView) {
super(itemView);
Product_Name = itemView.findViewById(R.id.ProductNameLbl);
Product_Barcode = itemView.findViewById(R.id.ProductBarcodeLbl);
Product_Price = itemView.findViewById(R.id.ProductPriceLbl);
Product_QTY = itemView.findViewById(R.id.QTYLbl);
addQTY = itemView.findViewById(R.id.AddQTYBtn);
lessQTY = itemView.findViewById(R.id.lessQTYBtn);
itemView.findViewById(R.id.RemoveBtn).setOnClickListener(view -> {
myAdapter.productList.remove(getAdapterPosition());
myAdapter.notifyItemRemoved(getAdapterPosition());
});
addQTY.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Quantity = Quantity+=1;
Product_QTY.setText(String.valueOf(Quantity));
}
});
lessQTY.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(Quantity == 1){
Product_QTY.setText("1");
}else{
Quantity = Quantity-=1;
Product_QTY.setText(String.valueOf(Quantity));
}
}
});
}
public DemoVH linkadapter(ListViewAdapter adapter){
myAdapter = adapter;
return this;
}
}
You can take a global variable in recycler view adapter and keep adding the amount in that variable in onBindViewHolder().
And when any item is increased or decreased or removed you can add or decrease that amount from that global variable.
Related
I have two recycler views and both of them use the same custom adapter.
https://imgur.com/3WMGgiL - this is how the item from the recycler view looks like
What i want to do is, whenever the "Anuleaza rezervarea" button is clicked, i want to remove the item from the current recyclerview and add it to another recycler view
How can i do that? I'll leave my custom adapter code below. I really don't understand what to write in the on click listener.
public class RecyclerViewAdapterRezervare extends RecyclerView.Adapter<RecyclerViewAdapterRezervare.RecycleViewHolder> {
private List<Rezervare> rezervari;
private Context context;
private LayoutInflater inflater;
private String numeUtilizator;
private String status;
private Rezervare rezervare = new Rezervare();
private DatabaseReference reff;
public RecyclerViewAdapterRezervare(List<Rezervare> rezervari, Context context, String numeUtilizator) {
this.rezervari = rezervari;
this.context = context;
this.numeUtilizator = numeUtilizator;
this.inflater = LayoutInflater.from(context);
}
#NonNull
#Override
public RecycleViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.rezervari_layout, parent, false);
return new RecycleViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull RecycleViewHolder holder, int position) {
rezervare = rezervari.get(position);
holder.btn_anuleaza.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new AlertDialog.Builder(view.getContext()).setTitle("Anulare rezervare").setMessage("Sunteti sigur ca doriti anularea rezervarii?")
.setPositiveButton(R.string.da, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
holder.btn_anuleaza.setText(R.string.anulat);
holder.btn_anuleaza.setBackgroundColor(view.getResources().getColor(R.color.red));
}
}).setNegativeButton(R.string.nu, null).show();
}
});
}
#Override
public int getItemCount() {
return rezervari.size();
}
public static class RecycleViewHolder extends RecyclerView.ViewHolder {
public TextView tv_nume_teren;
public TextView tv_adresa_teren;
public TextView tv_ora;
public TextView tv_data;
public Button btn_anuleaza;
public Button btn_navigheaza;
public static boolean isCanceled;
public RecycleViewHolder(#NonNull View itemView) {
super(itemView);
tv_nume_teren = itemView.findViewById(R.id.tv_rezervare_nume_teren);
tv_adresa_teren = itemView.findViewById(R.id.tv_rezervare_adresa_teren);
btn_anuleaza = itemView.findViewById(R.id.btn_anuleaza);
btn_navigheaza = itemView.findViewById(R.id.btn_maps);
tv_ora = itemView.findViewById(R.id.tv_rezervare_ore);
tv_data = itemView.findViewById(R.id.tv_rezervare_data);
isCanceled = false;
if(isCanceled){
btn_anuleaza.setText(R.string.anulat);
btn_anuleaza.setBackgroundColor(itemView.getResources().getColor(R.color.red));
} else {
btn_anuleaza.setText(R.string.anuleaza_rezervare);
btn_anuleaza.setBackgroundColor(itemView.getResources().getColor(R.color.teal_700));
}
}
}
When i click on the "Anuleaza rezervare" button, the two buttons become red and unclickable, and i want to transfer the item to another recycler view. How can i do that? Thank you!
You can achieve this by calling,
int removeIndex = 2;
data.remove(removeIndex);
adapter.notifyItemRemoved(removeIndex);
on the first recyclerView and update the second by calling,
String newItem = "This is a new item.";
int updateIndex = 3; // Your index to insert new the item
data.set(updateIndex, newItem);
adapter.notifyItemChanged(updateIndex);
for the second recyclerView.
i have a recyclerview in my activity and in each item i have two button and a ordernumber when minus bottom make zero the ordernumber, i want to hide this item and other item come up and fill the blank space.I hide the item with setVisibility() but i don`t know how to handle blank space.
this is my recyclerview adapter:
package com.test.mohammaddvi.snappfood.Adapter;
public class RecyclerViewBuyBasketAdapter extends RecyclerView.Adapter<RecyclerViewBuyBasketAdapter.SingleItemBuyBasket> {
private ArrayList<FinalFood> foodList;
private Context mContext;
private View view;
public RecyclerViewBuyBasketAdapter(ArrayList<FinalFood> foodList, Context mContext) {
this.foodList = foodList;
this.mContext = mContext;
}
#NonNull
#Override
public RecyclerViewBuyBasketAdapter.SingleItemBuyBasket onCreateViewHolder(ViewGroup parent, int viewType) {
view = LayoutInflater.from(parent.getContext()).inflate(R.layout.buybasketitem, null);
return new RecyclerViewBuyBasketAdapter.SingleItemBuyBasket(view);
}
#Override
public void onBindViewHolder(final RecyclerViewBuyBasketAdapter.SingleItemBuyBasket holder, int position) {
FinalFood food = foodList.get(position);
holder.foodName.setText(food.getName());
holder.foodDetails.setText(food.getDetails());
holder.foodPrice.setText(food.getPrice());
holder.foodOrderNumber.setText(food.getOrdernumber() + "");
handleClick(holder, view);
}
private void handleClick(final RecyclerViewBuyBasketAdapter.SingleItemBuyBasket holder, final View view) {
holder.foodPlusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FinalFood food = foodList.get(holder.getAdapterPosition());
int orderNumber = food.getOrdernumber();
int newOrderNumber = orderNumber + 1;
food.setOrdernumber(newOrderNumber);
holder.foodOrderNumber.setText(newOrderNumber + "");
}
});
holder.foodMinusButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FinalFood food = foodList.get(holder.getAdapterPosition());
int orderNumber = food.getOrdernumber();
if (orderNumber == 1) {
int newOrderNumber = orderNumber - 1;
food.setOrdernumber(newOrderNumber);
view.setVisibility(View.GONE);
} else {
int newOrderNumber = orderNumber - 1;
food.setOrdernumber(newOrderNumber);
holder.foodOrderNumber.setText(newOrderNumber + "");
}
}
});
}
#Override
public int getItemCount() {
return (null != foodList ? foodList.size() : 0);
}
public class SingleItemBuyBasket extends RecyclerView.ViewHolder {
TextView foodName;
TextView foodPrice;
Button foodPlusButton;
Button foodMinusButton;
TextView foodOrderNumber;
TextView foodDetails;
SingleItemBuyBasket(View itemView) {
super(itemView);
this.foodName = itemView.findViewById(R.id.foodNameInBuyBasket);
this.foodPrice = itemView.findViewById(R.id.foodPriceInBuyBasket);
this.foodDetails = itemView.findViewById(R.id.foodDetailsInBuyBasket);
this.foodPlusButton = itemView.findViewById(R.id.plusbuttonInBuyBasket);
this.foodMinusButton = itemView.findViewById(R.id.minusbuttonInBuyBasket);
this.foodOrderNumber = itemView.findViewById(R.id.ordernumberInBuyBasket);
}
}
}
and this is my activity:
package com.test.mohammaddvi.snappfood;
public class BuyBasket extends AppCompatActivity{
ArrayList<FinalFood> foods = new ArrayList<>();
RecyclerViewBuyBasketAdapter recyclerViewBuyBasketAdapter;
RecyclerView recyclerView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_buy_basket);
recyclerView = findViewById(R.id.recyclerview);
foods= (ArrayList<FinalFood>) getIntent().getSerializableExtra("final");
recyclerViewBuyBasketAdapter = new RecyclerViewBuyBasketAdapter(foods, BuyBasket.this);
recyclerView.setLayoutManager(new LinearLayoutManager(BuyBasket.this, LinearLayoutManager.VERTICAL, false));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(recyclerViewBuyBasketAdapter);
}
}
It's better you delete the entire row rather than changing visibility. This is the code to remove the row. You need to call removeItem(position); method in your button onclickListener
public void removeItem(int position)
{
// Remove specified position
models.remove(position);
// Notify adapter to remove the position
notifyItemRemoved(position);
// Notify adapter about data changed
notifyItemChanged(position);
// Notify adapter about item range changed
notifyItemRangeChanged(position, arraylist.size());
}
Inside your xml, define two parents. One for the hidden content and the other for the item that you want to appear when you hide the first one
Try playing with View when clicking in one parent or the other:
layout.setVisibility(View.GONE) or view.setVisibility(View.VISIBLE);
I'm currently working on android using RecyclerView, let's say I have 2 TextView in my custom row, I wanted to dynamically change the text of one in the TextView, how can I do that?
I do have the following code in my MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private AdaptersOnline adaptersOnline;
private RecyclerView.LayoutManager mLayoutManager;
private List<ModelClientInformation> modelOnlineLists = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
mLayoutManager = new LinearLayoutManager(this);
adaptersOnline = new AdaptersOnline(this, modelOnlineLists);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adaptersOnline);
}
//call this to new row
public void initializeClient(String id, String data1, String data2){
this.modelOnlineLists.add(new ModelClientInformation(id, data1, data2));
adaptersOnline.notifyDataSetChanged();
}
//Call this method to update textview
public void updateSpecificViewItem(String theID){
//get position base on the ID
adaptersOnline.updateTextView(
adaptersOnline.getPositionBaseOnItemID(theID));
}
}
in my Adapter Class
public class AdaptersOnline extends RecyclerView.Adapter<AdaptersOnline.TheViewHolder> {
Context mContext;
public List<ModelClientInformation> onlineList;
public AdaptersOnline(Context mContext, List<ModelClientInformation> modelOnlineList){
this.mContext = mContext;
this.onlineList = modelOnlineList;
}
public class TheViewHolder extends RecyclerView.ViewHolder {
TextView text1, text2;
public TheViewHolder(View view) {
super(view);
text1 = (TextView)view.findViewById(R.id.text1);
text2 = (TextView)view.findViewById(R.id.text2);
}
}
#Override
public TheViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_row, parent, false);
return new TheViewHolder(itemView);
}
#Override
public void onBindViewHolder(TheViewHolder holder, int position) {
final ModelClientInformation info = onlineList.get(position);
holder.text1.setText(holder.getText1());
holder.text1.setText(holder.getText2());
}
/**Function to Return the size of List**/
#Override
public int getItemCount() {
return onlineList.size();
}
/**Function to Clear the List**/
public void clear(){
onlineList.clear();
}
/**Possibly way to update one of the TextView here**/
public void updateTextView(int position){
//what should I do to update the TextView
}
/*Get the position of item inside data list base on the given ID*/
public int getPositionBaseOnItemID(String theID) {
int length = onlineList.size();
for (int i = 0; i < length; i ++) {
if(onlineList.get(i).getItemID().equals(theID)) {
return i;
}
}
return -1; //Item not found
}
}
and the Pojo
public ModelClientInformation class{
private String theID, text1, text2;
public ModelClientInformation(String theID, String text1, String text2){
this.theID = theID;
this.text1 = text1;
this.text2 = text2;
}
public String getItemID(){
return theID;
}
public String getText1(){
return text1;
}
public String getText2(){
return text2;
}
}
I'm don't have any idea to do it. . .
Anyone can help me?
UPDATE:
Please look at my changes,
1: I want to update one of the TextView inside MainActivity class by calling updateSpecificViewItem("theID").
2: Get the position of the item base on the given id by calling getPositionBaseOnItemID("theID").
3: To finally update the specific item, I want to call updateTextView(int position) method.
the only problem I'm facing right now is the number 3, how can I update only the text2 and not the entire item?
You need to override onBindViewHolder (TheViewHolder holder, int position, List payload)
#Override
public void onBindViewHolder(HelloViewHolder holder, int position, List<Object> payload) {
if (payloads.isEmpty()) {
super.onBindViewHolder(holder, position , payloads);
}else{
for (Object payload : payloads) {
if (payload instanceof String) {
holder.textView.setText(payload.toString)
}
}
}
}
And to update your textView you just need to call
adapter.notifyItemChanged(position , "an string for example")
This gives you a partial update of your view.
Hope this helps.
Solving my problem
MainActivity
public class MainActivity extends AppCompatActivity {
private RecyclerView recyclerView;
private AdaptersOnline adaptersOnline;
private RecyclerView.LayoutManager mLayoutManager;
private List<ModelClientInformation> modelOnlineLists = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mContext = getApplicationContext();
mLayoutManager = new LinearLayoutManager(this);
adaptersOnline = new AdaptersOnline(this, modelOnlineLists);
recyclerView = (RecyclerView)findViewById(R.id.recyclerView);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adaptersOnline);
}
//call this to new row
public void initializeClient(String id, String data1, String data2){
this.modelOnlineLists.add(new ModelClientInformation(id, data1, data2));
adaptersOnline.notifyDataSetChanged();
}
//Call this method to update specific Item
public void updateSpecificViewItem(String theID, String newText){
int position = adaptersOnline.getPositionBaseOnItemID(theID); // get position base on the ID
ModelClientInformation oldItem = adaptersOnline.getOnlineList().get(position); // From my Adapter I created a new method `getOnlineList()` that returns the list item of specific position.
ModelClientInformation newItem = new ModelClientInformation(
oldItem.ItemID(), // get and add the old Item ID
oldItem.getText1(), // Get and add the old Text1
newText // add the new text for text2
);
adaptersOnline.updateTextView(position, newItem); // call updateTextView() from the adapter and pass the position and the newItem.
}
}
Adapter
public class AdaptersOnline extends RecyclerView.Adapter<AdaptersOnline.TheViewHolder> {
Context mContext;
public List<ModelClientInformation> onlineList;
public AdaptersOnline(Context mContext, List<ModelClientInformation> modelOnlineList){
this.mContext = mContext;
this.onlineList = modelOnlineList;
}
public class TheViewHolder extends RecyclerView.ViewHolder {
TextView text1, text2;
public TheViewHolder(View view) {
super(view);
text1 = (TextView)view.findViewById(R.id.text1);
text2 = (TextView)view.findViewById(R.id.text2);
}
}
#Override
public TheViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.recyclerview_row, parent, false);
return new TheViewHolder(itemView);
}
#Override
public void onBindViewHolder(TheViewHolder holder, int position) {
final ModelClientInformation info = onlineList.get(position);
holder.text1.setText(holder.getText1());
holder.text1.setText(holder.getText2());
}
/**Function to Return the size of List**/
#Override
public int getItemCount() {
return onlineList.size();
}
/**Function to Clear the List**/
public void clear(){
onlineList.clear();
}
/**Function to return the Data List**/
public List<ModelClientInformation> getOnlineList(){
return this.onlineList;
}
/**Function to update the specific Item**/
public void updateTextView(int position, ModelClientInformation newItem){
onlineList.set(position, newItem); //set the item
notifyItemChanged(position, newItem); //notify the adapter for changes
}
/*Get the position of item inside data list base on the given ID*/
public int getPositionBaseOnItemID(String theID) {
int length = onlineList.size();
for (int i = 0; i < length; i ++) {
if(onlineList.get(i).getItemID().equals(theID)) {
return i;
}
}
return -1; //Item not found
}
}
Pojo
public ModelClientInformation class{
private String theID, text1, text2;
public ModelClientInformation(String theID, String text1, String text2){
this.theID = theID;
this.text1 = text1;
this.text2 = text2;
}
public String getItemID(){
return theID;
}
public String getText1(){
return text1;
}
public String getText2(){
return text2;
}
}
I'm not so sure if this is how it should be done but I'm able to update the specific List Item and it's view base on position.
Thanks to everyone who gave me the idea of notifyItemChanged() ! hope this help the other too.
In your adapter,
String textToUpdate;
#Override
public void onBindViewHolder(TheViewHolder holder, int position) {
final ModelClientInformation info = onlineList.get(position);
holder.text1.setText(holder.getText1());// update the textview u want by setting the "textToUpdate" variable
holder.text1.setText(holder.getText2());
}
public void updateTextView(String text){
textToUpdate = text;
notifyDatasetChanged();
}
Hope it helps !
Create a a public static in the RecylerView class, simply assign a text in that method. Next, in the class where your geting a hook to the recycler view instance, make sure to add a onclick and ontouch interface to store the position of the selected text row. Use the recycler view instance to calling notifyItemChanged.
Here is a description from Documentation.
notifyItemChanged added in version 22.1.0
void notifyItemChanged (int position)
Notify any registered observers that the item at position has changed. Equivalent to calling notifyItemChanged(position, null);.
This is an item change event, not a structural change event. It indicates that any reflection of the data at position is out of date and should be updated. The item at position retains the same identity.
I am getting data from server and then parsing it and storing it in a List. I am using this list for the RecyclerView's adapter. I am using Fragments.
I extended
RecyclerView.Adapter<RecyclerView.ViewHolder>
this my complete adapter class
public class GridAdapter extends RecyclerView.Adapter<GridAdapter.ViewHolder> {
private List<ProductModel> list;
ItemClickListener itemClickListener;
public GridAdapter(List<ProductModel> list, ItemClickListener itemClickListener) {
this.list = list;
this.itemClickListener = itemClickListener;
}
public void update(int position,ProductModel pm){
Log.v("update adapter",position+"");
list.set(position,pm);
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.grid_item, parent, false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(final ViewHolder holder, final int position) {
final ProductModel cp = list.get(position);
holder.tvName.setText(cp.name);
holder.tvPrice.setText(cp.price);
holder.ratingBar.setRating(cp.rate);
Log.v("grid",cp.id + "=="+ cp.checked);
if(cp.checked==1){
holder.imgCheck.setVisibility(View.VISIBLE);
}else{
holder.imgCheck.setVisibility(View.GONE);
}
LayerDrawable stars = (LayerDrawable) holder.ratingBar.getProgressDrawable();
stars.getDrawable(2).setColorFilter(Color.YELLOW, PorterDuff.Mode.SRC_ATOP);
holder.lnrItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
itemClickListener.onItemClick(cp,position);
}
});
}
#Override
public int getItemCount() {
return list.size();
}
public static class ViewHolder extends RecyclerView.ViewHolder {
TextView tvName, tvPrice;
ImageView imgMenu, imgCheck;
LinearLayout lnrItem;
RatingBar ratingBar;
public ViewHolder(View itemView) {
super(itemView);
lnrItem = itemView.findViewById(R.id.lnrItem);
imgMenu = itemView.findViewById(R.id.imgMenu);
imgCheck = itemView.findViewById(R.id.imgCheck);
tvName = itemView.findViewById(R.id.tvName);
ratingBar = itemView.findViewById(R.id.ratingBar);
tvPrice = itemView.findViewById(R.id.tvPrice);
}
}
}
and in fragment activity i use this code to load adapter to recyclerview
public void fillListProduct(List<ProductModel> mItems) { //method for load adapter for the first time
mAdapterGrid = new GridAdapter(mItems,this);
recGrid.setAdapter(mAdapterGrid);
}
void update(){ //method to change checked value
mAdapterGrid.update(cp.row,cp);
mAdapterGrid.notifyDataSetChanged();
}
in my custom adapter i have 3 variable lets call id, name, checked, everything work properly, in first load i set 0 as default value for checked, in my scenario user can change checked value by tap the row of recycleview.
my question is, when user tap desire row then checked will change from 0 to 1 and display it to recycelview, i already use notifydatasetchange() but not working.
Please help.
thanks
Try to remove mAdapterGrid.notifyDataSetChanged();
and modify this function in your adpater:
public void update(int position,ProductModel pm){
Log.v("update adapter",position+"");
list.set(position,pm);
notifyItemChanged(position);
}
I have a RecyclerView which shows list of items sorted in descending order that works fine when I initially launch the screen, but when I add some more items to the existing list and setting it to the adapter with notifyDataSetChanged() method the existing list gets sorted in ascending order and the new items get added at the bottom of the view.
below is adapter
public class RecycleAdapter extends RecyclerView.Adapter<RecycleAdapter.ViewHolder> {
private List<Cinema> lists;
private int savedItems;
public RecycleAdapter(List<Cinema> cinemas, int savedItems) {
this.lists = cinemas;
this.savedItems = savedItems;
}
public class ViewHolder extends RecyclerView.ViewHolder {
public TextView cinemaTitle;
public TextView cinemaDescription;
public TextView cinemaDate;
public LinearLayout newLayout;
public ViewHolder(View view) {
super(view);
cinemaTitle = (TextView) view.findViewById(R.id.cinema_title);
cinemaDescription = (TextView) view.findViewById(R.id.description_text);
cinemaDate = (TextView) view.findViewById(R.id.cinema_date);
newLayout = (LinearLayout) view.findViewById(R.id.new_cinema_layout);
}
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View itemView = LayoutInflater.from(parent.getContext())
.inflate(R.layout.item_cinema, parent, false);
return new ViewHolder(itemView);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Cinema cinema = cinemas.get(position);
if (cinema.getId() > savedItems) {
holder.newLayout.setVisibility(View.VISIBLE);
} else {
holder.newLayout.setVisibility(View.INVISIBLE);
}
String title = cinema.getMessage().trim();
String description = cinema.getDescription().trim();
String date = cinema.getPublishedDate().trim();
holder.cinemaTitle.setText(title);
holder.cinemaDescription.setText(description);
holder.cinemaDate.setText(date);
}
public void setCineams(List<Cinema> cinemas) {
this.cinemas = cinemas;
notifyDataSetChanged();
}
#Override
public int getItemCount() {
return cinemas.size();
}
}
and the method which updates the list from Fragment is given below:
#Override
public void onCinemaUpdated() {
cinemas = firebaseHelper.getAllCinemas();
//Method which sorts the lists in descending order after adding new items into it
sortCinemas();
if (recycleAdapterAdapter != null) {
recycleAdapterAdapter.setCineams(cinemas);
}
}
I am not sure why am I getting this behaviour. Can anyone clarify on this?
Change your constructor and setCinemas method code to : Let me know if it helps..
public RecycleAdapter(List<Cinema> cinemas, int savedItems) {
this.lists = cinemas;
this.savedItems = savedItems;
}
public void setCineams(List<Cinema> cinemas) {
this.lists = cinemas;
notifyDataSetChanged();
}