目前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); //更新