在Listview中更改Textview的颜色

时间:2013-09-06 18:24:08

标签: android listview colors textview baseadapter

我对Android非常陌生,我想在Listview中更改TextView的文本颜色。尝试这个时,颜色不会改变。谁可以帮我解决这个(简单?)问题?

我在我的" BaseAdapter"中使用以下代码控制ListView:

    public View getView(int i, View view, ViewGroup viewGroup) {


    if (view == null) {
        view = mInflater.inflate(R.layout.cijfer_listitem, null);

        holder = new ViewHolder();
        holder.mCijferTxt = (TextView) view.findViewById(R.id.cijferTxt);
        holder.mVakTxt = (TextView) view.findViewById(R.id.vakTxt);
        holder.mVakUitgebreidTxt = (TextView) view.findViewById(R.id.vakUitgebreidTxt);
        holder.mIdTxt = (TextView) view.findViewById(R.id.idTxt);

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

    }

    Cijfer cijfer = mCijfers.get(i);

    holder.mCijferTxt.setText(cijfer.getCijfer());
    holder.mVakTxt.setText(cijfer.getCursus());
    holder.mVakUitgebreidTxt.setText(cijfer.getCursusUitgebreid());
    holder.mIdTxt.setText("" + cijfer.getCijferID());

    if (cijfer.isDef() == true ){

        holder.mCijferTxt.setTextColor(R.color.colorTxtDef);
    }
    else{
        holder.mCijferTxt.setTextColor((R.color.colorTxtConcept));
    }

    return view;
}

public class ViewHolder {
    public TextView mCijferTxt;
    public TextView mVakTxt;
    public TextView mVakUitgebreidTxt;
    public TextView mIdTxt;
}

我想改变TextView" cijferTxt"的颜色。

希望这足以让你帮助我:)

2 个答案:

答案 0 :(得分:2)

试试这个

 holder.setTextColor(view.getResources().getColor(R.color.colorTxtDef));

答案 1 :(得分:0)

您正在尝试将文本颜色设置为颜色资源ID(它只是指向颜色的ID)。您需要从此颜色资源ID中提取颜色。 例如:

int color = context.getResources().getColor(R.color.colorTxtDef);
holder.mCijferTxt.setTextColor(color);