Search code examples
javaandroidandroid-studioarraylistandroid-listview

how to change the background color of my listview based on a given data as a parameter in android


I have an android application that basically scan a barcode. I have a lists of items and I would like to change a color of my list once the item has been scanned.

Please find the image below for reference.

enter image description here

I would like to change a background color of item to red only if the item remaining is zero. There is a item remaining filed on the right side.

I am not sure how to achieve this.


Solution

  • Please provide code of adapter for batter understanding. you can do it by conditioning in your recyclerView

    for example:

    In onBindViewHolder function:

          @Override
          public void onBindViewHolder(@NonNull ViewHolder holder, int 
          position) {
    
          //Get current  item's model  from Arraylist
          ScanModel scanModel = list.get(position);
    
    
          int remainingNumber = scanModel.getRemainigNumber();
          if(remaingNumber>1)
          {
          //change background color to white or whatever you have.
               holder.rvBackground.
               setBackgroundColor(Color.parseColor("#FFFFFF"));
          }
          else
          {
    
             //Change color to red 
             holder.rvBackground.
             setBackgroundColor(Color.parseColor("#ffffff"));
       
          }
         //Rest of your code.....
    
       
          }