0

I'm a newbie to programming. I have gone through all the related links regarding this question. So it is a request to not mark it as duplicate question. It is a different question and might be possible can help others too.

There are some house holds Refer Image 1. If a family of one house hold is migrated, it will have a value in a "Family_Migration" column in the Database otherwise "NULL" Refer Image 2. So the scenario is there are families living in multiple house holds as discussed above (some are migrated and some are still resident). Those House Holds who are migrated I want their Background color programmatically to be changed like this Refer Image 3

I'm sharing my adapter code with you. I have already set the getter/setter and I also debugged it in depth and I'm receiving value in getter, but here I'm stuck now, all I want to know how to accomplish the above scenario? As far as I know there must be a usage of IF condition and inside IF condition we put the data that we are receiving from the database. But how?

It is preferable and I will be very thankful to you If you can make some changes in the code and share it with me.

package pk.softech.sukh.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

import java.util.List;

import pk.softech.sukh.R;
import pk.softech.sukh.modal.HouseModal;

public class HouseAdapter extends ArrayAdapter<HouseModal> {

    public HouseAdapter(Context context,List<HouseModal> items) {
        super(context,R.layout.view_house_item,items);
    }

    class ViewHolder{
        TextView totalAfrad,totalKhandan,structureID;
    }

    @Override
    public View getView(int position, View view, ViewGroup parent) {
        ViewHolder holder;

        HouseModal item = getItem(position);
        if(view == null){
            view = LayoutInflater.from(getContext()).inflate(R.layout.view_house_item,null);
            holder = new ViewHolder();
            holder.structureID = (TextView) view.findViewById(R.id.structureID);
            holder.totalAfrad = (TextView) view.findViewById(R.id.totalAfrad);
            holder.totalKhandan = (TextView) view.findViewById(R.id.totalKhandan);                

            view.setTag(holder);
        }
        else{
            holder = (ViewHolder) view.getTag();   
        }

        holder.structureID.setText(String.valueOf(item.getStructureNo()));
        holder.totalKhandan.setText(String.valueOf(item.getFamilies()));
        holder.totalAfrad.setText(String.valueOf(item.getMembers()));

        return view;
    }

}
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
  • to achieve your requirement you have to add a boolean to your HouseModal, isFamilyMigrated after that on the TextView your will do, if( item.isisFamilyMigrated){ holder.structureID.setBackground(context.getDrawable(R.drawable.backgroundDrawable) else holder.structureID.setBackground(null).... – Anis BEN NSIR Oct 05 '18 at 15:43
  • Possible duplicate of [How do you change the background color of a TextView that is inside a ListView Programmatically?](https://stackoverflow.com/questions/11771280/how-do-you-change-the-background-color-of-a-textview-that-is-inside-a-listview-p) – Anis BEN NSIR Oct 05 '18 at 15:58

1 Answers1

0

Assuming HouseModel has a property, familyMigration, which represents the Family_Migration column in your database.

It is good practice in Android to extract resources such as background colors to resource files. I'm going to assume there are some colors defined in a color.xml file like below. You can fill in the color values between the tags as color hex codes.

<color name="not_migrated_background"> ... </color>
<color name="migrated_background"> ... </color>

You can use the existence of familyMigration property to decide which background color to use.

HouseModel item = getItem(position);
// ...
int backgroundColorResId = (item.familyMigration != null) ? R.color.migrated_background : R.color.not_migrated_background;
int backgroundColor = context.getColor(backgroundColorResId);
holder.structureID.setBackgroundColor(backgroundColor)
M. Palsich
  • 1,748
  • 13
  • 18