Android Studio - 返回错误位置的列表视图图像按钮
Android Studio - List View Image Button returning wrong positions
在我的 Android Studio 应用程序中,我有一个来自 SD 卡的歌曲列表,我使用自定义适配器和光标将其改编成列表
在列表视图中,每个列表项旁边都有一个"play"按钮
当按钮被点击时,会启动一个包含对应歌曲ID的intent
不幸的是,它一直传递错误的 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
如果有人能帮助我,我将不胜感激!
首先 避免总是基于 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 设置为最终版本。
我认为,您应该使用按钮的标签而不是 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);
}
});
}
}
在我的 Android Studio 应用程序中,我有一个来自 SD 卡的歌曲列表,我使用自定义适配器和光标将其改编成列表
在列表视图中,每个列表项旁边都有一个"play"按钮
当按钮被点击时,会启动一个包含对应歌曲ID的intent
不幸的是,它一直传递错误的 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
如果有人能帮助我,我将不胜感激!
首先 避免总是基于 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 设置为最终版本。
我认为,您应该使用按钮的标签而不是 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);
}
});
}
}