我正在为我们的音乐学校开发一个Android应用程序。我设计了一个包含6个项目的列表视图的布局,每个项目中的控件都来自不同的布局文件。
包含列表视图的布局文件
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#F8AE9F"
android:orientation="vertical" >
<TextView
android:id="@+id/textView1"
style="@style/aboutComposer"
android:layout_width="wrap_content"
android:layout_height="45sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center_vertical|center_horizontal"
android:text="@string/msv_songs"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvCartTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_centerHorizontal="true"
android:text="@string/cart_total"
android:textAppearance="?android:attr/textAppearanceMedium" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/tvCartTotal"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:src="@drawable/icon_pay_now" />
<ListView
android:id="@+id/lstMSVSongs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:focusable="false" >
</ListView>
</RelativeLayout>
布局文件,它是每个列表视图项的控件来源
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/tvViewSample"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="@+id/imgFilmPoster"
android:layout_width="75sp"
android:layout_height="75sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="@drawable/abc_ab_bottom_solid_dark_holo" />
<TextView
android:id="@+id/tvSongName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvFilmName"
android:layout_below="@+id/tvFilmName"
android:text="@string/dummy"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvYearReleased"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvSongName"
android:layout_below="@+id/tvSongName"
android:text="@string/dummy"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/tvYearReleased"
android:layout_below="@+id/tvYearReleased"
android:text="@string/dummy"
android:textAppearance="?android:attr/textAppearanceSmall" />
<TextView
android:id="@+id/tvFilmName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="14dp"
android:layout_toRightOf="@+id/imgFilmPoster"
android:focusable="true"
android:text="@string/dummy"
android:textAppearance="?android:attr/textAppearanceSmall" />
<ImageView
android:id="@+id/imgAddCart"
android:layout_width="35sp"
android:layout_height="35sp"
android:contentDescription="@string/add_to_cart"
android:layout_alignTop="@+id/imgView"
android:layout_toRightOf="@+id/imgView"
android:src="@drawable/add_to_cart"
android:tag = "@string/add_to_cart" />
<ImageView
android:id="@+id/imgView"
android:layout_width="35sp"
android:layout_height="35sp"
android:layout_below="@+id/imgFilmPoster"
android:layout_marginTop="14dp"
android:layout_toRightOf="@+id/imgPlay"
android:src="@drawable/view_icon" />
<ImageView
android:id="@+id/imgPlay"
android:layout_width="35sp"
android:layout_height="35sp"
android:layout_alignLeft="@+id/tvPrice"
android:layout_alignTop="@+id/imgView"
android:contentDescription="@string/play_mp3"
android:src="@drawable/play" />
</RelativeLayout>
用于加载单个列表项的数据和处理图像视图点击事件的Java代码
private List<Song> msvSongs = new ArrayList<Song>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.msv_music_sheets);
populateSongsList();
populateSongsListView();
}
private void populateSongsList() {
msvSongs.add(new Song(R.drawable.msv_ayirathil_oruvan,"Ayirathil Oruvan","Adho Andha Paravai Pola","1965",150,R.raw.msv_adho_andha_paravai_pola_ao));
msvSongs.add(new Song(R.drawable.msv_karuppu_panam,"Karuppu Panam","Aadavaralaam","1964",150,R.raw.msv_aadavaralaam_kp));
msvSongs.add(new Song(R.drawable.msv_paalum_pazhamum,"Paalum Paazhamum","Aalayamaniyin Oosai","1961",150,R.raw.msv_aalayamaniyin_oosai_pp));
msvSongs.add(new Song(R.drawable.msv_paava_mannippu,"Paava Mannipu","Athaan Ennathan","1961",150,R.raw.msv_athaan_ennathaan_pm));
msvSongs.add(new Song(R.drawable.msv_periya_idhuthu_pen,"Periya Idhuthu Pen","Andru Vandhadhadhum Adhey Nila","1963",175,R.raw.msv_andru_vandhadhum_adhey_nila_pip));
msvSongs.add(new Song(R.drawable.msv_puthiya_paravai,"Pudhiya Paravai","Enge Nimmadhi","1964",250,R.raw.msv_enge_nimmadhi_pparavai));
//msvSongs.add(new Song(R.drawable.msv_server_sundaram,"Server Sundaram","Avalukku Enna Azhagiya Mugam","1964",150,R.raw.msv_avalukkenna_azhagiamugham_ss));
}
private void populateSongsListView() {
ArrayAdapter<Song> msvSongs = new MSVSongsAdapter();
ListView songsList = (ListView) findViewById(R.id.lstMSVSongs);
songsList.setAdapter(msvSongs);
}
private class MSVSongsAdapter extends ArrayAdapter<Song>{
public MSVSongsAdapter(){
super(MsvSongs.this, R.layout.songs_view, msvSongs);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = convertView;
if(itemView == null){
itemView = getLayoutInflater().inflate(R.layout.songs_view, parent, false);
}
for(int i=0;i<msvSongs.size();i++){
//Find a song to display
final Song currentSong = msvSongs.get(position);
//Fill the FilmPoster View
ImageView filmPoster = (ImageView)itemView.findViewById(R.id.imgFilmPoster);
filmPoster.setImageResource(currentSong.getFilmImageID());
//Fill the FilmName View
TextView filmName = (TextView)itemView.findViewById(R.id.tvFilmName);
filmName.setText(currentSong.getMovieName());
//Fill the SongName View
TextView songName = (TextView)itemView.findViewById(R.id.tvSongName);
songName.setText(currentSong.getSongName());
//Fill the YearReleased View
TextView yearReleased = (TextView)itemView.findViewById(R.id.tvYearReleased);
yearReleased.setText(currentSong.getYearReleased());
//Fill the Price View
TextView songPrice = (TextView)itemView.findViewById(R.id.tvPrice);
songPrice.setText("Rs. "+currentSong.getSongPrice());
//Fill the Play MP3 View
final ImageView viewMP3 = (ImageView)itemView.findViewById(R.id.imgPlay);
viewMP3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String contentDesc = (String) viewMP3.getContentDescription();
if(contentDesc.contains("Play")){
if (mp3Player != null && mp3Player.isPlaying()){
Toast.makeText(MsvSongs.this, "Stop the previous song", Toast.LENGTH_SHORT).show();
}else{
mp3Player=MediaPlayer.create(MsvSongs.this, currentSong.getmp3File());
mp3Player.start();
viewMP3.setImageResource(drawable.stop);
viewMP3.setContentDescription("Stop MP3");
}
}else{
mp3Player.pause();
mp3Player.stop();
mp3Player.release();
mp3Player=null;
viewMP3.setImageResource(drawable.play);
viewMP3.setContentDescription("Play MP3");
}
}
});
当我在列表视图中单击某首歌曲的“播放”按钮时,它会播放该歌曲并将“播放”图像更改为“停止图像”。但它也会将播放图像更改为停止其他列表视图项的图像。
请您在上面的代码中说明问题是什么?
答案 0 :(得分:0)
请尝试这种方式,希望这有助于您解决问题。
private class MSVSongsAdapter extends BaseAdapter {
private Context context;
private ArrayList<Song> songList;
private MediaPlayer mp3Player;
public MSVSongsAdapter(Context context, ArrayList<Song> songList) {
this.context = context;
this.songList = songList;
}
@Override
public int getCount() {
return songList.size();
}
@Override
public Object getItem(int position) {
return songList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if(convertView==null){
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.songs_view,null);
holder.filmPoster = (ImageView) convertView.findViewById(R.id.imgFilmPoster);
holder.viewMP3 = (ImageView) convertView.findViewById(R.id.viewMP3);
holder.filmName = (TextView) convertView.findViewById(R.id.filmName);
holder.songName = (TextView) convertView.findViewById(R.id.songName);
holder.yearReleased = (TextView) convertView.findViewById(R.id.yearReleased);
holder.songPrice = (TextView) convertView.findViewById(R.id.songPrice);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
holder.filmPoster.setImageResource(songList.get(position).getFilmImageID());
holder.viewMP3.setImageResource(songList.get(position).getFilmImageID());
holder.filmName.setText(songList.get(position).getMovieName());
holder.songName.setText(songList.get(position).getSongName());
holder.yearReleased.setText(songList.get(position).getYearReleased());
holder.songPrice.setText("Rs. " + songList.get(position).getSongPrice());
holder.viewMP3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String contentDesc = (String) songList.get(position).getContentDescription();
if (contentDesc.contains("Play")) {
if (mp3Player != null && mp3Player.isPlaying()) {
Toast.makeText(context, "Stop the previous song", Toast.LENGTH_SHORT).show();
} else {
mp3Player = MediaPlayer.create(context, songList.get(position).getmp3File());
mp3Player.start();
((ImageView)v).setImageResource(R.drawable.stop);
((ImageView)v).setContentDescription("Stop MP3");
}
} else {
mp3Player.pause();
mp3Player.stop();
mp3Player.release();
mp3Player = null;
((ImageView)v).setImageResource(R.drawable.play);
((ImageView)v).setContentDescription("Play MP3");
}
}
});
return null;
}
static class ViewHolder{
ImageView filmPoster;
ImageView viewMP3;
TextView filmName;
TextView songName;
TextView yearReleased;
TextView songPrice;
}
}