1

I am fetching data from server through retrofit. If the data is positive the color should be green else it should be red. Just like the Sensex if it is positive then it shows green color else red. See Image for reference.Onbind viewholder i done below but it is for position in grid view

@Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        if(position % 2 ==0) {
            holder.itemView.setBackgroundColor(
                    ContextCompat.getColor(holder.itemView.getContext(), R.color.red));
        } else {
            holder.itemView.setBackgroundColor(
                    ContextCompat.getColor(holder.itemView.getContext(), R.color.green));
        }

How to achieve that.

gopssays
  • 41
  • 7
  • what is your question again? – Teo Aug 16 '21 at 06:24
  • @Teo-Pls see the image I want to do like it.If it is positive then I want to show green color on card and if it is negative then i want to show red color – gopssays Aug 16 '21 at 06:38
  • just set a param for your item? `isPositive` - Boolean, then set the color like what you had done in `onBindViewHolder` – Teo Aug 16 '21 at 06:43
  • @Teo-can you show some example bcz i am beginner – gopssays Aug 16 '21 at 07:15
  • Use Multiple viewtype funtionality with recyelerview.I think this is best way for your requirement. [https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-types](https://stackoverflow.com/questions/26245139/how-to-create-recyclerview-with-multiple-view-types) [https://blog.mindorks.com/recyclerview-multiple-view-types-in-android](https://blog.mindorks.com/recyclerview-multiple-view-types-in-android) May be this link help you.How to create that. – Umesh Yadav Aug 16 '21 at 06:39

2 Answers2

0
  1. You can prepare a separate list to display the data in adapter.like - `List<ListItem> listDisplay= new ArrayList()`
  2. Create ListItem class which offers getter() and setter() methods. Visit https://www.w3schools.com/java/java_encapsulation.asp .you can create a variable and it 's getter() & setter() methods in this class based on your requirements to display data on the card view
  3. When you will receive the list data from server, create a instance of ListItem class & using loop just set the received values to its variables and add the received data item in the list listDisplay
  4. Pass this listDisplay to adapter 's constructor while initialization.
  5. In adapter 's onBindViewHolder() method, do the following code
     position) {
   //create a ListItem
   ListItem listItem = *listMaintainedInAdapter*.get(position)
           
   /*create a instance of adapter's view holder class who holds the 
    views reference.*/
    
   
   /*Check data is positive or not. here, num is variable declared in 
         ListItem class who holds the value which you want to check*/
   
   if(listItem.num > 0){    
         //set card colour to green
   }else{
           //set card colour to red.
   }
}```
Komal
  • 26
  • 3
0
    @Override
        public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
            val data = list[holder.adapterPosition]
            if (data.changeValue > 0) {
                holder.itemView.setBackgroundColor(
                        ContextCompat.getColor(holder.itemView.getContext(), R.color.red));
            } else {
                holder.itemView.setBackgroundColor(
                        ContextCompat.getColor(holder.itemView.getContext(), R.color.green));
            }
    }

where data.changeValue is the data value coming from the API like 207.57 or -63.85 (I just assumed). I hope this solution will solve your problem.

Mohan Kadu
  • 36
  • 4