【发布时间】:2020-10-13 05:31:22
【问题描述】:
想要实现:当用户输入“天气”一词时,聊天机器人将天气预报作为该聊天项目(位于外部 RecyclerView 中)内的 RecyclerView 返回。
我希望天气信息成为实际的聊天项目。
我有 MessageAdapter 和 WeatherAdapter 分别处理消息和天气预报信息,我很难处理 MessageAdapter 的 onBindViewHolder 中的“返回天气预报”情况。
我尝试了许多不同的方法都无济于事: making a ConcatAdapter, making a custom adapter, 制作一个片段,从我进行天气获取的主要活动接收天气数据和showing that fragment in adapter...
java.lang.NullPointerException:尝试调用虚拟方法 'java.io.Serializable android.os.Bundle.getSerializable(java.lang.String)' 在空对象上 参考 在 com.demo.chatbot.WeatherFragment.onCreateView(WeatherFragment.java:25)
weatherList = (ArrayList<Weather>) getArguments().getSerializable("weather"); (WeatherFragment.java:25)
数据获取尚未开始,然后片段正在尝试加载数据...
主要问题是我必须在获取 API 后设置weatherAdapter.setWeatherList(weatherList.getWeeklyWeather());,这必须在我的主要活动中完成,并且当布局应该在 MessageAdapter 中完成时,必须在主要活动中初始化 RecyclerViews。
rvWeather = findViewById(R.id.rvWeather);
// rvWeather is supposed to be inside my item_chat_bot.xml
// but I have to add it to main activity for it to show
rvWeather.setAdapter(weatherAdapter);
// otherwise get No adapter attached; skipping layout
我不明白为什么这么复杂。在 react native 中,只需使用 .map 或 for 循环即可。我错过了什么明显的东西吗?
【问题讨论】:
-
如果您的列表有数据,则将列表实例传递给另一个 RecyclerView 并在另一个 RecyclerView 的适配器实例上调用 notifyDataSetChanged
标签: java android android-recyclerview android-adapter