How can I change ArrayAdapter to global and clear it in another class?


I have a ArrayAdapter declaration. I want to clear it and add some other data. So how can I change it to global?

public class ForecastFragment extends Fragment { private ArrayAdapter<String> mForecastArray; // ... List<String> weekForecast = new ArrayList<String>(Arrays.asList(forecastArray)); mForecastArray = new ArrayAdapter<String>(getActivity(), R.layout.list_item_forecast, R.id.list_item_forecast_textview, weekForecast); ListView listView = (ListView) rootView.findViewById(R.id.listview_forecast); listView.setAdapter(mForecastArray); }

Another class

public class FetchWeatherTask extends AsyncTask<String, Void, String[]>{ protected void onPostExecute(String[] strings) { if (strings!=null){ mForecastArray.clear(); for (String dayForecast : strings){ mForecastArray.add(dayForecast); } } }

I couldn't use mForecastArray in the second class


Global variables are always error prone and create issues in the long run.

You can have a listener defined in your AsyncTask that would have say a method updateAdapter and this listener would be implemented by your Fragment ForecastFragment. So once you you have

public class FetchWeatherTask extends AsyncTask<String, Void, String[]>{ FetchListener listener; public FetchWeatherTask(FetchListener listener) { this.listener = listener; } public interface FetchListener { public void updateAdapter(String[] arr); } protected void onPostExecute(String[] strings) { //if (strings!=null){ // mForecastArray.clear(); //for (String dayForecast : strings){ //mForecastArray.add(dayForecast); //} // just call your listener's update method listener.updateAdapter(strings); } }

<strong>Inside ForecastFragment:</strong>

public class ForecastFragment extends Fragment implements FetchWeatherTask.FetchListener { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // say if you are starting your AsyncTask here new FetchWeatherTask(this).execute(); } public void updateAdapter(String[] arr) { if (arr!=null){ mForecastArray.clear(); // use arr to re populate your mForecastArray } }


create your custom application refer this <a href="http://developer.android.com/reference/android/app/Application.html" rel="nofollow">link</a>

create static ArrayList in onCreate of your custom application. You can access that ArrayList anywhere in application.


  • Retrofit in android?
  • Facebook Share from my app
  • Stream.peek() method in Java 8 vs Java 9
  • Java/MongoDB - How to resolve error “exception: pipeline element 0 is not an object”
  • How to re-request permissions in Facebook SDK 4.0
  • Java shuffle card deck
  • How can I change ArrayAdapter to global and clear it in another class?
  • How to register many object as beans in Spring Java Config?
  • Generate random index different then any one given in a set
  • MongoDB/Java - How implement $out in java for mongodb
  • How to check whether an int is within a range of values
  • adapter holder on listener cannot be final
  • Why does android:label's text not changing when user changes language within my app?
  • Google Sheets API v4 Appending data getting bad request 400 error
  • Connect multiple arraylist to one single array list
  • Android layout , does it support orientation layout xml files? Does -land work at all?
  • JUnit assertions within in Java 8 stream
  • Morphia projection behavior for instance variable with default value
  • Access Token Removed when posting facebook sdk 4.0 in Android
  • Fragment tabhost is not working in Fragment
  • How to make Observable that emit character after 1 second of time interval
  • Display Two Fragments at the same time
  • Display JSON data on Android using GSON and Volley
  • Force Down error in MapView
  • Return list instead of an item Webservice Phone 8.1 UAP
  • media player does not work in my code
  • Drawing a polygon over the entire map
  • Android Google Maps: Polygon or shape that hides entire map
  • Get the clicked item in android list
  • How to use getFilter form Listadapter
  • Binding a list with another list with WPF
  • Fancy dynamic list in Android: TableLayout vs ListView
  • React Native fetch is not a function
  • UWP change ComboBox position when opening
  • WPF version of .ScaleControl?
  • jquery mobile loadPage not working
  • How to delete a row from a dynamic generate table using jquery?
  • json Serialization in asp
  • Rails 2: use form_for to build a form covering multiple objects of the same class
  • How to stop GridView from loading again when I press back button?