用xml布局来实现,不多说,直接贴代码
Test4Activity.java 代码:
public class Test4Activity extends Activity {
private Button button;
private TextView textView;
private
ArrayList<HashMap<String,
Object>> listItem;
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button)findViewById(R.id.button);
textView = (TextView)findViewById(R.id.textView);
button.setOnClickListener(new OnClickListener(){
@Override
public void
onClick(View v) {
//
TODO Auto-generated method stub
openAlertDialog(); //打开选项窗口
}
});
}
private void
openAlertDialog() {
// TODO Auto-generated method
stub
AlertDialog ad = new
AlertDialog.Builder(Test4Activity.this)
.setTitle("添加附件")
.setAdapter(getAdapter(), new
DialogInterface.OnClickListener() {
@Override
public
void onClick(DialogInterface dialog, int which) {
//
TODO Auto-generated method stub
//获取选中项的内容
textView.setText(listItem.get(which).get("ItemManager").toString());
}
}).show();
}
//获取设配器内容(要显示的效果)
private
ListAdapter getAdapter(){
listItem = new
ArrayList<HashMap<String,
Object>>();
HashMap<String, Object> map1 = new
HashMap<String, Object>();
map1.put("ImageManager", R.drawable.ic_launcher);
map1.put("ItemManager", "图片");
listItem.add(map1);
HashMap<String, Object> map2 = new
HashMap<String, Object>();
map2.put("ImageManager", R.drawable.ic_launcher);
map2.put("ItemManager", "音频");
listItem.add(map2);
HashMap<String, Object> map3 = new
HashMap<String, Object>();
map3.put("ImageManager", R.drawable.ic_launcher);
map3.put("ItemManager", "文档");
listItem.add(map3);
SimpleAdapter listItemAdapter= new
SimpleAdapter(this,listItem,R.layout.list_item,
new
String[]{"ImageManager","ItemManager"},
new
int[]{R.id.image,R.id.text});
return listItemAdapter;
}
}
main.xml 代码:
<?xml version="1.0"
encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_vertical">
<TextView android:id="@+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</LinearLayout>
要显示的效果布局 list_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"
android:gravity="center_vertical"
>
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
加载中,请稍候......