我有一个包含3个EditText的列表视图。一个EditText是一个Date字段,我在该字段上放置一个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);
}
}
答案 0 :(得分: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;
}
}