Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
475 views
in Technique[技术] by (71.8m points)

listview - Overriding Android ArrayAdapter

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 text inside the listview. So, I made a XML defining my custom list item, and subclassed the ArrayAdapter. But, whenever I call the add() method on my custom ArrayAdapter, an item does get added to the listview, but the text is not placed into it.

Here's my XML: `

<TextView xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/list_content" android:textSize="8pt"
    android:gravity="center" android:layout_margin="4dip"
    android:layout_width="fill_parent" android:layout_height="wrap_content" android:textColor="#FF00FF00"/>

And my ArrayAdapter subclass:

private class customAdapter extends ArrayAdapter<String> {
    public View v;
    public customAdapter(Context context){  
        super(context, R.layout.gamelistitem);
    }

    @Override
    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);
        }

        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;
    }
}

I'm sure I'm doing something horribly wrong, but I'm still a little new to Android.

Thank you! `

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...