为ListView添加列表项单击事件监听器
实现的简单的对话框:
ListView 布局:
<ListView android:id="@+id/lvMusics" android:layout_width="match_parent" android:layout_height="match_parent" xmlns:android="http://schemas.android.com/apk/res/android" />
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > <ImageView android:layout_width="64dp" android:layout_height="64dp" android:src="@drawable/image"/> <TableLayout android:layout_width="0dp" android:layout_height="match_parent" android:layout_weight="1.0" android:stretchColumns="*"> <TableRow android:layout_height="0dp" android:layout_weight="1.0"> <TextView android:id="@+id/tvName" android:layout_height="match_parent" android:gravity="center_vertical" android:textSize="20sp"/> <TextView android:id="@+id/tvDuration" android:layout_height="match_parent" android:gravity="center_vertical" android:textSize="20sp"/> </TableRow> <TableRow android:layout_height="0dp" android:layout_weight="1.0"> <TextView android:id="@+id/tvArtist" android:layout_height="match_parent" android:gravity="center_vertical" android:textSize="20sp"/> <TextView android:id="@+id/tvAlbum" android:layout_height="match_parent" android:gravity="center_vertical" android:textSize="20sp"/> </TableRow> </TableLayout> </LinearLayout>
对话框:
package com.example.listview1; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.AdapterView.OnItemClickListener; import android.widget.ListView; public class MainActivity extends Activity { private ListView lvMusics; private MusicAdapter adapter; private int page; private AlertDialog dialog; private void setupView(){ lvMusics = (ListView) findViewById(R.id.lvMusics); page = 1; adapter = new MusicAdapter(this, MusicBiz.getMusics(page)); lvMusics.setAdapter(adapter); dialog = new AlertDialog.Builder(MainActivity.this).setIcon(android.R.drawable.ic_dialog_info) .setTitle("音乐详情") .setPositiveButton("确定",null) .create(); } private void addListener(){ lvMusics.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { // TODO Auto-generated method stub // Log.i("info","position:" + position + ",id:" + id); // Music m = (Music)adapter.getItem(position); // Log.i("info",m.toString()); // Toast.makeText(MainActivity.this, "position:" + position + ",id:" + id + " " + m.toString(), Toast.LENGTH_LONG).show(); Music m = adapter.getItem(position); dialog.setMessage(m.toString()); dialog.show(); } }); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); setupView(); addListener(); } }
对象:
package com.example.listview1; public class Music { private String name; private String album; private int id; private String duration; private String artist; private String musicPath; private String albumPicPath; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAlbum() { return album; } public void setAlbum(String album) { this.album = album; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getDuration() { return duration; } public void setDuration(String duration) { this.duration = duration; } public String getArtist() { return artist; } public void setArtist(String artist) { this.artist = artist; } public String getMusicPath() { return musicPath; } public void setMusicPath(String musicPath) { this.musicPath = musicPath; } public String getAlbumPicPath() { return albumPicPath; } public void setAlbumPicPath(String albumPicPath) { this.albumPicPath = albumPicPath; } public String toString(){ return "Music [id=" + id + ",name=" + name + ",album=" + album + ",duration=" + duration + ",artist=" + artist + ",musicPath=" + musicPath + ",albumPicPath=" + albumPicPath + "]"; } }
适配器:
package com.example.listview1; public class Music { private String name; private String album; private int id; private String duration; private String artist; private String musicPath; private String albumPicPath; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAlbum() { return album; } public void setAlbum(String album) { this.album = album; } public int getId() { return id; } public void setId(int id) { this.id = id; } public String getDuration() { return duration; } public void setDuration(String duration) { this.duration = duration; } public String getArtist() { return artist; } public void setArtist(String artist) { this.artist = artist; } public String getMusicPath() { return musicPath; } public void setMusicPath(String musicPath) { this.musicPath = musicPath; } public String getAlbumPicPath() { return albumPicPath; } public void setAlbumPicPath(String albumPicPath) { this.albumPicPath = albumPicPath; } public String toString(){ return "Music [id=" + id + ",name=" + name + ",album=" + album + ",duration=" + duration + ",artist=" + artist + ",musicPath=" + musicPath + ",albumPicPath=" + albumPicPath + "]"; } }
数据:
package com.example.listview1; import java.util.ArrayList; public class MusicBiz { public static ArrayList<Music> getMusics(int page){ ArrayList<Music> musics = new ArrayList<Music>(); int i=(page - 1) * 20 +1; int end = i+20; for(;i<end;i++){ Music m = new Music(); m.setId(i); m.setName("歌曲" + i); m.setAlbum("专辑" + i); m.setAlbumPicPath("D:/www.123/123/1.jpg"); m.setDuration("14:23"); m.setArtist("陈奕迅"); m.setMusicPath("http://www.123wer/123/music.jpg"); musics.add(m); } return musics; } }