简体   繁体   English

我收到了来自 api 的单件商品

[英]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 作为名为mHourlyDataList传递给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.之后,唯一要做的就是相应地更新您的getItemCountonBindViewHolder方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我试图在jmeter中打开一个jmx文件并收到此错误 - I've tried to open a jmx file in jmeter and received this error 我已经收到我的第一个例外。 现在我该怎么办? - I've received my first Exception. Now what do I do? 如何解析(然后找到特定的)从API请求接收的XML数据? - How do I parse(and then find particular) XML data received from an API request? 当我将其从http请求发送到rest api时,接收到的xml内容已更改 - Received xml content is changed, when I send it to rest api from http request 无法从1个单项中减去? - Cannot subtract from 1 single item? 如何从收藏中获取特定项目? 标准 API - How can I get specific item from collection? Criteria API 我需要从菜单项访问Google Maps API标记 - I need to access a Google Maps API Marker from a menu item 编译器没有认识到我已经从它实现的接口中实现了 compareTo() 方法 - Compiler not Recognizing that I've implemented the compareTo() method from the Interface it Implements 从商品名称查找API查找商品 - find item from item name lookup api 如何使用 Java 中的按钮(标签)从 Arraylist 中删除单个项目 - How do I remove a single item from an Arraylist with a button (a tag) in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM