• 团队冲刺第八天-KeepRunningAPP


    摘要:今天主要是对昨天的问题进行了修复以及闹钟功能的完善,解决了activity不能跳转到fragment的问题,解决方法是在点击添加后跳出弹出框,用户可以设置闹钟时间以及标签内容,这样就避免了添加闹钟跳转到activity成功后再回到fragment。第二个问题是用户进入闹钟界面显示闹钟开关状态的问题,解决办法对开关加一个监听事件,通过和对数据库的存储的闹钟状态做匹配,让其显示开关状态。今天就完成了这么多,明天计划完成闹钟的触发事件。下面是今天的主要代码和效果图。

    package com.alarm;
    
    import android.content.Context;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.BaseAdapter;
    import android.widget.CompoundButton;
    import android.widget.Switch;
    import android.widget.TextView;
    import android.widget.Toast;
    
    import com.example.keeprunning1.R;
    
    import java.util.List;
    
    public class AlarmAdapter extends BaseAdapter {
        private List<Alarm> mlist;
        private Context mContext;
        private LayoutInflater mlayoutInflater;
        private AlarmOperator malarmOperator;;
        public AlarmAdapter(Context context,List<Alarm> list){
            mContext=context;
            mlist=list;
            mlayoutInflater=LayoutInflater.from(context);
            malarmOperator=new AlarmOperator(context);
        }
    
        @Override
        public int getCount() {
            return mlist.size();
        }
    
        @Override
        public Object getItem(int position) {
            return position;
        }
    
        @Override
        public long getItemId(int position) {
            return position;
        }
    
        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final ViewHolder viewHolder;
            if(convertView==null){
                viewHolder=new ViewHolder();
                convertView=mlayoutInflater.inflate(R.layout.alarmitem,null);
                viewHolder.mHour=convertView.findViewById(R.id.hour);
                viewHolder.mMinute=convertView.findViewById(R.id.minute);
                viewHolder.mContent=convertView.findViewById(R.id.content_item);
                viewHolder.mClockType=convertView.findViewById(R.id.switch_control);
                convertView.setTag(viewHolder);
            }else{
                viewHolder=(ViewHolder)convertView.getTag();
            }
    
            final Alarm bean=mlist.get(position);
            final String name=bean.getUsername();
            final String hour=bean.getHour();
            final String minute=bean.getMinute();
            Log.i("name", name);
            viewHolder.mClockType.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if(isChecked) {
                        malarmOperator.updateswitch(name,hour,minute);
                    }else{
                        malarmOperator.updateswitch1(name,hour,minute);
    
                    }
                }
            });
            int clockType=bean.getClockType();
            Log.d("volley", String.valueOf(clockType));
            if(clockType==0){
                viewHolder.mClockType.setChecked(false);
            }else {
                viewHolder.mClockType.setChecked(true);
            }
            viewHolder.mContent.setText(bean.content);
            viewHolder.mHour.setText(bean.hour);
            viewHolder.mMinute.setText(bean.minute);
            return convertView;
        }
        public class ViewHolder{
            public TextView mHour;
            public TextView mMinute;
            public TextView mContent;
            public Switch mClockType;
            //public CheckBox mCheckBox;
        }
    }
    AlarmAdapter
    package com.alarm;
    
    import android.app.TimePickerDialog;
    import android.content.DialogInterface;
    import android.database.Cursor;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.LayoutInflater;
    import android.view.View;
    import android.view.ViewGroup;
    import android.widget.AdapterView;
    import android.widget.Button;
    import android.widget.EditText;
    import android.widget.ListView;
    import android.widget.Switch;
    import android.widget.TextView;
    import android.widget.TimePicker;
    import android.widget.Toast;
    
    import androidx.annotation.Nullable;
    import androidx.appcompat.app.AlertDialog;
    import androidx.fragment.app.Fragment;
    
    import com.example.keeprunning1.R;
    
    import java.util.ArrayList;
    import java.util.Calendar;
    import java.util.List;
    
    public class AlarmFragment extends Fragment {
        private List<Alarm> list;
        private Button addalarm;
        private AlarmOperator alarmOperator;
        private AlarmAdapter alarmAdapter;
        private TextView hour;
        private TextView minute1;
        private EditText content;
        private Button setTime;
        View view;
        String username;
        String hourformat;
        String minuteformat;
        private Calendar calendar;
        private Switch aSwitch;
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            Log.i("hello", "hello");
        }
        @Nullable
        @Override
        public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, Bundle savedInstanceState) {
            view = inflater.inflate(R.layout.alarm, null);
            username = getArguments().getString("username");
            Log.i("username", username);
            addalarm = view.findViewById(R.id.addalarm);
            list = new ArrayList<>();
            alarmOperator = new AlarmOperator(view.getContext());
            ListView alarmList =view.findViewById(R.id.clock_list);
            init(username);
            alarmAdapter = new AlarmAdapter(view.getContext(), list);
            alarmList.setAdapter(alarmAdapter);
            addalarm.setOnClickListener(l);
            delete();
            aSwitch=view.findViewById(R.id.switch_control);
            //updateSwitch();
            return view;
        }
    
        View.OnClickListener l = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                switch (v.getId()) {
                    case R.id.addalarm:
                        alarm();
                        break;
                }
            }
        };
    
        public void alarm(){
            AlertDialog.Builder builder2 = new AlertDialog.Builder(getView().getContext());
            LayoutInflater inflater2 = LayoutInflater.from(getView().getContext());
            View viewDialog2 = inflater2.inflate(R.layout.addalarm, null);
            hour = viewDialog2.findViewById(R.id.hour);
            minute1 = viewDialog2.findViewById(R.id.minute);
            setTime = viewDialog2.findViewById(R.id.set_time);
            addtime();
            content = viewDialog2.findViewById(R.id.content);
            builder2.setView(viewDialog2);
            builder2.setTitle("新闹钟");
            builder2.setPositiveButton("确定", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    String userhour = hour.getText().toString().trim();
                    String userminute = minute1.getText().toString().trim();
                    String usercontent = content.getText().toString().trim();
                    Alarm bean = new Alarm(username, userhour, userminute, usercontent, 0);
                    Log.d("volley", bean.hour);
                    alarmOperator.insert(bean);
                    list.add(bean);
                    Toast.makeText(view.getContext(), bean.hour+":"+bean.minute+"添加成功", Toast.LENGTH_SHORT).show();
                    alarmAdapter.notifyDataSetChanged();
                }
            });
            builder2.setNegativeButton("取消", null);
            builder2.create().show();
        }
    
        public void addtime() {
            setTime.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    calendar = Calendar.getInstance();
                    int mhour = calendar.get(Calendar.HOUR_OF_DAY);
                    int mminute = calendar.get(Calendar.MINUTE);
                    new TimePickerDialog(view.getContext(), 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(view.getContext(), "" + hourformat + ":" + minuteformat, Toast.LENGTH_SHORT).show();
                            hour.setText(hourformat);
                            minute1.setText(minuteformat);
    
                        }
                    }, mhour, mminute, true).show();
                }
            });
        }
    
        public void delete() {
            ListView alarmList = (ListView) view.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(username, 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;
                }
            });
        }
    
        private void init(String username) {
            Cursor cursor = (Cursor) alarmOperator.findall(username);
            if (cursor != null) {
                while (cursor.moveToNext()) {
                    Alarm bean = new Alarm();
                    bean.content = cursor.getString(cursor.getColumnIndex("content"));
                    bean.hour = cursor.getString(cursor.getColumnIndex("hour"));
                    bean.minute = cursor.getString(cursor.getColumnIndex("minute"));
                    bean.username = cursor.getString(cursor.getColumnIndex("username"));
                    bean.clockType = cursor.getInt(cursor.getColumnIndex("clockType"));
                    Log.d("volley", bean.hour);
                    list.add(bean);
                }
                cursor.close();
            }
        }
    
        //public void updateSwitch(){
    //        if(aSwitch.isChecked()==true){
    //           alarmOperator.updateswitch(username);
    //        }
       // }
    }
    AlarmFragment

     

  • 相关阅读:
    Office加载项安装
    Office加载项
    centos部署vue项目
    centos系统下安装Nginx
    MongoDB 安装笔记
    CDN基本工作过程
    前端常见跨域解决方案(全)
    JS 扁平化(flatten) 数组
    console.log 打印的值不准确
    arr.flat(Infinity)数组扁平化
  • 原文地址:https://www.cnblogs.com/MoooJL/p/12757599.html
Copyright © 2020-2023  润新知