如何设置字符串图像而不是Int颜色

时间:2017-06-05 16:19:35

标签: java android

我有一个自定义网格视图,其中包含一个图像我有一个方法,改变相对布局的颜色,包含我想要的图像视图,而不是改变相对布局的颜色我想将其更改为图像视图,它&# 39; s相同的图像,但我试图做不同的颜色,但我失败了,所以这里是我的代码 这是项目的模块,其中包含一些文本和一个有一个名为Table_Color的int的图像,这是我想要从int更改为imageView

package abtech.waiteriano.com.waitrer.getters_and_setters;

import android.graphics.Bitmap;

/**
 * @author manish.s
 */

public class Item {
    Bitmap image;
    String Table_ID;
    String Table_Name;
    String OpenIn;
    String Covers;
    String Count_Checks;
    String CoversCapacity;
    String Check_ID;
    int Table_Color;

    public Item(Bitmap image, String Table_ID, String Table_Name, String OpenIn, String Covers, String Count_Checks, String CoversCapacity, String Check_ID, int Table_Color) {
        super();
        this.image = image;
        this.Table_ID = Table_ID;
        this.Table_Name = Table_Name;
        this.OpenIn = OpenIn;
        this.Covers = Covers;
        this.Count_Checks = Count_Checks;
        this.CoversCapacity = CoversCapacity;
        this.Table_Color = Table_Color;
        this.Check_ID = Check_ID;
    }

    public Bitmap getImage() {
        return image;
    }

    public void setImage(Bitmap image) {
        this.image = image;
    }

    public void setTable_ID(String Table_ID) {
        this.Table_ID = Table_ID;
    }

    public String getTable_ID() {
        return Table_ID;
    }

    public void setTable_Name(String Table_Name) {
        this.Table_Name = Table_Name;
    }

    public String getTable_Name() {
        return Table_Name;
    }

    public String getOpenIn() {
        return OpenIn;
    }

    public void setOpenIn(String OpenIn) {
        this.OpenIn = OpenIn;
    }

    public String getCovers() {
        return Covers;
    }

    public void setCovers(String Covers) {
        this.Covers = Covers;
    }

    public String getCount_Checks() {
        return Count_Checks;
    }

    public void setCount_Checks(String Count_Checks) {
        this.Count_Checks = Count_Checks;
    }

    public String getCoversCapacity() {
        return CoversCapacity;
    }

    public void setCoversCapacity(String CoversCapacity) {
        this.CoversCapacity = CoversCapacity;
    }

    public int getTable_Color() {
        return Table_Color;
    }

    public void setTable_Color(int Table_Color) {
        this.Table_Color = Table_Color;
    }
    public String getCheck_ID() {
        return Check_ID;
    }

    public void setCheck_ID(String Check_ID) {
        this.Check_ID = Check_ID;
    }
}

我应该从int更改为BitMap还是字符串? 这是我想要在我的适配器类中更改的部分 我有一个HolderClass像这样包含一些文本和ImageView和相对布局这个相对布局是我的情况,我将改为图像视图,这里是类

static class RecordHolder {
    TextView txtTime;
    RelativeLayout tableRL;
    TextView txtCovers;
    TextView txtChecks;
    TextView txtTitle;
    ImageView imageItem;

}

这是我获取所有持有人物品的视图

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

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

        holder = new RecordHolder();
        holder.txtTitle = (TextView) row.findViewById(R.id.tablesnumberstv);
        holder.txtTime = (TextView) row.findViewById(R.id.datetv);
        holder.tableRL = (RelativeLayout) row.findViewById(R.id.rlID);
        holder.txtChecks = (TextView) row.findViewById(R.id.checknumbertv);
        holder.txtCovers = (TextView) row.findViewById(R.id.conversnumbertv);
        holder.imageItem = (ImageView) row.findViewById(R.id.item_image);
        row.setTag(holder);
    } else {
        holder = (RecordHolder) row.getTag();
    }

    Item item = data.get(position);
    holder.txtTitle.setText(item.getTable_Name());
    holder.txtTime.setText(item.getOpenIn());
    holder.txtCovers.setText(item.getCovers());
    holder.tableRL.layout(0, 0, 0, 0);
    holder.txtChecks.setText(item.getCount_Checks());
    holder.imageItem.setImageBitmap(item.getImage());
    if (item.getTable_Color() != Color.GREEN)
        holder.tableRL.setBackgroundColor(item.getTable_Color());
    return row;

}

这是我的方法,它改变了相对布局颜色

        ResultSet rs = ConnectionClass.Ret_RS(str);
        while (rs.next()) {
            String OpenIn = "";
            if (null != rs.getTime("OpenIn"))
                OpenIn = rs.getTime("OpenIn").toString();
            int Table_Color = Color.GREEN;
            if (rs.getInt("Count_Checks") > 0) {
                if (rs.getInt("Num_Print") > 0)
                    Table_Color = Color.GRAY;
                else
                    Table_Color = Color.RED;
            } else {
                if (null != rs.getTime("ClosedIn")) {
                    Date time_close = rs.getTime("ClosedIn");
                    Date time_now = Calendar.getInstance().getTime();
                    if (time_now.getSeconds() - time_close.getSeconds() <= 30)
                        Table_Color = Color.YELLOW;
                    else
                        Table_Color = Color.GREEN;
                } else
                    Table_Color = Color.GREEN;
            }

            gridArray.add(new Item(homeIcon, rs.getString("ID"), rs.getString("Name"), OpenIn, rs.getString("Covers"), rs.getString("Count_Checks"), rs.getString("CoversCapacity"), rs.getString("Check_ID"), Table_Color));

        }

作为我想为示例设置的图像的名称

bluetable.png
redtable.png
yellowtable.png
greytable.png
greentable.png

抱歉,如果有任何不清楚的地方,我希望有人可以帮助我

1 个答案:

答案 0 :(得分:1)

替换此行

holder.tableRL.setBackgroundColor(item.getTable_Color());

holder.imageItem.setImageBitmap(((BitmapDrawable)drawable).getBitmap());

并在TableFragmentClass中 取代

int Table_Color = Color.GREEN;

int Table_Color = R.drawable.darkgreentable;