RecyclerView Parse查询后 notifyDataSetChanged 不起作用

3

我正在一个片段中尝试更新我的可回收视图,在查询某些数据后。在查询的done方法中,我调用了notifyDataSetChanged,但列表从未显示。

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();
    }
}

我的RecyclerView适配器如下所示:

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);
        }
    }
}

3
为什么你将 getItemCount 方法返回 0?你应该返回适配器中使用的列表的大小。 - Cristian Olaru
https://dev59.com/M2Af5IYBdhLWcg3wXhoJ#33584826 - Pratik Butani
1个回答

7

getItemCount()更改为实际返回正确的值:

@Override
public int getItemCount() {
    return data.size()
}

谢谢,我不知道我怎么会忽略那个!但现在我的应用程序崩溃了...我会在上面发布堆栈。 - ENG618
没关系,我意识到那个错误了。谢谢你的帮助。 - ENG618

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接