[英]I've received single item from api
I trying to display temperature values that came from the API response in RecyclerView.我试图在 RecyclerView 中显示来自 API 响应的温度值。 The thing is my program constantly display just one, first item.
问题是我的程序经常只显示一个,第一项。 I'm not sure where can be the issue, and I feel a bit lost in it.
我不确定问题出在哪里,我觉得有点迷茫。
Part of the api response that I want to display我要显示的 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...
Model class Model class
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;
}
}
} }
Activity class活动 class
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);
}
}
});
}
}
Adapter class适配器 class
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);
}
}
}
The setData
method in your adapter is a bit misleading (naming and variable wise).适配器中的
setData
方法有点误导(命名和变量明智)。 Looking at your onChanged
method in the Activity
: a single ForecastModel
object is what you are working with.查看
Activity
中的onChanged
方法:您正在使用单个ForecastModel
object。 Passing that object to the adapter in setData
as a List
named mHourlyData
is what's causing the initial confusion.将 object 作为名为
mHourlyData
的List
传递给setData
中的适配器是导致最初混乱的原因。 That List
will alway be of size 1, causing the problem you are facing.该
List
的大小始终为 1,从而导致您面临的问题。
I recommend changing the onChanged
method call to simply do this:我建议更改
onChanged
方法调用以简单地执行此操作:
adapter.setData(forecast)
And rename the adapter variable in setData
to reflect what your actually are receiving (want to receive):并重命名
setData
中的适配器变量以反映您实际接收的内容(想要接收):
private List<HourlyForecast> mHourlyForecast = new ArrayList<>();
public void setData(ForecastModel forecast){
this.mHourlyForecast = forecast.hourlyForecast;
notifyDataSetChanged();
}
After that, the only thing left to do is update your getItemCount
and onBindViewHolder
methods accordingly.之后,唯一要做的就是相应地更新您的
getItemCount
和onBindViewHolder
方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.