Answer the question
In order to leave comments, you need to log in
Why is ListActivity not updated by notifyDataSetChanged?
Good afternoon!
It seems like a simple program, but after selecting a date in the dialog, the data on the ListActivity is not updated.
And if you call the onClick handler again by clicking on the field, then, even before the dialog screen appears, the data will be updated.
The update call was originally in onListItemClick, if anything.
public class MainActivity extends ListActivity {
int DIALOG_DATE = 1;
int myYear;
int myMonth;
int myDay;
ArrayAdapter<String> mAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SimpleDateFormat curFormat = new SimpleDateFormat("dd.MM.yyyy");
Date[] idate52 = new Date[8];
String[] idateString52 = new String[8];
Date[] date52 = idate52;
String[] dateString52 = idateString52;
mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
GregorianCalendar vCalendar = new GregorianCalendar();
date52[0] = vCalendar.getTime();
myYear = vCalendar.get(vCalendar.YEAR);
myMonth = vCalendar.get(vCalendar.MONTH);
myDay = vCalendar.get(vCalendar.DAY_OF_MONTH);
dateString52[0] = curFormat.format(date52[0]).toString();
mAdapter.add(dateString52[0]);
vCalendar.setTime(date52[0]);
for (int i = 1; i < 8; i++) {
vCalendar.add(vCalendar.DAY_OF_MONTH, -52);
date52[i] = vCalendar.getTime();
vCalendar.setTime(date52[i]);
dateString52[i] = curFormat.format(date52[i]).toString();
mAdapter.add(dateString52[i]);
};
setListAdapter(mAdapter);
}
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
if (position == 0) {
showDialog(DIALOG_DATE);
reCalc();
}
}
protected Dialog onCreateDialog(int id){
if (id == DIALOG_DATE ) {
DatePickerDialog gDPD = new DatePickerDialog(this, myCallBack, myYear, myMonth, myDay );
return gDPD;
}
return super.onCreateDialog(id);
}
protected void reCalc () {
SimpleDateFormat curFormat = new SimpleDateFormat("dd.MM.yyyy");
Date[] date52 = new Date[8];
String[] dateString52 = new String[8];
GregorianCalendar vCalendar = new GregorianCalendar();
vCalendar.set(myYear, myMonth, myDay);
date52[0] = vCalendar.getTime();
dateString52[0] = curFormat.format(date52[0]).toString();
vCalendar.setTime(date52[0]);
mAdapter.clear();
mAdapter.add(dateString52[0]);
for (int i = 1; i < 8; i++) {
vCalendar.add(vCalendar.DAY_OF_MONTH, -52);
date52[i] = vCalendar.getTime();
vCalendar.setTime(date52[i]);
dateString52[i] = curFormat.format(date52[i]).toString();
mAdapter.add(dateString52[i]);
};
// mAdapter.addAll(dateString52);
mAdapter.notifyDataSetChanged();
}
OnDateSetListener myCallBack = new OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
myYear = year;
myMonth = month;
myDay = dayOfMonth;
}
};
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question