1

我有一个带有 3 个 EditText 的列表视图。一个 EditText 是一个日期字段,我在该字段上放置了一个 onClickListener 以打开 datePickerDialog。我的问题是当我选择一个日期时,它不会将文本放回 EditText 中。有谁知道如何解决这一问题?

这是我的代码:

public class BezoekverslagAfsprakenListAdapter extends ArrayAdapter<Afspraak> implements OnDateSetListener{
public Context context;
public List<Afspraak> afspraken;
ViewHolder holder;
DatePickerDialog dialog;
int dag, maand, jaar;
DatePicker picker;

public BezoekverslagAfsprakenListAdapter(Activity context, List<Afspraak> afspraken){
    super(context, R.layout.afspraak_item, afspraken);
    this.context = context;
    this.afspraken = afspraken;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {   
    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.afspraak_item, null, false);
        holder = new ViewHolder();
        holder.afspraak = (EditText)convertView.findViewById(R.id.et_afspraak);
        holder.wie = (EditText)convertView.findViewById(R.id.et_wie);
        holder.datum = (EditText)convertView.findViewById(R.id.et_datumAfspraak);
        holder.position = position;
        convertView.setTag(holder);

        holder.datum.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                // your click actions go here
                Calendar c = Calendar.getInstance();
                int mYear = c.get(Calendar.YEAR);
                int mMonth = c.get(Calendar.MONTH);
                int mDay = c.get(Calendar.DAY_OF_MONTH);
                dialog = new DatePickerDialog(context, position, null, mYear, mMonth, mDay);
                dialog.show();  
            }
        }); 
    }
    else{
        holder = (ViewHolder) convertView.getTag();
    }   

    return convertView;
}

static class ViewHolder {
      EditText afspraak;
      EditText wie;
      EditText datum;
      int position;
    }

@Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
        int dayOfMonth) {
    // TODO Auto-generated method stub
    monthOfYear = monthOfYear +1;
    holder.datum.setText(dayOfMonth+"-"+monthOfYear+"-"+year);
}

}

4

1 回答 1

0

我已经自己修好了!

在我的活动中,我添加了这些方法:

DatePickerDialog.OnDateSetListener dateListener =  new DatePickerDialog.OnDateSetListener() {

    @Override
    public void onDateSet(DatePicker view, int year, int monthOfYear,
            int dayOfMonth) {
        // TODO Auto-generated method stub
        myYear = year;
        myMonth = monthOfYear;
        myDay = dayOfMonth;
        myMonth = myMonth +1;
        bezoekverslagAfsprakenListAdapter.getItem(bezoekverslagAfsprakenListAdapter.globalPosition).setDatum(myDay +"-"+myMonth+"-"+myYear);
        bezoekverslagAfsprakenListAdapter.notifyDataSetChanged();
    }}; 

 @Override
    protected Dialog onCreateDialog(int id) {  
        switch (id) {    
        case DATE_DIALOG_ID:        
            return new DatePickerDialog(this,dateListener,myYear, myMonth,myDay );    
        }    
        return null;
    }

和我的 ListAdapter 代码:

public class BezoekverslagAfsprakenListAdapter extends ArrayAdapter<Afspraak> /*implements OnDateSetListener*/{
public Context context;
public List<Afspraak> afspraken;
ViewHolder holder;
DatePickerDialog dialog;
int dag, maand, jaar;
DatePicker picker;
public int mYear;
public int mMonth;
public int mDay;
static final int DATE_DIALOG_ID = 0;
int globalPosition;
String dateSet;
//private DatePickerDialog.OnDateSetListener mDateSetListener;

public BezoekverslagAfsprakenListAdapter(Activity context, List<Afspraak> afspraken){
    super(context, R.layout.afspraak_item, afspraken);
    this.context = context;
    this.afspraken = afspraken;
}

public void setDateSet(String dateSet) {
    this.dateSet = dateSet;
    holder.datum.setText(dateSet);
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {   
    if(convertView == null) {
        LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.afspraak_item, null, false);
        holder = new ViewHolder();
        holder.afspraak = (EditText)convertView.findViewById(R.id.et_afspraak);
        holder.wie = (EditText)convertView.findViewById(R.id.et_wie);           

        holder.datum = (EditText)convertView.findViewById(R.id.et_datumAfspraak);

        holder.datum.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View view)
            {                 
                ((Activity) BezoekverslagAfsprakenListAdapter.this.context)
                .showDialog(DATE_DIALOG_ID);                   
                globalPosition = position; 
            }
        });
        convertView.setTag(holder);
    }
    else{
        holder = (ViewHolder) convertView.getTag();
    }   

    holder.position = position; 

    holder.datum.setText(afspraken.get(position).getDatum());

    return convertView;
}

static class ViewHolder {
      EditText afspraak;
      EditText wie;
      EditText datum;
      int position;
    }

}

于 2013-09-16T08:43:37.077 回答