简体   繁体   中英

How do I move Objects from an ArrayList to a RecyclerView in another Class?

I am parsing data using a DOM Parser and storing the data in an ArrayList as Objects.

MaintenancePlanner.java

  Weather weather = new Weather();
  public ArrayList<Weather> weathers = new ArrayList<Weather>();



  weathers.add(new Weather(weathr, time, degrees));

I want to display these objects in a recyclerview in another class-

WeatherFragment.java

 MaintenancePlanner mp;

 private void readWeatherObjects() {

        for (Weather weather : mp.weathers) {

                mWeathers.add(weather);

        }

        weatherAdapter = new WeatherAdapter(getContext(), mWeathers);
        recyclerView.setAdapter(weatherAdapter);



    }

There are two cases,

One you can put your list of data at an activity level, so all fragments of the attached activity will able to access this.

Other way is passing by value, using Bundle you can pass a list of data to next screens as ArrayList has already Serializable implementation, Just you need to serialize your model.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM