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

Update data into List Item using Tap on button

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

当我点击列表项中的按钮时 select 日期和时间,成功后 selection 当对话框关闭并且列表视图出现在前台时 not getting updated data 进入 list item,但是当 再次 我点击按钮然后 it's updating 该列表项...

那么可能是什么原因呢?

方法 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();
        }
    });

并在 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();

Adapter.java:

holder.buttonReschedule.setOnClickListener(new OnClickListener() {

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

                notifyDataSetChanged();
            }
        });

您需要在 activity class 中创建一个函数并将 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() 中调用此函数

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

和 onSet() 函数内部

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