在RecyclerView中没有调用适配器onCreateViewHolder和onBindViewHolder方法?

时间:2015-11-30 14:02:42

标签: java android android-fragments android-recyclerview

我正在使用recyclerview适配器作为我的片段,但我的列表没有显示,因为onCreateViewHolder()onBindViewHolder()没有被调用。请告诉我代码的问题是什么?

MyFragment代码

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

异步任务代码

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

    }
}

适配器代码

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

1 个答案:

答案 0 :(得分:3)

您没有获得价值,因为您的清单是空的。 替换这个

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

到这个

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