更改自定义gridview bg颜色

时间:2014-04-05 13:13:18

标签: android gridview android-custom-view

如何动态更改自定义网格视图的背景颜色? 我有一个gridview和一个包含设备状态的类。因此,如果设备处于活动状态,其网格颜色应为绿色。

我可以获取每个设备的状态但不确定如何或在何处实现此背景更改..我尝试更改我的CustomGridView类但它无法正常工作。如果你们给我看或告诉我一个正确的方法,我将不胜感激

ArrayList<IpPhone> data = new ArrayList<IpPhone>();

public CustomGridViewAdapter(Context context, int layoutResourceId, ArrayList<IpPhone> data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    RecordHolder holder = null; 
    ViewGroup vg = parent;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new RecordHolder();
        holder.txtTitle1 = (TextView) row.findViewById(R.id.txt_phoneNumber);
        holder.txtTitle2 = (TextView) row.findViewById(R.id.txt_ipAddress);
        holder.txtTitle3 = (TextView) row.findViewById(R.id.txt_phoneState);

        row.setTag(holder);
    } 
    else {

        holder = (RecordHolder) row.getTag();
    }

    //I tried this if else to change bg colors but it didnt work

    if(data.get(position).state == "0")
    {
        row.setBackgroundColor(Color.BLUE);
    }
    else if (data.get(position).state == "1"){
        row.setBackgroundColor(Color.RED);
    }

    IpPhone item = data.get(position);
    holder.txtTitle1.setText(item.getphoneNumber());
    holder.txtTitle2.setText(item.getipAddress());
    holder.txtTitle3.setText(item.getphoneLocation());
    return row;

}

The Output

0 个答案:

没有答案