-1

我需要幫助解決這個問題我已經通過我的代碼幾天瞭解什麼是錯誤的,但我無法弄清楚。 我有一個RecyclerView的片段,我用一個圖像佔位符和一些默認文本初始化適配器數據,片段顯示它們如預期那樣使用加載器從互聯網獲取數據並解析並將新數據傳遞給適配器所有這一切都按需要發生),直到數據到達適配器,然後Taaadaaaa最初的數據消失,並且新的數據實際上並未顯示該片段顯示一個空白屏幕肯定我做錯了,請指教。 這是片段代碼RecyclerView在更新適配器數據後沒有顯示任何東西

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

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

}

+0

代碼縮進存在問題,某些代碼顯示爲純文本,我沒有看到數據在適配器中聲明的位置,並且沒有遵循Android命名約定(小寫的第一個字母代表您的類,沒有m的數據前綴爲成員變量) –

+0

代碼顯示爲純文本這發生在我寫這篇文章時,因爲這是第一次在這裏發佈問題,因爲適配器變量肯定是我錯過了,包括他們,他們是{Context mContext; Arraylist data = null;位圖placeHolder;} –

+0

你應該編輯你的問題 –

回答

2

我打過電話notifyDataSetChanged 但這不是alooooooooot後的問題的測試和日誌記錄問題是與setAdapterData方法,其中我只是從加載器傳遞數據引用到適配器,但這並沒有工作,我不得不克隆ArrayList,而不是傳遞給我聽起來很奇怪的參考,我仍然不明白爲什麼我必須這樣做,如果你可以看看MyGridAdapter類中的setAdapterData方法,並告訴我你認爲怎麼樣

0

嘗試調用notifyDataSetChanged()當您修改適配器的數據:

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

我看到你試圖在你的監聽器的回調中做到這一點,但也許它是空的,因此從不發出通知?

[註釋] 當我想要替換整個數據集在我的適配器我經常寫類似這樣的方法:

public void setData(List<MyData> newData) { 
    this.data.clear(); 
    this.data.addAll(newData); 
    this.notifyDataSetChanged(); 
} 
相關問題