Skip to content
Advertisement

How to show a RecyclerView (with data fetched from API) as a list item in another RecyclerView in android?

Want to achieve: a chatbot that returns weather forecast as a RecyclerView inside that chat item (which is in the outer RecyclerView) when the user inputs the word ‘weather’.

I want the weather info to be the actual chat item.

I have MessageAdapter and WeatherAdapter which deal with messages and weather forecast info respectively and I have difficulty handling the ‘returning weather forecast’ case in onBindViewHolder of MessageAdapter.

I have tried many different ways to no avail: making a ConcatAdapter, making a custom adapter, making a fragment that receives the weather data from the main activity where I do the weather fetching and showing that fragment in adapter

java.lang.NullPointerException: Attempt to invoke virtual method ‘java.io.Serializable android.os.Bundle.getSerializable(java.lang.String)’ on a null object reference at com.demo.chatbot.WeatherFragment.onCreateView(WeatherFragment.java:25)

JavaScript

Data fetching hasn’t started and then the fragment is trying to load the data…

The main problem is I have to set weatherAdapter.setWeatherList(weatherList.getWeeklyWeather()); after fetching API and this has to be done in my main activity and the RecyclerViews have to be initialized in main activity when the layout should be done in MessageAdapter.

JavaScript

I don’t understand why it’s so complicated. In react native, it’s just a matter of using .map or a for loop. Am I missing something obvious?

Advertisement

Answer

weather recyclerview

The point is to have the weather forecast be one message. Use two for loops (one in WeatherViewHolder and one in onBindViewHolder) and TextView arrays in adapter to handle the weather layout. Here are my codes for the adapter:

JavaScript
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement