0

I have the following code inside my adapter:

public View getView(int position, View convertView, ViewGroup parent) 
{
    if (convertView==null)
    {
        LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.box_afisare, null);
    }

    final TextView titlu = (TextView) convertView.findViewById(R.id.titlu);
       titlu.setText(Html.fromHtml(textt.get(position)));
       titlu.setTextSize(TypedValue.COMPLEX_UNIT_SP,font);

        final Integer pos = position;
        titlu.setOnClickListener(new View.OnClickListener()
        {

            public void onClick(View v) 
            {

                if (main_contextt.selectie.contains(id_post.get(pos)))
                {
                    Toast.makeText(mContext," REMOVE ",Toast.LENGTH_LONG).show();
                    titlu.setBackgroundColor(Color.parseColor("#0077CC"));
                }
                else
                {
                main_contextt.selectie.add(id_post.get(pos));

                titlu.setBackgroundColor(Color.parseColor("#404040"));

                }
            }
        });


    return convertView;
}

I manage to colorate the selected line or lines. But when i scroll the listview and those selected lines are no longer in view range of the phone....the background color disapear.

It disapear only if that line/lines is out of view. I think the adapter is redrawing?

What to do to remain the color set on the line/lines even after i scroll the listview?

thanks

1 Answers1

0

ListViews recycle their child views. So if you have 20 items in your ListView but only enough room on the screen to show 4 at a time, the ListView will only have 4 children which it will recycle to show all 20 items. When one view scrolls out of view, it is recycled for the next view coming into view. See this answer for a great explanation.

What you need to do is tie the color to some underlying data. Then you would use code like.

 titlu.setOnClickListener(new View.OnClickListener()
    {
        public void onClick(View v) 
        {
            // set data.get(pos).color
        }
    });

 titlu.titlu.setBackgroundColor(data.get(pos).color);

...something like that.

Community
  • 1
  • 1
NSouth
  • 5,067
  • 7
  • 48
  • 83