Android Studio - 列表视图图像按钮返回错误的位置

时间:2015-11-16 22:41:51

标签: android listview android-adapter simplecursoradapter cursor-position

  • 在我的Android Studio应用程序中,我有一张SD卡中的歌曲列表,我使用自定义适配器和光标调整到列表中

  • 在列表视图中,每个列表项旁边都有一个“播放”按钮

  • 单击该按钮时,它将启动包含相应歌曲ID的意图

  • 不幸的是它不断传递错误的ID,当我检查位置时,即使我点击不在第6位的不同列表项,它也几乎总是“6”

这是点击iamge按钮的代码

public class Adapter extends SimpleCursorAdapter {

public Context context;
public Cursor cursor;
public int layout;
public String[] selection;
public int[] resources;

public View convertView;

ImageButton playButton;
TextView songName;
TextView artist;
ImageView albumArt;



public Adapter(Context context, int layout, Cursor cursor, String[] selection, int[] resources, int flags) {
    super(context, layout, cursor, selection, resources, flags);
    this.context = context;
    this.layout = layout;
    this.cursor = cursor;
    this.resources = resources;
    this.selection = selection;

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
        //inflates list
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(layout,parent,false);

        songName = (TextView) convertView.findViewById(resources[0]);
        artist = (TextView) convertView.findViewById(resources[1]);
        albumArt = (ImageView) convertView.findViewById(resources[2]);

    return convertView;
}

@Override
public void bindView(View view, Context context, final Cursor cursor) {
    super.bindView(view, context, cursor);

        final Cursor thisCursor = cursor;
        final Context thisContext = context;

        playButton = (ImageButton)  convertView.findViewById(R.id.listPlay);

        //set artist and text
        songName.setText(cursor.getString(cursor.getColumnIndex(selection[0])));
        artist.setText(cursor.getString(cursor.getColumnIndex(selection[1])));


        //artwork
        final Uri ART_CONTENT_URI = Uri.parse("content://media/external/audio/albumart");

        try {
            Uri albumArtUri = ContentUris.withAppendedId(ART_CONTENT_URI, cursor.getLong(cursor.getColumnIndex(selection[2])));
            Bitmap songCoverArt = MediaStore.Images.Media.getBitmap(context.getContentResolver(), albumArtUri);
            albumArt.setImageBitmap(songCoverArt);
        }catch (Exception e){

        }

        //if play button is selected
        playButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                int position = thisCursor.getPosition();
                //returns wrong position and id's?


                //get id of song based on that position
                String stringID = cursor.getString(cursor.getColumnIndex(selection[3]));
                //create bundle to send
                Bundle bundle = new Bundle();
                //add position to bundle
                bundle.putString("ID", stringID);
                Intent intent = new Intent(thisContext, Home.class);
                intent.putExtras(bundle);
                //flag to allow this out of activty context
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                thisContext.startActivity(intent);
            }
    });
}

}

  • 请注意cursor.getColumnIndex(selection [3])))== MediaStore.Audio.Media._ID

  • 如果有人能帮助我,我会非常感激!

2 个答案:

答案 0 :(得分:1)

首先避免始终根据position进行数据操作(有时可行,请看一下此视频:ListView https://www.youtube.com/watch?v=wDBM6wVEO70的世界。)

试试这个:

            final String stringID = cursor.getString(cursor.getColumnIndex(selection[3])); //make it final before setting the click listener

            playButton.setOnClickListener(new View.OnClickListener() {
               public void onClick(View v) {

            //create bundle to send
            Bundle bundle = new Bundle();
            //add position to bundle
            bundle.putString("ID", stringID);
            Intent intent = new Intent(thisContext, Home.class);
            intent.putExtras(bundle);
            //flag to allow this out of activty context
            intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            thisContext.startActivity(intent);
        }
});

我认为你还需要让Context最终完成。

答案 1 :(得分:0)

我认为,你应该使用按钮的标签而不是final变量(这会导致内存泄漏)。我以前遇到过同样的问题,你应该使用下面的适配器解决问题,

 public class Adapter extends SimpleCursorAdapter {

public Context context;
public Cursor cursor;
public int layout;
public String[] selection;
public int[] resources;

public View convertView;

ImageButton playButton;
TextView songName;
TextView artist;
ImageView albumArt;



public Adapter(Context context, int layout, Cursor cursor, String[] selection, int[] resources, int flags) {
    super(context, layout, cursor, selection, resources, flags);
    this.context = context;
    this.layout = layout;
    this.cursor = cursor;
    this.resources = resources;
    this.selection = selection;

}

@Override
public View newView(Context context, Cursor cursor, ViewGroup parent) {
        //inflates list
        LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(layout,parent,false);

        songName = (TextView) convertView.findViewById(resources[0]);
        artist = (TextView) convertView.findViewById(resources[1]);
        albumArt = (ImageView) convertView.findViewById(resources[2]);

    return convertView;
}

@Override
public void bindView(View view, Context context, final Cursor cursor) {
    super.bindView(view, context, cursor);

        //final Cursor thisCursor = cursor;
        final Context thisContext = context;

        playButton = (ImageButton)  convertView.findViewById(R.id.listPlay);

        //set artist and text
        songName.setText(cursor.getString(cursor.getColumnIndex(selection[0])));
        artist.setText(cursor.getString(cursor.getColumnIndex(selection[1])));


        //artwork
        final Uri ART_CONTENT_URI = Uri.parse("content://media/external/audio/albumart");

        try {
            Uri albumArtUri = ContentUris.withAppendedId(ART_CONTENT_URI, cursor.getLong(cursor.getColumnIndex(selection[2])));
            Bitmap songCoverArt = MediaStore.Images.Media.getBitmap(context.getContentResolver(), albumArtUri);
            albumArt.setImageBitmap(songCoverArt);
        }catch (Exception e){

        }
        playButton.setTag(cursor.getString(cursor.getColumnIndex(selection[3])));
        //if play button is selected
        playButton.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                //int position = thisCursor.getPosition();
                //returns wrong position and id's?


                //get id of song based on that position
                String stringID = (String) v.getTag();
                //create bundle to send
                Bundle bundle = new Bundle();
                //add position to bundle
                bundle.putString("ID", stringID);
                Intent intent = new Intent(thisContext, Home.class);
                intent.putExtras(bundle);
                //flag to allow this out of activty context
                intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                thisContext.startActivity(intent);
            }
    });
}
}