更新适配器数据后,RecyclerView 未显示任何内容 [英] RecyclerView not showing anything after updating Adapter data

查看:27
本文介绍了更新适配器数据后,RecyclerView 未显示任何内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

伙计们,我在这件事上需要帮助我已经通过我的代码几天来弄清楚出了什么问题,但我无法弄清楚.我有一个带有 RecyclerView 的片段,我使用图像占位符和一些默认文本初始化适配器数据,片段按预期显示它们,然后使用加载程序从 Internet 获取数据并解析并将新数据传递给适配器(所有这种情况根据需要发生)直到数据到达适配器然后 Taaadaaaa 初始数据消失并且新数据实际上没有显示片段显示一个空白屏幕肯定我做错了什么请告知.这是片段代码

guys i need help with this matter i have been going through my code for a couple of days to figure whats going wrong but i couldn't figure it out. I have a fragment with a RecyclerView i initialize the Adapter data with an image place holder and some default text and the fragment shows them as expected then using a loader i fetch data from the internet and parse and pass the new data to the Adapter (all of this happens as required) until the data reaches the Adapter then Taaadaaaa the initial data disappears and the new data is not showing actually the fragment shows a blank screen definitely i am doing something wrong please advise. This is the fragment code

public class fragment_MovieStartGridlayout extends android.support.v4.app.Fragment implements MyGridAdapter.MyGridAdapterListener{

private static RecyclerView myRecyclerView;
private static MyGridAdapter myGridAdapter;
private static RecyclerView.LayoutManager rvLayoutManager;
private LoaderManager.LoaderCallbacks<ArrayList<HashMap>> dataLoaderCallbacks;


private OnFragmentInteractionListener mListener;

public fragment_MovieStartGridlayout() {

}

@Override
public void onActivityCreated  (Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

  dataLoaderCallbacks = new LoaderManager.LoaderCallbacks<ArrayList<HashMap>>() {

  @Override
  public Loader<ArrayList<HashMap>> onCreateLoader(int id, Bundle args) {
   return new DataLoader(getActivityContext(), MyUriBuilder.DISCOVER, null);
            }

@Override
public void onLoadFinished(Loader<ArrayList<HashMap>> loader,ArrayList<HashMap> data) {
                myGridAdapter.setMyAdapterData(data);
            }

            @Override
            public void onLoaderReset(Loader<ArrayList<HashMap>> loader) {
                loader.reset();
            }

        };
        getLoaderManager().initLoader(0, null, dataLoaderCallbacks);

    setRetainInstance(true);
}

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

    View fragGridLayout = inflater.inflate(R.layout.fragment_gridlayout_movie_start, container, false);
    myRecyclerView = (RecyclerView) fragGridLayout.findViewById(R.id.myViewRecycler);
    rvLayoutManager = new GridLayoutManager(getActivityContext(),2);
    myRecyclerView.setLayoutManager(rvLayoutManager);
    myGridAdapter = new MyGridAdapter(getActivityContext());
    myRecyclerView.setAdapter(myGridAdapter);

    return fragGridLayout;
}


@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 onDataSetChanged(boolean state) {
    if (state){
        myGridAdapter.notifyDataSetChanged();
    }
}


public interface OnFragmentInteractionListener {
    public void onFragmentInteraction(Uri uri);
}


    public Context getActivityContext(){
        return this.getActivity();
    }

}

这是 RecyclerView 的适配器

and this is the Adapter for the RecyclerView

 public class MyGridAdapter extends RecyclerView.Adapter<MyGridAdapter.MyViewHolder> {

LayoutInflater layoutInflater;
private static ArrayList<HashMap> data=null;
private Context mContext;
public static Bitmap placeHolder;
    public MyGridAdapter(Context context){
      mContext = context;
      setInitialData();
      placeHolder =    BitmapFactory.decodeResource(mContext.getResources(),R.drawable.android_blue);
}
    public interface MyGridAdapterListener{
        public void onDataSetChanged(boolean state);
    }

private MyGridAdapterListener listener = null;
public void registerListener(MyGridAdapterListener newListener){
    listener = newListener;
}

@Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    Context context = parent.getContext();
    layoutInflater = LayoutInflater.from(context);
    View v = layoutInflater.inflate(R.layout.gridlayout_item_movie_start, parent, false);
    MyViewHolder vHolder=new MyViewHolder(v);
    return vHolder;
}

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {

      HashMap singleData = data.get(position);
      String movieRate = String.valueOf(singleData.get(MyJSONDataParser.TAG_VOTE_AVERAGE)) ;
        holder.movieTitle.setText((String)singleData.get(MyJSONDataParser.TAG_TITLE));
        holder.moviePoster.setImageBitmap(placeHolder);
        holder.movieRating.setRating(Float.valueOf(movieRate));
}

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


public static class MyViewHolder extends RecyclerView.ViewHolder  {
    TextView movieTitle;
    RoundedImageView moviePoster;
    RatingBar movieRating;

    public MyViewHolder(View v) {
        super(v);
        movieTitle = (TextView) v.findViewById(R.id.movieTitleTextView);
        moviePoster = (RoundedImageView) v.findViewById(R.id.roundImageView);
        movieRating = (RatingBar) v.findViewById(R.id.ratingBar);
    }
}


private boolean dataChanged = false;

public void setMyAdapterData(ArrayList<HashMap> data){
        this.data = data;
        dataChanged = true;
        if (listener != null){
            listener.onDataSetChanged(dataChanged);
        }
    }


protected void setInitialData(){
    HashMap initialItem = new HashMap();
    ArrayList<HashMap> initialData = new ArrayList<>(20);

    initialItem.put(MyJSONDataParser.TAG_TITLE, "Loading...");
    initialItem.put(MyJSONDataParser.TAG_MOVIE_BITMAP, placeHolder);
    initialItem.put(MyJSONDataParser.TAG_VOTE_AVERAGE, 3.3);
    for (int i = 0;i<20;i++){
        initialData.add(initialItem);
    }
    this.data = initialData;
}

}

推荐答案

我确实调用了 notifyDataSetChanged但这不是经过 alooooooooot 测试和记录后的问题,问题出在 setAdapterData 方法上,我只是将数据引用从加载器传递到适配器,但这不起作用,我不得不克隆 arraylist 而不是仅仅传递引用这对我来说听起来很奇怪,我仍然不明白为什么我必须这样做,如果您可以查看 MyGridAdapter 类中的 setAdapterData 方法并告诉我您的想法

I did call notifyDataSetChanged but this was not the issue after alooooooooot of testing and Logging the problem was with setAdapterData method where i just passed the data reference from the loader to the adapter but this didn't work and i had to clone the arraylist instead of just passing the reference which sounds strange to me and i still don't understand why I had to do that if you can take a look at the setAdapterData method in MyGridAdapter class and tell me what do you think 

这篇关于更新适配器数据后,RecyclerView 未显示任何内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆