对话框学习

ListView的列表项单击事件
             为ListView设置列表项单击事件
           setOnitemClickListener(OnItemClickListener listener)
   OnItemClickListener
   void onitemClick(AdapterView<?> parent,View view,int position,long id)


   AlertDialog对话框(1)
      对话框一般用于提示信息和与当前应用程序直接相关的小功能
      一个对话框一般是一个出现在当前Activity之上的一个小窗口。
          处于下面的Activity失去焦点,对话框接受所有的用户交互
      对话框分类:
             警示对话框
     进度对话框
     日期、时间对话框
     单选、复选对话框
     列表项对话框
        AlertDialog对话框(2)
    警示对话框是最基本的对话框
      对话框的构建
    AlertDialog.Builder
         Builder setIcon(int iconRes)
 Builder setIcon(Drawable icon)
 Builder setTitle(CharSequence title)
 Builder setTitle(int titleRes)
 Builder setMessage(CharSequence msg)
 Builder SetMessage(int msgRes)
 Builder setCancelable(boolean cancelable)
     
     AlertDialogBuilder
               Builder setPositiveButton(CharSequence text,OnClickListener listener)
      Builder setPositiveButton(int textRes,OnChickListener listener)
      Builder setNegativeButton(CharSequence text,OnClickListener listener)
      Builder setNegativeButton(int textRes,OnClickListener listener)
               Builder setNeutralButton(CharSequence text,OnClickListener listener)
      Builder setNeutralButton(int textRes,OnClickListener listener)
            AlertDialog create()
   AlertDialog show()




   AlertDialog
       void show()
void dismiss()
void setTitle(ChartSequence title)
void setMessage(CharSequence text)
void setIcon(int iconRes)


    单击列表项显示详情对话框
            要点:
     为自定义适配器重写getitem
      和getItemId方法
     创建对话框

     为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"
     />

 界面item.xml布局:

   

<?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;
        }
}


你可能感兴趣的:(对话框学习)