如何根据变量更改TextView的(在列表视图内)文本颜色?

时间:2015-08-01 21:30:56

标签: android listview

我正在尝试在适配器中执行此操作,如下所示

@Override
    public View getView(int position, View convertView, ViewGroup parent) {
        View view;
        ZgrViewHolder holder;
        mInflater = LayoutInflater.from(mContext);
        QuestionObject questionObject = arrayList_.get(position);

        if(convertView == null) {
            convertView = mInflater.inflate(R.layout.zgr_listview_layout, parent, false);
            holder = new ZgrViewHolder();
            holder.questionTitle = (TextView)convertView.findViewById(R.id.questionTitle);
            holder.questionDate = (TextView)convertView.findViewById(R.id.questionDate);
            holder.questionContent = (TextView)convertView.findViewById(R.id.questionContent);
            holder.senderContact = (TextView)convertView.findViewById(R.id.questionSenderContact);
            holder.messageID = (TextView)convertView.findViewById(R.id.messageID);

            holder.questionTitle.setText(questionObject.mailTitle);
            holder.messageID.setText(String.valueOf(questionObject.messageID));
            holder.questionDate.setText(questionObject.Date_);
            holder.questionContent.setText(questionObject.mailContent);
            holder.senderContact.setText(questionObject.senderContact);

            if (questionObject.ReadState == 1){
                holder.questionTitle.setTextColor(Color.argb(100,100,100,100));
            }

            convertView.setTag(holder);
        } else {
            holder = (ZgrViewHolder)convertView.getTag();
        }
        return convertView;
    }

但是当listview完成后,没有一个textview具有我指定的颜色。我调试,ReadState变量有时是1,但仍然没有。

2 个答案:

答案 0 :(得分:1)

试试这个

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view;
    ZgrViewHolder holder;
    mInflater = LayoutInflater.from(mContext);
    QuestionObject questionObject = arrayList_.get(position);

    if(convertView == null) {
        convertView = mInflater.inflate(R.layout.zgr_listview_layout, parent, false);
        holder = new ZgrViewHolder();
        holder.questionTitle = (TextView)convertView.findViewById(R.id.questionTitle);
        holder.questionDate = (TextView)convertView.findViewById(R.id.questionDate);
        holder.questionContent = (TextView)convertView.findViewById(R.id.questionContent);
        holder.senderContact = (TextView)convertView.findViewById(R.id.questionSenderContact);
        holder.messageID = (TextView)convertView.findViewById(R.id.messageID);


        }

        convertView.setTag(holder);
    } else {
        holder = (ZgrViewHolder)convertView.getTag();
    }
        holder.questionTitle.setText(questionObject.mailTitle);
        holder.messageID.setText(String.valueOf(questionObject.messageID));
        holder.questionDate.setText(questionObject.Date_);
        holder.questionContent.setText(questionObject.mailContent);
        holder.senderContact.setText(questionObject.senderContact);

        if (questionObject.ReadState == 1){

        holder.questionTitle.setTextColor(Color.argb(100,100,100,100));
        return convertView;
}

答案 1 :(得分:0)

请将textview颜色分配给这段代码if(convertView == null){} 谢谢并反馈我