开发者

android listview getView() method does not make the same updates as onListItemClick() method for background color

开发者 https://www.devze.com 2023-03-03 10:25 出处:网络
I am attempting to update the background color of a list item when it is selected and keep the changes even if the view changes.

I am attempting to update the background color of a list item when it is selected and keep the changes even if the view changes. My update works as expected when I click on the item but can not get it to maintain the changes if I leave the view. I thought that making the same update in the getView () method would give me what I needed but it doesn't seem to work. Any suggestions?

       public class MyScaAdapter extends SimpleCursorAdapter {

    public MyScaAdapter(Context context, int layout, Cursor c,
            String[] from, int[] to) {
        super(context, layout, c, from, to);
    }
            public View getView(int position, View convertView, ViewGroup parent) {
                View v = super.getView(position, con开发者_如何学GovertView, parent);

                if (convertView == null & position == selectedPos) {
                    v.setSelected(true);
                    v.setBackgroundColor(android.R.color.background_light);
                }        
                return v;
             }  
         }

    protected void onListItemClick(ListView l, View v, int position, long id) {
        super.onListItemClick(l, v, position, id);

       v.setSelected(true);
       v.setBackgroundColor(android.R.color.background_light);  


        selectedPos = position;

    }


Maintain a global reference to the view so that you can set your background color in the onResume() method.

0

精彩评论

暂无评论...
验证码 换一张
取 消