Overriding Android ArrayAdapter

后端 未结 1 1880
醉酒成梦
醉酒成梦 2020-12-15 00:28

I want to do a very simple thing. I have a listview in my application which I dynamically add text to. But, after a certain point, I would like to change the color of the te

相关标签:
1条回答
  • 2020-12-15 00:44

    You need to set the text in getView(). Get the value to set with getItem(pos), then set it.

    public View getView(int pos, View convertView, ViewGroup parent){
        this.v = convertView;
        if(v==null) {
            LayoutInflater vi = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            v=vi.inflate(R.layout.gamelistitem, null);
        }
    
        // Moved this outside the if blocks, because we need it regardless
        // of the value of timeLeft.
        TextView tv = (TextView)v.findViewById(R.id.list_content);
        tv.setText(getItem(pos));
    
        if(timeLeft!=0) {
            //TextView tv = (TextView)v.findViewById(R.id.list_content);
            //tv.setText(str[pos]);
            tv.setTextColor(Color.GREEN);
        }
        else {
            //TextView tv = (TextView)v.findViewById(R.id.list_content);
            //tv.setText(str[pos]);
            tv.setTextColor(Color.RED);
        }
    
        return v;
    }
    

    Also, is there a reason you're storing v as a member variable, rather than just inside the function?

    0 讨论(0)
提交回复
热议问题