2

public class SongListAdapter_AddMode extends ArrayAdapter{

Context context;
ArrayList<Song> songs;

public SongListAdapter_AddMode(Context context, ArrayList<Song> songs) {
    super(context, R.layout.list_item1_addmode, songs );
    this.songs = songs;
    this.context = context;
}

@Override
public View getView(int position, View convertView, ViewGroup parent){

    ViewHolder holder;
    final int pos = position;
    Log.d("TAG", "position :" + position);
    Song currentSong = songs.get(position);
    Log.d("TAG", "position : " + position );

    if( convertView == null ){
        holder = new ViewHolder();

        convertView = LayoutInflater.from(context).inflate( R.layout.list_item1_addmode, parent, false );

        holder.titleLabel = (TextView) convertView.findViewById( R.id.topLabel );
        holder.artistLabel = (TextView) convertView.findViewById( R.id.bottomLabel );
        holder.cover = (ImageView) convertView.findViewById( R.id.list_image );
        holder.button = (ImageView) convertView.findViewById(R.id.addButton);
        holder.button.setOnClickListener( new OnClickListener() {

            @Override
            public void onClick(View v) {
                Log.d("TAG", "pos" + pos );
            }
        });
        convertView.setTag(holder);

    }else{
        holder = (ViewHolder) convertView.getTag();
    }

    Uri coverPath = ContentUris.withAppendedId(Uri.parse("content://media/external/audio/albumart"), currentSong.getID() );

    Picasso.with(context).load( coverPath ).error( R.drawable.untitled ).into( holder.cover );
    holder.titleLabel.setText( currentSong.getName() );
    holder.artistLabel.setText( currentSong.getArtistName() );

    return convertView;
}

private class ViewHolder {
    TextView titleLabel;
    TextView artistLabel;
    ImageView cover;
    ImageView button;
}

}

Well this is a small example of what an ArrayAdapter might look like. As you can see in the getView() method, I get the currentSong from a list which is actually very huge. It contains 1600~ Songs. When I print the position to the LogCat, it says 0 up to 7. How am I getting the correct position in the song list but when I print it, it is totally different?

I ALWAYS get the correct song. Even in the 900~ and above. But the position(LogCat) is always from 0 up to 7...

I need to get the current row. I want to add an onClickListener() to a button from the current View in this method and when I click it, I want to do something which I cannot do with an onItemClickListener() on the ListView.

Davlog
  • 2,162
  • 8
  • 36
  • 60
  • It cannot be that the integer just changes the value from one line to another. Are you really sure the songs are displayed in the right order? – Gumbo Aug 02 '14 at 20:44
  • @Gumbo Yeah. I just scroll down and see what positions are shown in the LogCat. The listview shows me the correct songs but I only see 0-7 and again 0-7 in the LogCat output. That's why I am so confused! – Davlog Aug 02 '14 at 20:46
  • @Gumbo I just restarted my application and now WHILE I am scrolling it shows me the correct position but when I stop it shows me 0-7. Maybe I scrolled too slow before. But still, it is very strange and won't let me do what I need to do. – Davlog Aug 02 '14 at 20:48
  • Maybe ther's something wrong with the other methods of the Adapter. For example if `getCount()` returns 7, the list will only call position 0-7. Can you maybe post the rest of your adapter class? – Gumbo Aug 02 '14 at 20:57
  • @Gumbo well I posted the whole code now. – Davlog Aug 02 '14 at 20:58

4 Answers4

3

It seems that you set the button click listener to print the position of the recyclable views thus you get 0-7.

Instead set the listener outside the if (convertview == null) check.

Simas
  • 43,548
  • 10
  • 88
  • 116
  • Whoa! Thank you! It worked! I just made a new variable `final pos = position` so I can use it in the `onClickListener` and print it. I get the correct position now. Thanks! – Davlog Aug 02 '14 at 21:26
0

I ALWAYS get the correct song

of course since the code is correct

But the position(LogCat) is always from 0 up to 7...

This is due to how the ListView recycles its rows. 7 items are visible on the screen of your test device. See this post

Community
  • 1
  • 1
Droidman
  • 11,485
  • 17
  • 93
  • 141
  • That sure is true, but `position` should still be the index of the row in the listview, how else should you be able to fill in the correct data for each row? – Gumbo Aug 02 '14 at 20:54
  • yeah, I wondered too. If I get the song at index 1000 why is it in the next line suddenly 7 or lower? – Davlog Aug 02 '14 at 20:55
0

You should override getCount:

@Override
public int getCount(){
    return songs.size();
}

Because it is essential for correct displaying the list, but returns 0 b default.

Gumbo
  • 1,716
  • 1
  • 15
  • 22
0

When your view has wrong position ids or displays wrong items in your autocomplete dropdown view, then instantiate your view like this (Kotlin):

val view: View = convertView ?: LayoutInflater.from(context).inflate(resourceId, parent, false)

Erkan
  • 140
  • 2
  • 11