Android 闹钟 开发过程记录(七)

listview中的item两种方式弹出菜单。

第一种:

		//为listview的item设置长按时弹出一个菜单
		lv_alarm_clock.setOnCreateContextMenuListener(new OnCreateContextMenuListener() {

			@Override
			public void onCreateContextMenu(ContextMenu menu, View v,
					ContextMenuInfo menuInfo) {
				menu.setHeaderTitle("操作");     
				menu.add(0, 0, 0, "修改闹钟");  
				menu.add(0, 1, 0, "删除闹钟");
			}
		});
	//菜单条目被选中的所要执行的操作
	@Override
	public boolean onContextItemSelected(MenuItem item) {
		AdapterContextMenuInfo menuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
		final int position = menuInfo.position;
		switch (item.getItemId()) {
		case 0:	//修改闹钟
			Intent alarmSettingIntent = new Intent(AlarmClockListActivity.this, AlarmClockSettingActivity.class);
			alarmSettingIntent.putExtras(getBundle(position));
			startActivity(alarmSettingIntent);
			break;
		case 1:	//删除闹钟
			AlertDialog.Builder builder = new Builder(this);
			builder.setTitle("提示");
			builder.setMessage("要删除此闹钟吗?");
			builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					int id = infos.get(position).getId();
					AlarmManager alarmManager = (AlarmManager) getSystemService(Context.ALARM_SERVICE);
					Intent intent = new Intent(AlarmClockListActivity.this, AlarmClockReceiver.class);
					intent.setAction("cn.edu.usts.cardhelper.alarmclock");
					intent.putExtras(getBundle(position));
					PendingIntent pi = PendingIntent.getBroadcast(AlarmClockListActivity.this, id, intent, PendingIntent.FLAG_UPDATE_CURRENT);
					alarmManager.cancel(pi);
					Log.i(TAG, "------闹钟:"+id+"的广播取消成功!----");
					dao = new AlarmClockDao(AlarmClockListActivity.this);
					dao.delete(id);
					startThreadToReadSavedAlarmClockInfos();
				}
			});
			builder.setNegativeButton("取消", new OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {

				}
			});
			builder.show();
			break;
		}
		return super.onContextItemSelected(item);
	}

第二种:

	lv.setOnItemLongClickListener(lv_OnItemLongClickListener);

	private OnItemLongClickListener lv_OnItemLongClickListener = new OnItemLongClickListener() {

		@Override
		public boolean onItemLongClick(AdapterView<?> parent, View view,
				final int position, long id) {
			final String[] items = new String[]{"重命名","删除录音"};
			Builder builder = new AlertDialog.Builder(AlarmClockChooseRingsRecordActivity.this).setTitle("文件操作").setItems(items, new DialogInterface.OnClickListener() {

				@Override
				public void onClick(DialogInterface dialog, int which) {
					if(items[which].equals("重命名")){
						Builder builder = new AlertDialog.Builder(AlarmClockChooseRingsRecordActivity.this).setTitle("重命名");

						final EditText et_reName = new EditText(AlarmClockChooseRingsRecordActivity.this);
						et_reName.setText(audioInfos.get(position).getAudioName());
						builder.setView(et_reName);

						builder.setPositiveButton("保存", new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
								String name = et_reName.getText().toString().trim();
								String filePath = audioInfos.get(position).getAudioUri().toString();
								if("".equals(name)){
									ToastUtil.showShortToast(AlarmClockChooseRingsRecordActivity.this, "名称不能为空");
									return;
								}else{
									File oldFile = new File(filePath);
									Log.i(TAG, "---------------"+oldFile+"--------------");
									File newFile = new File(filePath.substring(0, filePath.lastIndexOf("/")), name+".amr");
									Log.i(TAG, "---------------"+newFile+"--------------");
									oldFile.renameTo(newFile);
									startThreadToReFreshRecordsList();
								}
							}
						});
						builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog, int which) {
							}
						});
						builder.show();
					}else{
						Uri uri = audioInfos.get(position).getAudioUri();
						new File(uri.toString()).delete();
						startThreadToReFreshRecordsList();
					}
				}
			});
			builder.show();
			return false;
		}
	};


你可能感兴趣的:(android,ListView,长按弹出菜单项)