列表视图复选框未选中的事件和两个edittext值输入然后复选框选中但edittext值更改但旧的vlaue不删除在android中

时间:2014-01-08 05:21:20

标签: android listview checkbox

    public class AddNewClasssAdapter extends BaseAdapter {
        Context context;
        List<AddNewClassRowItem> rowItems;
        List<TimingData> editTimeList;
        List<StudentData> editStudentList;

        List<String> timeDataEdit;
        List<String> studentDataEdit;

        LayoutInflater inflater;
        // Search 
        Calendar mcurrentTime;
        String zone = "";

        public AddNewClasssAdapter(Context context, List<AddNewClassRowItem> items) {
            this.context = context;
            this.rowItems = items;
            timeData = new ArrayList<String>();
        }

        public AddNewClasssAdapter(Context context, List<AddNewClassRowItem> items, List<TimingData> editTimeList, List<StudentData> editStudentList) {
            this.context = context;
            this.rowItems = items;
            this.editTimeList = editTimeList;
            this.editStudentList = editStudentList;
            timeDataEdit = new ArrayList<String>();
            studentDataEdit = new ArrayList<String>();
        }

        public class ViewHolder {
            TextView txtDayCreateAddNewClass, txtTimeCreateAddNewClass,txtToTimeCreateAddNewClass;
            CheckBox checkBoxCreateAddNewClass;

        }

        public int getCount() {
            // return array_str_logo.length;
            return rowItems.size();
        }

        public Object getItem(int position) {
            return rowItems.get(position);
        }

        public long getItemId(int position) {
            return rowItems.indexOf(getItem(position));
        }

        public View getView(final int position, View convertView, ViewGroup parent) {

            final ViewHolder holder;
            LayoutInflater mInflater = (LayoutInflater) context
                    .getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.custom_create_add_new_class, null);
                holder = new ViewHolder();
                holder.txtDayCreateAddNewClass = (TextView) convertView.findViewById(R.id.txt_day_create_add_new_class);
                holder.txtTimeCreateAddNewClass = (TextView) convertView.findViewById(R.id.txt_time_create_add_new_class);
                holder.txtToTimeCreateAddNewClass = (TextView) convertView.findViewById(R.id.txt_to_time_create_add_new_class);
                holder.checkBoxCreateAddNewClass = (CheckBox) convertView.findViewById(R.id.check_box_create_add_new_class);
                convertView.setTag(holder);
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            // Set the results into TextViews
            /*
             * holder.txt_cust_student_name.setText(array_str_title[position]);
             * holder.txt_cust_class_id.setText(array_str_message[position]);
             * holder.txt_cust_group.setText(array_str_date[position]);
             */

            AddNewClassRowItem rowItem = (AddNewClassRowItem) getItem(position);

            holder.txtDayCreateAddNewClass.setText(rowItem.getDay_name());

            if(isClassEdit.equals("1")){

                try {
                    for (int i = 0; i < editTimeList.size(); i++) {
                        if(holder.txtDayCreateAddNewClass.getText().toString().equals(editTimeList.get(i).getDay())
                                && !holder.checkBoxCreateAddNewClass.isChecked()){
                            holder.checkBoxCreateAddNewClass.setChecked(true);
                            holder.txtTimeCreateAddNewClass.setText(editTimeList.get(i).getStartTime());
                            holder.txtToTimeCreateAddNewClass.setText(editTimeList.get(i).getEndTime());
                            timeDataEdit.add("{\\\"endtime\\\":\\\""
                                    + editTimeList.get(i).getEndTime()
                                    + "\\\",\\\"starttime\\\":\\\""
                                    + editTimeList.get(i).getStartTime()
                                    + "\\\",\\\"day\\\":\\\""
                                    + editTimeList.get(i).getDay() + "\\\"}");
                        }
                    }

                    timeData = timeDataEdit;
                    strTimeString = "{\"main\":\"" + timeDataEdit + "\"}";
                    // TODO
//                  for (int i = 0; i < editStudentList.size(); i++) {
//                      if(holder.txtDayCreateAddNewClass.getText().toString().equals(editTimeList.get(i).getDay())
//                              && !holder.checkBoxCreateAddNewClass.isChecked()){
//                      studentDataEdit.add("{\\\"sid\\\":\\\""
//                              + editStudentList.get(i).getStudent_id().toString()
//                              + "\\\"}");
////                        studentDataEdit.add(editStudentList.get(i).getStudent_id());
//                      }
//                  }
//                  
//                  studentData = studentDataEdit;
//                  strStudentString = "{\"studentdata\":\"" + studentDataEdit + "\"}";
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            holder.txtTimeCreateAddNewClass.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    try {
                        Calendar mcurrentTime = Calendar.getInstance();
                        int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                        int minute = mcurrentTime.get(Calendar.MINUTE);
                        TimePickerDialog mTimePicker;
                        mTimePicker = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {

                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay,   int minute) {String timeSet = "";
                            if (hourOfDay > 12) {
                                hourOfDay -= 12;
                                timeSet = "PM";
                            } else if (hourOfDay == 0) {
                                hourOfDay += 12;
                                timeSet = "AM";
                            } else if (hourOfDay == 12)
                                timeSet = "PM";
                            else
                                timeSet = "AM";

                            String minutes = "";
                            if (minute < 10)
                                minutes = "0" + minute;
                            else
                                minutes = String.valueOf(minute);

                            //holder.txtTimeCreateAddNewClass.setText(new StringBuilder().append(hourOfDay).append(":").append(minute).append(" ").append(zone));
                            //holder.txtTimeCreateAddNewClass.setText( hourOfDay + ":" + minute);   

                            String aTime = new StringBuilder().append(hourOfDay).append(':').append(minutes).append(" ").append(timeSet).toString();

                            holder.txtTimeCreateAddNewClass.setText(aTime);}

                        }, hour, minute, false);//Yes 24 hour time
                        mTimePicker.setTitle("Select Time");
                        mTimePicker.show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

            holder.txtToTimeCreateAddNewClass.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    try {
                        mcurrentTime = Calendar.getInstance();
                        int hour = mcurrentTime.get(Calendar.HOUR_OF_DAY);
                        int minute = mcurrentTime.get(Calendar.MINUTE);

                        TimePickerDialog mTimePicker;
                        mTimePicker = new TimePickerDialog(context, new TimePickerDialog.OnTimeSetListener() {

                            @Override
                            public void onTimeSet(TimePicker view, int hourOfDay,   int minute) {
                                String timeSet = "";
                                if (hourOfDay > 12) {
                                    hourOfDay -= 12;
                                    timeSet = "PM";
                                } else if (hourOfDay == 0) {
                                    hourOfDay += 12;
                                    timeSet = "AM";
                                } else if (hourOfDay == 12)
                                    timeSet = "PM";
                                else
                                    timeSet = "AM";

                                String minutes = "";
                                if (minute < 10)
                                    minutes = "0" + minute;
                                else
                                    minutes = String.valueOf(minute);

                                holder.txtToTimeCreateAddNewClass.setText(new StringBuilder().append(hourOfDay).append(":").append(minutes).append(" ").append(timeSet));

                                //holder.txtToTimeCreateAddNewClass.setText( hourOfDay + ":" + minute+":" +mcurrentTime.get(Calendar.AM_PM)+"");                        
                            }

                        }, hour, minute, false);//Yes 24 hour time
                        mTimePicker.setTitle("Select Time");
                        mTimePicker.show();
                    } catch (Exception e) {
                        e.printStackTrace();
                    }

                }
            });


            //System.out.println("\"\"");
            holder.checkBoxCreateAddNewClass.setOnCheckedChangeListener(new OnCheckedChangeListener() {
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                    try {
                        if (isChecked) {
                            String toTime=holder.txtToTimeCreateAddNewClass.getText().toString();
                            String TimeString=holder.txtTimeCreateAddNewClass.getText().toString();
                            //String txtDay=holder.txtDayCreateAddNewClass.getText().toString();

                            if(TimeString=="" || toTime==""){
                                showMessage("Warning", "Please select time", null);
                                holder.checkBoxCreateAddNewClass.setChecked(false);
                            }
                            else{
                                timeData.add("{\\\"endtime\\\":\\\""
                                        + holder.txtToTimeCreateAddNewClass.getText().toString()
                                        + "\\\",\\\"starttime\\\":\\\""
                                        + holder.txtTimeCreateAddNewClass.getText().toString()
                                        + "\\\",\\\"day\\\":\\\""
                                        + holder.txtDayCreateAddNewClass.getText().toString() + "\\\"}");

                                holder.txtToTimeCreateAddNewClass.setEnabled(false);
                                holder.txtTimeCreateAddNewClass.setEnabled(false);

//                          strTimeString = "{\"main\":\"" + timeData + "\"}";
//
//                          Log.e("Json String:", "" + strTimeString);
                            }
                        }else{
                            timeData.remove("{\\\"endtime\\\":\\\""
                                    + holder.txtToTimeCreateAddNewClass.getText().toString()
                                    + "\\\",\\\"starttime\\\":\\\""
                                    + holder.txtTimeCreateAddNewClass.getText().toString()
                                    + "\\\",\\\"day\\\":\\\""
                                    + holder.txtDayCreateAddNewClass.getText().toString() + "\\\"}");

//                      strTimeString = "{\"main\":\"" + timeData + "\"}";
//
//                      Log.e("Json String:", "" + strTimeString);
                        }
                        strTimeString = "{\"main\":\"" + timeData + "\"}";
                        Log.e("Json String:", "" + strTimeString);
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });
            return convertView;
        }

    }// End of Add_New_Class_list_Adapter Class



enter code here

第一次检查框值已检查并且在更改日期之后但不是空显示

{"main":"[{\"endtime\":\"10:44 AM\",\"starttime\":\"10:44 AM\",\"day\":\"Sun\"}]"}

第一次检查框值已检查并且在更改日期之后但是旧的arry不为null并且新的arry添加

{"main":"[{\"endtime\":\"10:44 AM\",\"starttime\":\"10:44 AM\",\"day\":\"Sun\"}, {\"endtime\":\"10:45 AM\",\"starttime\":\"10:44 AM\",\"day\":\"Mon\"}]"}

0 个答案:

没有答案