RecycleView接收okhttp请求到的数据后进行更新

目前RecycleView需要接收一个List<Hours> hours;类型的列表数据
在Adapter类中写一个updata方法
注意:notifyDataSetChanged();是更新整个recycleview的数据

  public void update(List<Hours> hours) {
        mHours.clear();
        mHours.addAll(hours);
        notifyDataSetChanged();
    }

在activity中 在处理接收数据的方法中调用adapter的updata方法即可进行更新数据

class MyHandler extends Handler {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            String data = (String) msg.obj;
            Gson gson = new Gson();
            WeatherData weatherData = gson.fromJson(data, WeatherData.class);
            mTvCity.setText(weatherData.city);
            mTvTem.setText(weatherData.tem);
            mTvTem1.setText(weatherData.tem1);
            mTvTem2.setText(weatherData.tem2);
            mTvWea.setText(weatherData.wea);
            mTvAir.setText(weatherData.air_level);
            mTvCountry.setText(weatherData.country);
            mTvDate.setText(weatherData.date);
            hours = weatherData.hours;
            realTimeTemperatureAdapter.updata(hours);

        }
    }

向指定位置插入Item
public final void notifyItemInserted(int position)
移除指定位置Item
public final void notifyItemRemoved(int position)
更新指定位置Item
public final void notifyItemChanged(int position)

为adapter中添加两个方法:
public void addData(int position) {
mDatas.add(position, “Insert One”);
notifyItemInserted(position); // 删除
}

public void removeData(int position) {
mDatas.remove(position);
notifyItemRemoved(position); //插入
}
notifyItemChanged(position); //更新

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值