-1
public class ReferralStatus extends Fragment { 

public MyPendingAdapter pAdapter; 
public RecyclerView recyclerView; 

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

} 

@Override 
public View onCreateView(LayoutInflater inflater, ViewGroup container, 
         Bundle savedInstanceState) { 
    // Inflate the layout for this fragment 
    View layout= inflater.inflate(R.layout.fragment_referral_status, container, false); 

    recyclerView = (RecyclerView) layout.findViewById(R.id.pendingRecyclerList); 
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
    return layout; 
} 

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

    PendingAsyncTask pendingRunner = new PendingAsyncTask(); 
    pendingRunner.execute("someURL"); 
} 

public class MyPendingAdapter extends RecyclerView.Adapter<MyPendingAdapter.PendingUserHolder> { 

    Context context; 
    String[] pendingUserArray, socialSitesArray; 


    public MyPendingAdapter(Context c, String[] pendingUserList, String[] socialSites) { 

     Log.d("golu", "pendingConstructor"); 
     this.context=c; 
     this.pendingUserArray=pendingUserList; 
     this.socialSitesArray=socialSites; 
    } 

    public class PendingUserHolder extends RecyclerView.ViewHolder{ 

     TextView pendingUsername; 
     TextView socialSite; 

     public PendingUserHolder(View v){ 
      super(v); 
      pendingUsername = (TextView) v.findViewById(R.id.pendingUsername); 
      socialSite= (TextView) v.findViewById(R.id.socialSiteLabel); 
     } 
    } 

    @Override 
    public PendingUserHolder onCreateViewHolder(ViewGroup parent, int viewType) { 

     Log.d("golu", "pendingHolder"); 
     LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View row = inflater.inflate(R.layout.pending_list_row,parent,false); 
     PendingUserHolder holder = new PendingUserHolder(row); 
     return holder; 
    } 

    @Override 
    public void onBindViewHolder(PendingUserHolder holder, int position) { 
     holder.pendingUsername.setText(pendingUserArray[position]); 
     holder.socialSite.setText(socialSitesArray[position]); 
    } 

    @Override 
    public int getItemCount() { 
     Log.d("golu", "pendingCount"); 
     return pendingUserArray.length; 
    } 
} 

public class PendingAsyncTask extends AsyncTask<String,Void,Boolean>{ 

    String [] pendingUserList, socialSitesMedium; 



    @Override 
    protected Boolean doInBackground(String... params) { 

     //just returning simple boolean value 
     return true; 

    } 

    @Override 
    protected void onPostExecute(Boolean aBoolean) { 
     super.onPostExecute(aBoolean); 
     if(aBoolean) 
     { 
      //Hardcoding Arrays for now      
      pendingUserList=getResources().getStringArray(R.array.pendingUserList); 
         socialSitesMedium=getResources().getStringArray(R.array.socialSites); 



      pAdapter = new MyPendingAdapter(getActivity(),pendingUserList,socialSitesMedium); 
      recyclerView.setAdapter(pAdapter); 


     } 
    } 
} 

//這是XML文件「回收站View..No適配器連接:跳過佈局」使用異步任務錯誤的片段

<LinearLayout 

android:layout_width="match_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical"> 

     <TextView 
      android:layout_width="wrap_content" 
      android:layout_height="wrap_content" 
      android:text="@string/pending" 
      android:id="@+id/pendingLabel" 
      android:textColor="#FF0000" 
      android:textAllCaps="true" 
      android:textStyle="bold" 
      android:paddingTop="5dp" 
      android:paddingBottom="5dp" 
      android:layout_marginLeft="10dp" /> 

     <android.support.v7.widget.RecyclerView 
      android:id="@+id/pendingRecyclerList" 
      android:layout_width = "match_parent" 
      android:layout_height = "match_parent"> 

     </android.support.v7.widget.RecyclerView> 
</LinearLayout> 

//它展示跳繩佈局錯誤... 。我究竟做錯了什麼? // FYI:我嘗試初始化RecyclerView和設置在onPostExecute的LinearLayoutManager ....它仍然沒有工作:/

回答

0

我在做什麼錯?

RecyclerView(和ListView)要求Adapter能夠工作,因爲適配器是這些小部件獲取數據顯示的方式。您的代碼中有適配器,但缺少告訴RecyclerView使用它的代碼:setAdapter()

更多細節

+0

我沒有在onPostExecute na中設置適配器嗎? – abhishekm473

+0

@ user2508635在'onCreateView'中執行。在更新onPostExecute中填充回收站視圖的數據後,您可以在'onPostExecute'刷新適配器 – Raghunandan

0

你需要有初始化適配器和適配器設置爲回收站觀點的問題在於OnCreateView方法見https://developer.android.com/training/material/lists-cards.html。 由於適配器不傳遞數據,您可以稍後更新顯示。 由於onCreateView方法是更新顯示方式,所以它的重要初始化它否則它會給出一個錯誤「沒有適配器連接跳過顯示」

相關問題