要求:用Dialog對話框顯示一個列表,並實現其點擊效果java
代碼:ide
private ListView lv_list; private String deleteName; private AlertDialog.Builder builder; private Dialog dialog; private void showDialog() { /** * 建立Dialog,參數爲當前環境與樣式。 */ builder = new AlertDialog.Builder(this); View view = LayoutInflater.from(Managr_BanCi_List.this).inflate( R.layout.dialog_list, null); TextView tv_text = (TextView) view.findViewById(R.id.tv_dialog_list); tv_text.setText("請選擇指派到其餘班次的員工"); lv_list = (ListView) view.findViewById(R.id.lv_dialog_list); builder.setView(view); builder.setCancelable(false); // 設置適配器,適配器參數分別爲:當前環境、listview中的item、item中的textview、textview所需的數據數據 lv_list.setAdapter(new ArrayAdapter<String>(Managr_BanCi_List.this, R.layout.listview_item, R.id.tv_listview_item, data_Delete)); // 爲listview中的item設置點擊事件 lv_list.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { biaoshi = true; Intent intent = new Intent(Managr_BanCi_List.this, Manager_Add.class); DeleteBanC_PeoPleName deleteBanC_PeoPleName = (DeleteBanC_PeoPleName) treeMap_Delete .get(data_Delete[position]); intent.putExtra("companyId", getIntent().getExtras().getString("id")); deleteName = deleteBanC_PeoPleName.name; intent.putExtra("name", deleteBanC_PeoPleName.name);// 姓名 intent.putExtra("dapart", deleteBanC_PeoPleName.dapart);// 部門 intent.putExtra("id", deleteBanC_PeoPleName.id);// 員工 intent.putExtra("loginname", deleteBanC_PeoPleName.loginname);// 登陸用戶名 intent.putExtra("phone", deleteBanC_PeoPleName.phone);// 手機 intent.putExtra("pwd", deleteBanC_PeoPleName.pwd);// 密碼 intent.putExtra("biaoshi", biaoshi);// 員工轉移班次 startActivityForResult(intent, 10); } }); // 顯示 dialog = builder.show(); }
關閉:ui
dialog.dismiss();