如何使用Spinner显示字符串日期列表



我想使用Spinner来使用observer显示房间数据库中的字符串日期列表,但日期没有出现在微调器中,即使我没有收到空引用消息的错误,我也不知道问题出在哪里,这是我的代码

public class DailyDataActivity extends AppCompatActivity {
private Spinner mSpinner;
private List<NumberOfNotification> listDate;
private NotificationSpinnerAdapter notificationSpinnerAdapter;
private TextView mTextView;
private LiveData<List<NumberOfNotification>> numberLiveData = new MutableLiveData<>();
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_daily_data);
mSpinner = findViewById(R.id.spinner);
mTextView = findViewById(R.id.textView4);
listDate = new ArrayList<>();
notificationSpinnerAdapter = new NotificationSpinnerAdapter(this, listDate);
mSpinner.setAdapter(notificationSpinnerAdapter);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
mTextView.setText(String.valueOf(listDate.get(position).getNumberOfNotificationOfToday()));
}
@Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
initList();
}
public void initList() {
final NotificationViewModel notificationViewModel = new ViewModelProvider(this).get(NotificationViewModel.class);
notificationViewModel.getAllDatesOfNumberOfNotification().observe(this, new Observer<List<NumberOfNotification>>() {
@Override
public void onChanged(List<NumberOfNotification> numberOfNotifications) {
for (NumberOfNotification n : numberOfNotifications) {
System.out.println(getClass().getSimpleName() + " " + numberOfNotifications.size());
listDate.clear();

listDate.addAll(numberOfNotifications);
notificationSpinnerAdapter.notifyDataSetChanged();
}
}
});
System.out.println("test date" + listDate);
}
}```
public void initList() {
final NotificationViewModel notificationViewModel = new ViewModelProvider(this).get(NotificationViewModel.class);
notificationViewModel.getAllDatesOfNumberOfNotification().observe(this, new Observer<List<NumberOfNotification>>() {
@Override
public void onChanged(List<NumberOfNotification> numberOfNotifications) {
listDate.clear();
for (NumberOfNotification n : numberOfNotifications) {
System.out.println(getClass().getSimpleName() + " " + numberOfNotifications.size());

listDate.addAll(numberOfNotifications);

}
}
});
notificationSpinnerAdapter.notifyDataSetChanged();
System.out.println("test date" + listDate);
}
}

最新更新