Update data into List Item using Tap on button

91 Views Asked by At

I am using Custom Date and Time picker (DateTimePicker in a single dialog)

When i do tap on button in a list item to select date and time and after successful selection when dialog closes and list view comes to the foreground not getting updated data into list item, but when again i do tap on button then it's updating that list item...

So what could be the reasons ?

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

and using below code in onCreate() of Activity:

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

METHOD: 2

NOTE:- Using this code in onCreate(), it's updating but only first list item every time:

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

There are 1 best solutions below

0
On BEST ANSWER

You need to create a function in activity class and pass arraylist item to it

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

call this function inside onClick()

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

and inside onSet() function

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