Summary: Today, the addition of the alarm clock has been completed, the process is still smooth, but in the end, there was a problem with the use of the long press to delete the alarm clock event, and the long press to delete cannot be achieved. Because the Switch switch control is used in the alarm list, I feel It has something to do with it, so I took out the Switch control and tested it. Sure enough, I realized the long-press delete. The specific reason is still unclear. I plan to solve it tomorrow, and then beautify the interface and then learn the alarm trigger event.
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
package com.example.naozhong; import android.app.TimePickerDialog; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.EditText; import android.widget.TextView; import android.widget.TimePicker; import android.widget.Toast; import androidx.appcompat.app.AppCompatActivity; import java.util.Calendar; public class AddAlarm extends AppCompatActivity implements View.OnClickListener { private TextView hour; private TextView minute1; private Button setTime; private EditText content; private Button save; private Calendar calendar; private AlarmOperator alarmOperator; private AlarmAdapter alarmAdapter; String hourformat; String minuteformat; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.addalarm); alarmOperator=new AlarmOperator(this); hour = findViewById(R.id.hour); minute1 = findViewById(R.id.minute); setTime = findViewById(R.id.set_time); setTime.setOnClickListener(this); content = findViewById(R.id.content); save = findViewById(R.id.save); save.setOnClickListener(this); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.set_time: addtime(); break; case R.id.save: addalarm(); break; } } private void addalarm() { String userhour=hour.getText().toString().trim(); String userminute=minute1.getText().toString().trim(); String usercontent=content.getText().toString().trim(); Alarm bean=new Alarm("2",userhour,userminute,usercontent,0); Log.d("volley", bean.hour); alarmOperator.insert(bean); Intent intent2 = new Intent(this, AlarmActivity.class); startActivity(intent2); } private void addtime() { calendar = Calendar.getInstance(); int mhour = calendar.get(Calendar.HOUR_OF_DAY); int mminute = calendar.get(Calendar.MINUTE); new TimePickerDialog(AddAlarm.this, new TimePickerDialog.OnTimeSetListener() { @Override public void onTimeSet(TimePicker view, int hourOfDay, int minute) { calendar.set(Calendar.HOUR_OF_DAY, hourOfDay); calendar.set(Calendar.MINUTE, minute); Calendar currentTime=Calendar.getInstance(); if(calendar.getTimeInMillis()<=currentTime.getTimeInMillis()){ calendar.setTimeInMillis(calendar.getTimeInMillis()+24*60*60*1000); } hourformat = String.format(String.valueOf(hourOfDay)); minuteformat = String.format(String.valueOf(minute)); Toast.makeText(AddAlarm.this, "" + hourformat + ":" + minuteformat, Toast.LENGTH_SHORT).show(); hour.setText(hourformat); minute1.setText(minuteformat); } }, mhour, mminute, true).show(); } }
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.alarm); addalarm=findViewById(R.id.addalarm); list=new ArrayList<>(); String username="2"; alarmOperator =new AlarmOperator(this); ListView alarmList=(ListView) findViewById(R.id.clock_list); alarmList.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { @Override public boolean onItemLongClick(AdapterView<?> parent, View view, final int position, long id) { AlertDialog.Builder builder=new AlertDialog.Builder(view.getContext()); builder.setMessage("确定删除?"); builder.setTitle("提示"); final Alarm[] alarms = {null}; builder.setPositiveButton("确定", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { alarms[0]=list.get(position); alarmOperator.deletebyuser("1",alarms[0].hour,alarms[0].minute); list.remove(position); alarmAdapter.notifyDataSetChanged(); } }); builder.setNegativeButton("取消", new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { } }); builder.create().show(); return false; } }); init(username); alarmAdapter=new AlarmAdapter(this,list); alarmList.setAdapter(alarmAdapter); addalarm.setOnClickListener(l); }