2

I'm facing strange selection of list-view item while scrolling.

Initial selection screenshot(selected 1st entry) enter image description here

after scrolling listview item is auto selected why? (see below screenshot)

enter image description here

adapter source code is here

public View getView(int position, View convertView, ViewGroup parent) {
    View vi=convertView;
    if(convertView==null)
        vi = inflater.inflate(R.layout.list_row, null);

    TextView title = (TextView)vi.findViewById(R.id.title); // title
    TextView artist = (TextView)vi.findViewById(R.id.artist); // artist name
    TextView duration = (TextView)vi.findViewById(R.id.duration); // duration
    ImageView thumb_image=(ImageView)vi.findViewById(R.id.list_image); // thumb image

    HashMap<String, String> song = new HashMap<String, String>();
    song = data.get(position);

    // Setting all values in listview
    title.setText(song.get(CustomizedListView.KEY_TITLE));
    artist.setText(song.get(CustomizedListView.KEY_ARTIST));
    duration.setText(song.get(CustomizedListView.KEY_DURATION));
    imageLoader.DisplayImage(song.get(CustomizedListView.KEY_THUMB_URL), thumb_image);
    return vi;
}

make button visible in setonitemclicklistner() is giving the problem

list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
        int position, long id) {
view.setSelected(true);
view.setBackgroundResource(R.drawable.gradient_bg_hover);   
TextView title;
TextView artist;
title = (TextView)view.findViewById(R.id.title); // title
artist = (TextView)view.findViewById(R.id.artist); // artist
title.setTextColor(getResources().getColor(android.R.color.white));
artist.setTextColor(getResources().getColor(android.R.color.white));

ImageButton btnChild = (ImageButton)view.findViewById(R.id.arrow);
btnChild.setVisibility(View.VISIBLE);

if(lastselected!= null)
{

    title = (TextView)lastselected.findViewById(R.id.title); // title
    artist = (TextView)lastselected.findViewById(R.id.artist); // artist
    title.setTextColor(getResources().getColor(android.R.color.black));
    artist.setTextColor(getResources().getColor(android.R.color.black));

     btnChild = (ImageButton)lastselected.findViewById(R.id.arrow);
    btnChild.setVisibility(View.INVISIBLE);
    lastselected.setBackgroundResource(R.drawable.gradient_bg);
}

lastselected= view;

after image button is visible the getview recycle the same view for next displaying item. I don’t know how to fix this.

user755
  • 2,521
  • 3
  • 18
  • 29

4 Answers4

1

use

listview.setSelector(drawable)

if you want to change selector or disable it.

ali shekari
  • 740
  • 1
  • 11
  • 26
0

You can use an onScrollListener to detect when the list is scrolling and prevent an item being selected.

Have a look here (click).

onScroll() 

Callback method to be invoked when the list or grid has been scrolled. This will be called after the scroll has completed

onScrollStateChanged()

Callback method to be invoked while the list view or grid view is being scrolled. If the view is being scrolled, this method will be called before the next frame of the scroll is rendered.

Scott Helme
  • 4,786
  • 2
  • 23
  • 35
0

This is a common pitfall with adapter getView(). When a view is recycled (convertView is not null), you will need to reset the recycled view to its initial state. In this case, reset the checked/selected state of the recycled list row.

laalto
  • 150,114
  • 66
  • 286
  • 303
  • yes.I implemented like 'if(isSelected[position]== true) { vi.setSelected(true); } else { vi.setSelected(false); }' But still same issue – user755 Aug 12 '13 at 09:07
  • you are right! list has a invisible button when user selects the listitem I make the button visible inside setOnItemClickListener() and this change is being recycled inside getview. any clues how to proceed? – user755 Aug 12 '13 at 13:44
0

Try this:

Selected Item issue while scrolling listview

Because of lazy loading, the states of the views are not guaranteed. It would be much more precise if you store an extra boolean, or in your case another another pair of strings to your HashMap if it's easier, to flag whether the song was selected. Implementing ViewHolders won't hurt as well. Hope this helps.

Community
  • 1
  • 1
donkey
  • 204
  • 1
  • 7