一般弹出对话框的内容是个提醒信息,或者是“确认”和“取消”的选择信息,如果要是item是从外部获取或者很多个怎么办,下面就介绍使用ListView来填充Item,并有点击事件!
其实写法非常的简单,写一个Adapter类,调用即可
<span style="font-family:Microsoft YaHei;font-size:14px;">package com.example.dialoaglistview; import java.util.List; import java.util.Map; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.BaseAdapter; import android.widget.TextView; public class DialogItemsAdapter extends BaseAdapter{ private Context context; private List<Map<String,Object>> listItems; private LayoutInflater listContainer; public DialogItemsAdapter(Context context,List<Map<String,Object>> listItems){ this.context = context; listContainer = LayoutInflater.from(context); this.listItems = listItems; } public final class ListItemView{ public TextView value; } @Override public int getCount() { return listItems.size(); } @Override public Object getItem(int position) { if(position < listItems.size()){ return listItems.get(position); } return null; } @Override public long getItemId(int position) { return position; } @Override public View getView(int position, View convertView, ViewGroup parent) { ListItemView listItemView = new ListItemView(); if (convertView == null) { convertView = listContainer.inflate(R.layout.listview_item, null); listItemView.value = (TextView) convertView .findViewById(R.id.text_item); convertView.setTag(listItemView); } else { listItemView = (ListItemView) convertView.getTag(); } listItemView.value.setText((String) listItems.get(position) .get("value")); return convertView; } } </span>然后要为ListView单独写一个布局文件
<span style="font-family:Microsoft YaHei;font-size:14px;"><?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:gravity="center_horizontal" android:orientation="vertical" > <TextView android:id="@+id/text_item" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="样式自己调" android:textSize="20sp" /> </LinearLayout> </span>剩下的就是调用这个Adapter了,主要代码如下
<span style="font-family:Microsoft YaHei;font-size:14px;">private void showMyDialog() { final String items[] = {"报告厅","多媒体教室","机房","美术系教室","普通教室","实训基地","实验室","体育","音乐系教室","语音室"}; List<Map<String, Object>> data = new ArrayList<Map<String,Object>>(); for(int i = 0;i<items.length;i++){ Map<String,Object> item = new HashMap<String,Object>(); item.put("value",items[i]); data.add(item); } ListView listView= new ListView(this); //构造listview对象。 DialogItemsAdapter adapter = new DialogItemsAdapter( this, data); listView.setAdapter(adapter); final AlertDialog alertDialog = new AlertDialog.Builder( this).create(); alertDialog.setCanceledOnTouchOutside(false);//使除了dialog以外的地方不能被点击 alertDialog.setTitle("选择教室类型"); alertDialog.setView(listView); alertDialog.show(); listView.setOnItemClickListener(new OnItemClickListener() {//响应listview中的item的点击事件 @Override public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) { // TODO Auto-generated method stub TextView tv = (TextView) arg1 .findViewById(R.id.text_item);//取得每条item中的textview控件 textView.setText(tv.getText().toString()); alertDialog.cancel(); } }); }</span>上面的items可以从外部文件获取,也可以从网络上获取,总之,获取多个item都可以
原文地址:http://blog.csdn.net/qqhjqs/article/details/45317249