Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/xpath/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 使用点击按钮将数据更新到列表项中_Android_Baseadapter_Android Viewholder_Custom Datetimepicker - Fatal编程技术网

Android 使用点击按钮将数据更新到列表项中

Android 使用点击按钮将数据更新到列表项中,android,baseadapter,android-viewholder,custom-datetimepicker,Android,Baseadapter,Android Viewholder,Custom Datetimepicker,我正在使用自定义日期和时间选择器(单个对话框中的日期时间选择器) 当我点击列表项中的按钮来选择日期和时间,并且在成功选择之后,当对话框关闭并且列表视图出现在前台时,没有将更新的数据获取到列表项,但当再次时,我点击按钮,然后它正在更新该列表项 那么原因是什么呢 方法1 @Override public View getView(final int position, View convertView, ViewGroup parent) { // convert view = design

我正在使用
自定义日期和时间选择器
(单个对话框中的日期时间选择器)

当我点击列表项中的按钮来选择日期和时间,并且在成功选择之后,当对话框关闭并且列表视图出现在前台时,
没有将更新的数据
获取到
列表项
,但当再次时,我点击按钮,然后
它正在更新该列表项

那么原因是什么呢

方法1

@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();
        }
    });
并在活动的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()
中使用此代码,它将更新,但每次仅更新
第一个列表项

              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();
Adapter.java:

holder.buttonReschedule.setOnClickListener(new OnClickListener() {

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

                notifyDataSetChanged();
            }
        });

您需要在activity类中创建一个函数,并将arraylist项传递给它

//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()内调用此函数

函数的内部函数

onSet(.. ..){
   ...
mFoo.setDate(strSelectedDate);
mFoo.setTime(strSelectedTime);
 adapter.notifyDataSetChanged();
}
onSet(.. ..){
   ...
mFoo.setDate(strSelectedDate);
mFoo.setTime(strSelectedTime);
 adapter.notifyDataSetChanged();
}