我的適配器的Android再循環視圖不工作
public class DepartmentsAdapter extends RecyclerView.Adapter<DepartmentsAdapter.ViewHolder>{
// Store a member variable for the contacts
private List<AppDepartments> mDepartments;
// Store the context for easy access
private Context mContext;
@Override
public DepartmentsAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
// Inflate the custom layout
View contactView = inflater.inflate(R.layout.app_department_item, parent, false);
// Return a new holder instance
ViewHolder viewHolder = new ViewHolder(contactView);
return viewHolder;
}
// Involves populating data into the item through holder
@Override
public void onBindViewHolder(DepartmentsAdapter.ViewHolder holder, int position) {
AppDepartments appDepartments = mDepartments.get(position);
// Set item views based on your views and data model
/* holder.txtViewName.setText(appDepartments.getDeptName());
holder.txtViewUser.setText(appDepartments.getDeptUser());*/
holder.txtViewName.setText("HELLO");
holder.txtViewUser.setText("ITS VICTOR");
}
@Override
public long getItemId(int i) {
return 0;
}
@Override
public int getItemCount() {
return 0;
}
public class DiscoverFragment extends Fragment {
public DiscoverFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_discover, container, false);
departmentsAdapter = new DepartmentsAdapter(getActivity(),departList);
// Lookup the recyclerview in activity layout
RecyclerView displayDeparts = (RecyclerView) rootView.findViewById(R.id.appDepartments);
displayDeparts.setAdapter(departmentsAdapter);
displayDeparts.setLayoutManager(new LinearLayoutManager(getActivity()));
// Inflate the layout for this fragment
return rootView;
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
}
@Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
}
上面的代碼返回空白屏幕,不顯示我的片段的textviews 。請幫助。 我已經評論了模型,以便我可以用硬編碼文本進行測試。 我下面這篇文章http://guides.codepath.com/android/Using-the-RecyclerView
不要忘記填寫 – hakim
您還沒有添加任何''departList值值到departList並且在Adapter的getItemCount中返回departList.size()而不是0 –