我试图在recyclerview中显示来自api响应的温度值。问题是我的程序经常只显示一个,第一项。我不确定问题出在哪里,我有点迷失了方向。
我想显示的部分api响应
"hourly": [
{
"dt": 1607184000,
"temp": x,
"feels_like": x,
"pressure": x,
"humidity": x,
"dew_point": x,
"uvi": x,
"clouds": x,
"visibility": x,
"wind_speed": x,
"wind_deg": x,
"pop": x
},
and so on...
模型类
public class ForecastModel {
@SerializedName("hourly")
private List<HourlyForecast> hourlyForecast = null;
public List<HourlyForecast> getHourlyForecast() {
return hourlyForecast;
}
public class HourlyForecast{
@SerializedName("dt")
private int dt;
public int getDt() {
return dt;
}
}
}
活动类
public class HourlyForecastActivity extends AppCompatActivity {
private static final String TAG = "HourlyForecastActivity";
private List<ForecastModel> mData = new ArrayList<>();
RecyclerView recyclerView;
HourlyForecastAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hourly_forecast);
recyclerView = findViewById(R.id.hourly_recycler_view);
adapter = new HourlyForecastAdapter();
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
recyclerView.setAdapter(adapter);
prepareConnection();
}
private void prepareConnection(){
ForecastViewModel viewModel = new ViewModelProvider(this).get(ForecastViewModel.class);
viewModel.getData().observe(this, new Observer<ForecastModel>() {
@Override
public void onChanged(ForecastModel forecast) {
if (mData.size() > 0){
mData.clear();
}
if (forecast != null){
mData.addAll(Collections.singleton(forecast));
adapter.setData(mData);
}
}
});
}
}
适配器类
public class HourlyForecastAdapter extends RecyclerView.Adapter<HourlyForecastAdapter.HourlyViewHolder> {
private List<ForecastModel> mHourlyData = new ArrayList<>();
public void setData(List<ForecastModel> mHourlyData){
this.mHourlyData = mHourlyData;
notifyDataSetChanged();
}
@NonNull
@Override
public HourlyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.hourly_items, parent, false);
return new HourlyViewHolder(view);
}
@Override
public void onBindViewHolder(@NonNull HourlyViewHolder holder, int position) {
ForecastModel model = mHourlyData.get(position);
holder.mTemperature.setText(String.valueOf(model.getHourlyForecast().get(position).getDt()));
}
@Override
public int getItemCount() {
return mHourlyData.size();
}
public class HourlyViewHolder extends RecyclerView.ViewHolder {
private TextView mTemperature, mPressure, mHumidity, mWindSpeed, mDescription;
private ImageView mIcon;
public HourlyViewHolder(@NonNull View itemView) {
super(itemView);
mTemperature = itemView.findViewById(R.id.hourly_temperature);
mPressure = itemView.findViewById(R.id.hourly_pressure);
mHumidity = itemView.findViewById(R.id.hourly_humidity);
mWindSpeed = itemView.findViewById(R.id.hourly_wind_speed);
mDescription = itemView.findViewById(R.id.hourly_description);
}
}
}
1条答案
按热度按时间5anewei61#
这个
setData
适配器中的方法有点误导(命名和变量方面)。看着你的onChanged
中的方法Activity
:单个ForecastModel
对象是您正在使用的对象。将该对象传递给中的适配器setData
作为一个List
命名mHourlyData
是什么导致了最初的混乱。那个List
将始终是大小1,导致你所面临的问题。我建议你换个房间
onChanged
方法调用以简单地执行此操作:并在中重命名适配器变量
setData
要反映您实际收到(想要收到)的内容:在那之后,唯一要做的就是更新你的
getItemCount
以及onBindViewHolder
方法。