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

时间:2015-07-17 10:58:13

标签: android baseadapter android-viewholder custom-datetimepicker

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

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

那么原因可能是什么?

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

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

1 个答案:

答案 0 :(得分:1)

您需要在活动类中创建一个函数并将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();
}