分析查询后RecyclerView notifyDataSetChanged不工作 [英] RecyclerView notifyDataSetChanged not working after Parse query
本文介绍了分析查询后RecyclerView notifyDataSetChanged不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我在尝试更新了我的看法回收片段,解析查询一些数据之后。在查询我称之为 notifyDataSetChagned
的完成
方法,但该列表永远不会显示
包com.garciaericn.t2d.fragments;
公共类DevicesCardViewFragment扩展片段实现View.OnClickListener { 私人OnFragmentInteractionListener mListener;
私人BatteryHelper mBatteryHelper;
意图mBatteryStatus; 私人RecyclerView mRecyclerView;
私人DeviceAdapter mAdapter;
私人RecyclerView.LayoutManager mLayoutManager;
私人列表<装置> mDevices; 公共DevicesCardViewFragment(){
//必需的空公共构造
mDevices =新的ArrayList<装置>();
} 公共静态的newInstance DevicesCardViewFragment(){
//捆绑参数是必要的 返回新DevicesCardViewFragment();
} @覆盖
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState);
//获取参数
getDevices(); mBatteryHelper =新BatteryHelper(getActivity()); IntentFilter的IntentFilter的=新的IntentFilter(Intent.ACTION_BATTERY_CHANGED);
mBatteryStatus = getActivity()registerReceiver(NULL,IntentFilter的)。 //当前设备的更新统计信息。 Toast.makeText(getActivity(),电池电量:+ mBatteryHelper.getCurrentBatteryLevel()+%,Toast.LENGTH_LONG).show(); //更新设备统计 } @覆盖
公共查看onCreateView(LayoutInflater充气器,容器的ViewGroup,捆绑savedInstanceState){
查看查看= inflater.inflate(R.layout.fragment_devices_list,集装箱,FALSE); mListener.showAd(); //获取回收视图
mRecyclerView =(RecyclerView)view.findViewById(R.id.devices_recycler_view);
mRecyclerView.setHasFixedSize(真); mAdapter =新DeviceAdapter(getActivity(),mDevices); //设置适配器
mRecyclerView.setAdapter(mAdapter); //设置布局管理器
mLayoutManager =新LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
返回视图。
} @覆盖
公共无效onAttach(活动活动){
super.onAttach(活动);
尝试{
mListener =(OnFragmentInteractionListener)活性;
}赶上(抛出ClassCastException E){
抛出新ClassCastException异常(activity.toString()
+必须实现OnFragmentInteractionListener);
}
} @覆盖
公共无效onDetach(){
super.onDetach();
mListener = NULL;
} 公开名单<装置> getDevices(){ ParseQuery&所述;装置>查询= ParseQuery.getQuery(Device.DEVICES);
//新ParseQuery<装置>(Device.DEVICES);
query.whereEqualTo(deviceUser,ParseUser.getCurrentUser());
query.findInBackground(新FindCallback&所述;装置>(){
@覆盖
公共无效完成(列表<装置>装置,ParseException的E){
如果(E == NULL){
//循环回装置
对于(设备设备:设备){
设备currentDevice =新的设备();
currentDevice.setDeviceName(device.getDeviceName());
currentDevice.setBatteryLevel(device.getBatteryLevel());
currentDevice.setIsCharging(device.isCharging());
mDevices.add(currentDevice);
}
mAdapter.notifyDataSetChanged();
}其他{
// 出了些问题
Toast.makeText(getActivity(),错误:+ e.toString(),Toast.LENGTH_LONG).show();
}
}
}); 返回mDevices;
} @覆盖
公共无效的onClick(视图v){
// Click事件何去何从
} 公共静态的newInstance DevicesCardViewFragment(列表<装置> mDevices){
DevicesCardViewFragment片段=新DevicesCardViewFragment(); 捆绑ARGS =新包(); 返回片段;
} 公共接口OnFragmentInteractionListener {
公共无效showAd();
}
}
我recyclerViewAdapter看起来像这样...
包com.garciaericn.t2d.data;公共类DeviceAdapter扩展RecyclerView.Adapter< DeviceAdapter.MyViewHolder> { 私人最终LayoutInflater吹气; 清单<装置>数据= Collections.emptyList(); 公共DeviceAdapter(上下文的背景下,列表与LT;装置>数据){
充气= LayoutInflater.from(上下文);
this.data =数据;
} @覆盖 公共MyViewHolder onCreateViewHolder(ViewGroup中的父母,INT viewType){
查看查看= inflater.inflate(R.layout.card_layout,父母,假); MyViewHolder持有人=新MyViewHolder(视图); 回到持有人;
} @覆盖
公共无效onBindViewHolder(MyViewHolder持有人,INT位置){ 设备device = data.get(位置); holder.deviceNameTV.setText(device.getDeviceName());
holder.batteryLevelTV.setText(device.getBatteryLevel());
} @覆盖
公众诠释getItemCount(){
返回0;
} 公共静态类MyViewHolder扩展RecyclerView.ViewHolder {
//临时数据集
私有String [] mDataset; TextView的deviceNameTV;
TextView的batteryLevelTV; 公共MyViewHolder(查看ItemView控件){
超(ItemView控件);
deviceNameTV =(TextView中)itemView.findViewById(R.id.device_name_tv);
batteryLevelTV =(TextView中)itemView.findViewById(R.id.battery_level_tv);
}
}
}
解决方案
修改 getItemCount()
实际返回正确的值:
@覆盖
公众诠释getItemCount(){
返回data.size()
}
I'm in a fragment trying to updated my recycler view, after querying Parse for some data. In the done
method of the query I call notifyDataSetChagned
, but the list is never displayed
package com.garciaericn.t2d.fragments;
public class DevicesCardViewFragment extends Fragment implements View.OnClickListener {
private OnFragmentInteractionListener mListener;
private BatteryHelper mBatteryHelper;
Intent mBatteryStatus;
private RecyclerView mRecyclerView;
private DeviceAdapter mAdapter;
private RecyclerView.LayoutManager mLayoutManager;
private List<Device> mDevices;
public DevicesCardViewFragment() {
// Required empty public constructor
mDevices = new ArrayList<Device>();
}
public static DevicesCardViewFragment newInstance() {
// Bundle parameters is necessary
return new DevicesCardViewFragment();
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get arguments
getDevices();
mBatteryHelper = new BatteryHelper(getActivity());
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
mBatteryStatus = getActivity().registerReceiver(null, intentFilter);
// Update stats of current device.
Toast.makeText(getActivity(), "Battery level: " + mBatteryHelper.getCurrentBatteryLevel() + "%", Toast.LENGTH_LONG).show();
// Update device stats
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_devices_list, container, false);
mListener.showAd();
// Obtain recycler view
mRecyclerView = (RecyclerView) view.findViewById(R.id.devices_recycler_view);
mRecyclerView.setHasFixedSize(true);
mAdapter = new DeviceAdapter(getActivity(), mDevices);
// Set adapter
mRecyclerView.setAdapter(mAdapter);
// Set layout manager
mLayoutManager = new LinearLayoutManager(getActivity());
mRecyclerView.setLayoutManager(mLayoutManager);
return view;
}
@Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public List<Device> getDevices() {
ParseQuery<Device> query = ParseQuery.getQuery(Device.DEVICES);
// new ParseQuery<Device>(Device.DEVICES);
query.whereEqualTo("deviceUser", ParseUser.getCurrentUser());
query.findInBackground(new FindCallback<Device>() {
@Override
public void done(List<Device> devices, ParseException e) {
if (e == null) {
// Loop through return devices
for (Device device : devices) {
Device currentDevice = new Device();
currentDevice.setDeviceName(device.getDeviceName());
currentDevice.setBatteryLevel(device.getBatteryLevel());
currentDevice.setIsCharging(device.isCharging());
mDevices.add(currentDevice);
}
mAdapter.notifyDataSetChanged();
} else {
// Something went wrong
Toast.makeText(getActivity(), "Error: " + e.toString(), Toast.LENGTH_LONG).show();
}
}
});
return mDevices;
}
@Override
public void onClick(View v) {
// Click events go here
}
public static DevicesCardViewFragment newInstance(List<Device> mDevices) {
DevicesCardViewFragment fragment = new DevicesCardViewFragment();
Bundle args = new Bundle();
return fragment;
}
public interface OnFragmentInteractionListener {
public void showAd();
}
}
My recyclerViewAdapter looks like this...
package com.garciaericn.t2d.data;
public class DeviceAdapter extends RecyclerView.Adapter<DeviceAdapter.MyViewHolder> {
private final LayoutInflater inflater;
List<Device> data = Collections.emptyList();
public DeviceAdapter(Context context, List<Device> data) {
inflater = LayoutInflater.from(context);
this.data = data;
}
@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = inflater.inflate(R.layout.card_layout, parent, false);
MyViewHolder holder = new MyViewHolder(view);
return holder;
}
@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Device device = data.get(position);
holder.deviceNameTV.setText(device.getDeviceName());
holder.batteryLevelTV.setText(device.getBatteryLevel());
}
@Override
public int getItemCount() {
return 0;
}
public static class MyViewHolder extends RecyclerView.ViewHolder {
// Temp data set
private String[] mDataset;
TextView deviceNameTV;
TextView batteryLevelTV;
public MyViewHolder(View itemView) {
super(itemView);
deviceNameTV = (TextView) itemView.findViewById(R.id.device_name_tv);
batteryLevelTV = (TextView) itemView.findViewById(R.id.battery_level_tv);
}
}
}
解决方案
Change getItemCount()
to actually return the correct value:
@Override
public int getItemCount() {
return data.size()
}
这篇关于分析查询后RecyclerView notifyDataSetChanged不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文