3

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); } } } 
2
  • 3
    why are you returning 0 for getItemCount? you should return the size of that list used in adapter Commented Jan 29, 2015 at 22:16
  • stackoverflow.com/a/33584826/1318946 Commented Nov 7, 2015 at 16:13

1 Answer 1

7

Change getItemCount() to actually return the correct value:

@Override public int getItemCount() { return data.size() } 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, I don't know how I overlooked that! But now my app is crashing...I'll post the stack above.
Never mind, I realized what the error with that was. Thanks for your help

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.