Search code examples
javascriptandroidandroid-studiolistviewanonymous

ListView problem "cannot resolve constructor arrayadapter anonymous"


I got error on this line:

arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, allOnDay);

The Error is:

cannot resolve constructor arrayadapter anonymous...

I have this code in the onCreate:

ArrayList<String> allOnDay = new ArrayList<>();
cal_ListView = findViewById(R.id.cal_ListView);
for (int i = 0; i < data.size(); i++) {
   if (values.get(i).equals(year+"")) {
      if (keys.get(i).getMonth()== month +1 && keys.get(i).getDay() == dayOfMonth) {
         allOnDay.add(data.get(i));
      }
   }
}
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, allOnDay);
cal_ListView.setAdapter(arrayAdapter);

Any Suggestions how I can correct this?


Solution

  • Probably you try to initialize ArrayAdapter other than Activity context. In that case use getApplicationContext() or getContext() or getActivity() instead of this.

    Use

    arrayAdapter = new ArrayAdapter<String>(getContext(), android.R.layout.simple_list_item_1, allOnDay);
    

    Instead of

    arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, allOnDay);