1

我正在使用Custom Date and Time picker(单个对话框中的 DateTimePicker)

当我点击列表项中的按钮以选择日期和时间并成功选择后,当对话框关闭并且列表视图进入前台not getting updated datalist item,但是当我再次点击按钮时it's updating,该列表项...

那么可能是什么原因呢?

方法一

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
    // convert view = design
    View view = convertView;
    if (view == null) {
        holder = new ViewHolder();
        view = inflater.inflate(Resource, null);


        holder.textDate = (TextView) view.findViewById(R.id.textDate);
        holder.textTime = (TextView) view.findViewById(R.id.textTime);


        view.setTag(holder);
    }
    else 
    {
        holder = (ViewHolder) view.getTag();
    }



    holder.textDate.setText(appointmentsArrayList.get(position).getDate());
    holder.textTime.setText(appointmentsArrayList.get(position).getTime());


    holder.buttonReschedule.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            // TODO Auto-generated method stub
            appointmentsActivity.customDateTimePicker.showDialog();             
            appointmentsArrayList.get(position).setDate(appointmentsActivity.strSelectedDate);
            appointmentsArrayList.get(position).setTime(appointmentsActivity.strSelectedTime);
            try {
                Log.d("setDate:- ", appointmentsArrayList.get(position).getDate()); 
                Log.d("setTime:- ", appointmentsArrayList.get(position).getTime()); 
            } catch (Exception e) {
                // TODO: handle exception
            }               
            notifyDataSetChanged();
        }
    });

并在 Activity 的 onCreate() 中使用以下代码:

customDateTimePicker = new CustomDateTimePicker(this,
                new CustomDateTimePicker.ICustomDateTimeListener() {

                    @Override
                    public void onSet(Dialog dialog, Calendar calendarSelected,
                            Date dateSelected, int year, String monthFullName,
                            String monthShortName, int monthNumber, int date,
                            String weekDayFullName, String weekDayShortName,
                            int hour24, int hour12, int min, int sec,
                            String AM_PM) {


                            strSelectedDate = calendarSelected
                                .get(Calendar.DAY_OF_MONTH)
                                + " " + monthShortName + " " + year;

                            strSelectedTime = hour12 + ":" + min
                                    + " " + AM_PM;

                        adapter.notifyDataSetChanged();

                        Toast.makeText(AppointmentsActivity.this, strSelectedDate+", "+strSelectedTime, Toast.LENGTH_LONG).show();

                    }

                    @Override
                    public void onCancel() {

                    }
                });
        /**
         * Pass Directly current time format it will return AM and PM if you set
         * false
         */
        customDateTimePicker.set24HourFormat(false);
        /**
         * Pass Directly current data and time to show when it pop up
         */
        customDateTimePicker.setDate(Calendar.getInstance());

方法:2

注意:-在 中使用此代码onCreate(),它会更新,但仅first list item每次更新:

              int position = 0;
              ................

                   strSelectedTime = strHour + ":" + strMin
                                    + " " + AM_PM;

                            appointmentsArrayList.get(position).setDate(strSelectedDate);
                            appointmentsArrayList.get(position).setTime(strSelectedTime);
                            try {
                                Log.d("setDate:- ", appointmentsArrayList.get(position).getDate()); 
                                Log.d("setTime:- ", appointmentsArrayList.get(position).getTime());
                            } catch (Exception e) {
                                // TODO: handle exception
                            }       

                            adapter.notifyDataSetChanged();

                        Toast.makeText(AppointmentsActivity.this, strSelectedDate+", "+strSelectedTime, Toast.LENGTH_LONG).show();

适配器.java:

holder.buttonReschedule.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                appointmentsActivity.customDateTimePicker.showDialog();             

                notifyDataSetChanged();
            }
        });
4

1 回答 1

1

您需要在活动类中创建一个函数并将数组列表项传递给它

//change type to your type class of arraylist you are using
public void showDatePicker(Foo foo){
   mFoo=foo  //mFoo is a global variable 
   customDateTimePicker.showDialog();
}

在 onClick() 中调用这个函数

Foo foo=appointmentsArrayList.get(position);
appointmentsActivity.showDatePicker(foo);

在 onSet() 函数内部

onSet(.. ..){
   ...
mFoo.setDate(strSelectedDate);
mFoo.setTime(strSelectedTime);
 adapter.notifyDataSetChanged();
}
于 2015-07-17T11:43:25.083 回答