公共类SongListAdapter_AddMode扩展了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;
}


}

好吧,这是ArrayAdapter外观的一个小例子。如您在getView()方法中看到的,我从一个实际上很大的列表中获得了currentSong。它包含1600〜歌曲。当我在LogCat上打印位置时,它表示0到7。如何在歌曲列表中找到正确的位置,但是当我打印它时,它却完全不同?

我总是得到正确的歌曲。即使在900〜以上。但是位置(LogCat)始终从0到7 ...

我需要获取当前行。我想以这种方法从当前onClickListener()的按钮中添加一个View,当我单击它时,我想做一些onItemClickListener()上的ListView无法做的事情。

最佳答案

似乎您设置了按钮单击侦听器以打印可回收视图的位置,因此得到0-7。

而是将监听器设置在if (convertview == null)检查之外。

10-07 19:23
查看更多