Adapter onCreateViewHolder and onBindViewHolder methods are not getting called in RecyclerView?

I am using recyclerview adapter for my fragment, but my list is not getting shown as the onCreateViewHolder() and onBindViewHolder() are not getting called. Please let me know what is the issue with my code?

MyFragment code :

public class MyFragment extends Fragment {

private Integer mCurrentPage = 1;
private Integer mChosenOrder=0;
ArrayList<MyParcelableObject> mMyList;
private RecyclerView mRecyclerView;
MyAdapter mMyAdapter;

public MyFragment() {
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View rootView = inflater.inflate(R.layout.fragment_main, container, false);
     mRecyclerView= (RecyclerView) rootView.findViewById(R.id.gridview_movies);


    mRecyclerView.setLayoutManager(new GridLayoutManager(getContext(), 2));

    Log.e(LOG_TAG, "In oncreateview");
    if (savedInstanceState != null && savedInstanceState.getParcelableArrayList(ConstantUtil.My_LIST_KEY) != null) {
        mMyList = savedInstanceState.getParcelableArrayList(ConstantUtil.My_LIST_KEY);
    } else {
        mMyList = new ArrayList<>();

    }
  new MyTask(getActivity(), mMyList,mMyAdapter).execute(mChosenOrder);
    mMyAdapter = new MyAdapter(getActivity(),mMyList);
    Log.e(LOG_TAG,"Adapter size oncreateview"+mMyAdapter.getItemCount());
       mRecyclerView.setAdapter(mMyAdapter);

    Log.e(LOG_TAG, "In oncreateview after attaching adapter");


    return rootView;

}

@Override
public void onStart() {
    super.onStart();
    populate();

}


private void populate() {


    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(getActivity());
    if (prefs != null) {
        String order = prefs.getString(getString(R.string.sorting_order), getString(R.string.pref_defaultValue));
        int order_value = Integer.parseInt(order);
        if (order_value >= 0) {
            Resources resources = getResources();
            mChosenOrder = Integer.parseInt(resources.getStringArray(R.array.pref_sorting_values)[order_value]);
        } else {
            mChosenOrder = order_value;
        }
    } else {
        mChosenOrder = Integer.parseInt(getString(R.string.pref_defaultValue));
    }

   new MyTask(getActivity(),mMyList,mMyAdapter).execute(mChosenOrder);

    Log.e(LOG_TAG,"populate Adapter size "+mMyAdapter.getItemCount());
    mRecyclerView.setAdapter(mMyAdapter);



}


@Override
public void onSaveInstanceState(Bundle outState) {

    super.onSaveInstanceState(outState);
    outState.putParcelableArrayList(ConstantUtil.My_LIST_KEY, mMyList);
}
}

Async Task code

public class MyTask extends AsyncTask<Integer, Void, MyParcelableObject[]> {

private Context context;
private List<MyParcelableObject> mMyParcelableObjects;
private RecyclerView recyclerView;
MyAdapter myAdapter;

public MyTask(Context context, List<MyParcelableObject> myParcelableObjects,MyAdapter myAdapter) {
    this.context = context;
    mMyParcelableObjects = myParcelableObjects;
    this.myAdapter = myAdapter;
    //this.imageAdapter = imageAdapter;
    this.recyclerView = recyclerView;
}


@Override
protected MyParcelableObject[] doInBackground(Integer... params) {
    HttpURLConnection urlConnection = null;
    BufferedReader reader = null;
    String myStr[] = null;
    // Will contain the raw JSON response as a string.
    String myStrJsonStr = null;
    Uri buildUri = null;
    MyParcelableObject[] myParcelableObjects = null;
    //try {
    //  Context context = getApplicationContext();
ArrayList<MyParcelableObject> myParcelableObjectArrayList = null;
    String[] sortOrder = context.getResources().getStringArray(R.array.pref_sorting_values);
    int sort = Integer.parseInt(sortOrder[0]);


    myParcelableObjectArrayList = getJsonFromUri(params[0]);        //correctly gets the json array
    if (myParcelableObjectArrayList != null) {

            myParcelableObjects = myParcelableObjectArrayList.toArray(new MyParcelableObject[myParcelableObjectArrayList.size()]);
            return myParcelableObjects;

    return null;
}



/**
 * @param results
 */
@Override
protected void onPostExecute(MyParcelableObject[] results) {
    Log.e(LOG_TAG, "In onPostExecute");

    if (results != null) {
        mMyParcelableObjects = Arrays.asList(results);
        myAdapter = new MyAdapter(context,mMyParcelableObjects);

        Log.e(LOG_TAG,"adapter size"+myAdapter.getItemCount());
        myAdapter.notifyDataSetChanged();

    }
}

Adapter code

public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ViewHolder> {

List<MyParcelableObject> mParcelableObjects;
ViewHolder mViewHolder;
Context mContext;

public MyAdapter(Context context, List<MyParcelableObject> parcelableObjects) {
    mParcelableObjects = parcelableObjects;
    mContext=context;
}



public static class ViewHolder extends RecyclerView.ViewHolder {
    public ImageView mImageView;

    public ViewHolder(View view) {
        super(view);
        mImageView = (ImageView) view.findViewById(R.id.movie_content_imageview);

    }
}

@Override
public MyAdapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {

    Log.e("LOG_TAG","in on onCreateViewHolder");
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.content_main, parent, false);

    ViewHolder vh = new ViewHolder(v);
    return vh;
}

@Override
public void onBindViewHolder(MyAdapter.ViewHolder holder, int position) {
    Log.e("LOG_TAG","in on onBindViewHolder");
    String myPoster = null;
    MyParcelableObject myParcelableObject = mParcelableObjects.get(position);
    if (myParcelableObject.poster_path != null) {
        myPoster = myParcelableObject.poster_path.replaceAll("/", "");
    }
    Uri uri = Uri.parse(ConstantUtil.POSTER_URL).buildUpon().
            appendPath(ConstantUtil.W342_SIZE).
            appendPath(myPoster).build();
    Picasso.with(mContext).load(uri).placeholder(R.drawable.resource_notfound).error(R.drawable.resource_notfound).into(mViewHolder.mImageView);
}

@Override
public int getItemCount() {

    return mParcelableObjects.size();
}
}

Answers


You are not getting value because your list is empty. replace this

new MyTask(getActivity(), mMyList,mMyAdapter).execute(mChosenOrder);

to this

mMyList  = new MyTask(getActivity(), mMyList,mMyAdapter).execute(mChosenOrder).get();

Need Your Help

PHP - Parse an array string into multiple array

php arrays algorithm parsing

i'm starting learning php. I have an array :

Why does my Sax Parser produce no results after using InputStream Read?

java android sax httpurlconnection

I have this piece of code which I'm hoping will be able to tell me how much data I have downloaded (and soon put it in a progress bar), and then parse the results through my Sax Parser. If I commen...