HiHo!

On Mon, 2009-12-14 at 11:06 -0800, Romain Guy wrote:
> >            v.setBackgroundResource(R.color.todo_item_highlighted);
> 
> Here's your problem. Views are reused by ListView (so if you change
> the background of item #1, it might be reused as item #4 later), 

You mean the reuse of convertView in the following code of
core/java/android/widget/ArrayAdapter.java?

private View createViewFromResource(int position,
                                    View convertView,
                                    ViewGroup parent,
                                    int resource) {
        View view;
        TextView text;

        if (convertView == null) {
            view = mInflater.inflate(resource, parent, false);
        } else {
            view = convertView;
        }

Why is this not documented in the documentation of onListItemClick()
giving a warning that v may not be as what the developer expects?

> this
> type of change must be done from the Adapter's getView() method.

You mean something like the following?

    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        setListAdapter(new ArrayAdapter(this,
                                        R.layout.todo_list_row,
                                        new String[] {
                                                "Phobos",
                                                "Demos",
                                                "Venus",
                                                "Pluto",
                                                "Mercury"
                                        }) {
                @Override
                public View getView(int position,
                                    View convertView,
                                    ViewGroup parent) {

                    TextView t = (TextView) getLayoutInflater().inflate(
                        R.layout.todo_list_row,
                        parent,
                        false
                    );

                    t.setText(getItem(position).toString());

                    if (position == selectedPos) {

                        t.setBackgroundResource(
                            R.color.todo_item_highlighted);
                    }

                    return t;
                }
        });
    }

    @Override
    protected void onListItemClick(ListView l,
                                   View v, int position, long id) {

            super.onListItemClick(l, v, position, id);

            selectedPos = position;
    }

Thank you.

> -- 
> Romain Guy
> Android framework engineer
> romain...@android.com
> 
> Note: please don't send private questions to me, as I don't have time
> to provide private support.  All such questions should be posted on
> public forums, where I and others can see and answer them

-- 
Best regards,
Eus (FSF member #4445)

In this digital era, where computing technology is pervasive, your
freedom depends on the software controlling those computing devices.

Join free software movement today! It is free as in freedom, not as in
free beer!

Join: http://www.fsf.org/jf?referrer=4445

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscr...@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to