2015-01-29 5 views
3

일부 데이터에 대해 구문 분석을 쿼리 한 후 일부가 리사이클 러 뷰를 업데이트하려고했습니다.RecyclerView notifyDataSetChanged가 구문 쿼리 후 작동하지 않습니다.

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을 반환합니까? 당신은 어댑터에서 사용되는 그리스트의 크기를 반환해야한다 –

+0

http://stackoverflow.com/a/33584826/1318946 –

답변

7

변경 getItemCount() 실제로 올바른 값 반환 :

done 내가 notifyDataSetChagned를 호출 쿼리의 방법 만에서 목록

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

recyclerViewAdapter은 다음과 같습니다 표시되지 않습니다

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

감사합니다, 어떻게 간과했는지 모르겠다! 하지만 지금은 내 응용 프로그램이 충돌 ... 나는 스택을 게시 할 것입니다. – ENG618

+0

신경 쓰지 마라. 나는 그 오류가 무엇인지 깨달았다. 도움 주셔서 감사합니다 – ENG618

+0

당신은 환영합니다, 행운을 빌어 요! –

관련 문제