2016-09-06 217 views
1

更新我有一個fragment_1其中有按鈕呼叫地址其發送用戶fragment_2 填寫表格4 5 編輯文本和同fragment_2用戶需要按保存表單返回回來填補上fragment_2到這裏一切工作fine.In登錄我可以看到fragment_1從fragment_2回收站電源適配器沒有與更新適配器

Fragm獲取數據的細節fragment_1 ent_1其中有RecyclerView顯示用戶表單填寫上Fragment_2

問題是我在不同的method.In獲得在fragment_1數據的方法我打電話adapter.notifyDataSetChanged(); 這應該調用適配器方法但只有getItemCount正在運行,而不是其他處理RecyclerView的方法。 請檢查logcat的下方,以獲取有關問題的理念

這裏是我的方法,其中我打電話notifydatasetchanged()

public void PPL_Location(PPL_list_wrapper ppl_list_wrapper){ 
     PPL_wrapper=ppl_list_wrapper; 
     Log.d(TAG,"PROFILE DATA CALLING"); 
     Log.d(TAG,ppl_list_wrapper.toString()); 
     Loc_details.add(PPL_wrapper); 
     Log.d(TAG,PPL_wrapper.getName()); 
     adapter=new ppl_Recycler_Adapter(getActivity(),Loc_details); 
     int item=adapter.getItemCount(); 
    adapter.notifyDataSetChanged(); 
     Log.d(TAG,"Here is the value of Location Details: "+"Size "+Loc_details.size()+" "+"Details "+Loc_details.iterator()+" "+ "Location "+Loc_details.get(0)+" "+item); 
    } 

這裏是我的適配器稱爲PPL_Re_Adapter

public class ppl_Recycler_Adapter extends RecyclerView.Adapter<ppl_Recycler_Adapter.ViewHolder> { 
    List<PPL_list_wrapper> ppl_Details; 
    Context context; 
    public static final String TAG="PPL_Re_Adapter####"; 


    public ppl_Recycler_Adapter(Context context,List<PPL_list_wrapper> ppl_Details) { 
     this.ppl_Details=ppl_Details; 
     this.context=context; 
     Log.d(TAG,"Adapter Constructor Running With Parameters"); 

    } 


    @Override 
    public ppl_Recycler_Adapter.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { 
     LayoutInflater layoutInflater= (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     View view=layoutInflater.inflate(R.layout.ppl_single_row,parent,false); 
     Log.d(TAG,"onCreate View Holder");  
     return new ViewHolder(view); 
    } 

    @Override 
    public void onBindViewHolder(ppl_Recycler_Adapter.ViewHolder holder, int position) { 
     Log.d(TAG,"onBindViewHolder"); 
     if (ppl_Details.size()==0){ 
      Log.d(TAG,"List is Null");  
     } 
     else { 
      Log.d(TAG,"Process Views Here"); 
     }  
    } 

    @Override 
    public int getItemCount() { 
     if (ppl_Details==null && ppl_Details.isEmpty()){ 
      Log.d(TAG,"List Is Null");  
      return 0;  
     } 
     else { 
      Log.d(TAG,"List Is Not Null"); 
      Log.d(TAG,"Array Size is "+ppl_Details.size()); 
      return ppl_Details.size(); 
     } 
    } 

    public class ViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener { 

     ImageView ppl_Image; 
     TextView ppl_name; 
     TextView ppl_address; 
     TextView ppl_timePeriod; 
     ImageButton ppl_delete; 
     ImageButton ppl_Verify; 
     FloatingActionButton fab;  

     public ViewHolder(View itemView) { 
      super(itemView); 
      Log.d(TAG,"View Holder Running"); 
      ppl_Image= (ImageView) itemView.findViewById(R.id.past_permanent_location_picture); 
      ppl_name= (TextView) itemView.findViewById(R.id.name); 
      ppl_address= (TextView) itemView.findViewById(R.id.address); 
      ppl_timePeriod= (TextView) itemView.findViewById(R.id.time_period); 
      ppl_delete= (ImageButton) itemView.findViewById(R.id.delete); 
      ppl_Verify= (ImageButton) itemView.findViewById(R.id.verify); 
      fab= (FloatingActionButton) itemView.findViewById(R.id.PPL_fab_Add_PPL); 
      itemView.setOnClickListener(this);  
     } 

     @Override 
     public void onClick(View v) { 
      Context context=v.getContext(); 
      Intent showPPL_Form=new Intent(context,Add_PPL.class); 
      context.startActivity(showPPL_Form); 
     } 
    } 

這裏Logcat。在Logcat中,Log中沒有顯示來自適配器的logcat然後getItemCount

09-06 16:12:46.513 28727-28727/com.example.com.pro_working1 D/====Fragment_1====: PROFILE DATA CALLING 
09-06 16:12:46.513 28727-28727/com.example.com.pro_working1 D/====Fragment_1====: com.[email protected]426c3f50 
09-06 16:12:46.513 28727-28727/com.example.com.pro_working1 D/====Fragment_1====: n sana 
09-06 16:12:46.513 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: Adapter Constructor Running With Parameters 
09-06 16:12:46.513 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: List Is Not Null 
09-06 16:12:46.513 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: Array Size is 1 
09-06 16:12:46.513 28727-28727/com.example.com.pro_working1 D/====Fragment_1====: Here is the value of Location Details: Size 1 Details [email protected] Location com.[email protected]426c3f50 1 
09-06 16:12:46.513 28727-28727/com.example.com.pro_working1 D/Navigation Drawer*****: Here is PPL DATA com.[email protected]426c3f50 
09-06 16:12:46.523 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: Adapter Constructor Running With Parameters 
09-06 16:12:46.533 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: List Is Not Null 
09-06 16:12:46.533 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: Array Size is 0 
09-06 16:12:46.533 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: List Is Not Null 
09-06 16:12:46.533 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: Array Size is 0 
09-06 16:12:46.533 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: List Is Not Null 
09-06 16:12:46.533 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: Array Size is 0 
09-06 16:12:46.533 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: List Is Not Null 
09-06 16:12:46.533 28727-28727/com.example.com.pro_working1 D/PPL_Re_Adapter####: Array Size is 0 

Fragment_1完整代碼

RecyclerView mRecyclerView; 
    RecyclerView.Adapter adapter; 
    List<PPL_list_wrapper> Loc_details=new ArrayList<PPL_list_wrapper>(); 
    FloatingActionButton fab; 
    PPL_list_wrapper PPL_wrapper; 
    public static final String TAG="====Fragment_1===="; 


    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     View view=inflater.inflate(R.layout.fragment_fragment_1, container, false); 
     mRecyclerView= (RecyclerView) view.findViewById(R.id.ppl_RecyclerView); 
     mRecyclerView.setLayoutManager(new LinearLayoutManager(getActivity())); 
     adapter=new ppl_Recycler_Adapter(getActivity(),Loc_details); 
     mRecyclerView.setAdapter(adapter); 

     fab= (FloatingActionButton) view.findViewById(R.id.PPL_fab_Add_PPL); 

     fab.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 
       FragmentTransaction fragmentTransaction=getFragmentManager().beginTransaction(); 
       Add_PPL add_ppl=new Add_PPL(); 
       fragmentTransaction.add(R.id.Navigation_Main_Layout,add_ppl); 
       fragmentTransaction.commit(); 
      } 
     }); 




     return view; 

    } 

    public void PPL_Location(PPL_list_wrapper ppl_list_wrapper){ 
     PPL_wrapper=ppl_list_wrapper; 
     Log.d(TAG,"PROFILE DATA CALLING"); 
     Log.d(TAG,ppl_list_wrapper.toString()); 
     Loc_details.add(PPL_wrapper); 
     Log.d(TAG,PPL_wrapper.getName()); 
     adapter=new ppl_Recycler_Adapter(getActivity(),Loc_details); 
     int item=adapter.getItemCount(); 
     adapter.notifyDataSetChanged(); 
     Loc_details.add(PPL_wrapper); 
     Log.d(TAG,"Here is the value of Location Details: "+"Size "+Loc_details.size()+" "+"RecyclerView "+mRecyclerView+" "+ "Location "+Loc_details.get(0)+" "+"Item Size "+item); 
     mRecyclerView.setAdapter(adapter); 
     mRecyclerView.invalidate(); 


    } 
+0

在adapter.notifyDataSetChanged()後再次添加listview.setAdapter(adapter); –

+0

@ user2564055這不會工作。我嘗試 – Ritu

回答

0

您首先調用適配器沒有結果,那麼你填寫表格並期望細節填入您RecyclerView和問題是,你得到結果成片段,但你的列表沒有那個結果。

現在按照我的代碼我試過了,我硬編碼適配器的getItemCount返回值爲1,它顯示所有方法到logcat,所以我只是讓你的列表靜態,現在它的工作。

請讓你列表靜態

static List<PPL_list_wrapper> Loc_details=new ArrayList<PPL_list_wrapper>(); 

希望它能幫助

正如你說你的佈局同時顯示片段一次。其片段的問題,添加或替換,以便按您的代碼更改此行

Add_PPL add_ppl=new Add_PPL(); 
fragmentTransaction.add(R.id.Navigation_Main_Layout,add_ppl); 

進入這個

fragmentTransaction.replace(R.id.Navigation_Main_Layout,add_ppl); 

而對於你的第二個問題是出大比分的佈局,請檢查您的單列布局XML並確保高度爲wrap_content或張貼您的代碼在這裏

+0

Thankyou這麼多的工作,但有更多的問題,我面臨1)我的fragment_2更改爲fragment_1,但它混亂了佈局通過顯示兩個片段在同一時間,直到我按再次添加按鈕2)我的1回收站採取全屏和其他人之間後,他們之間的利潤率大 – Ritu

+0

檢查我更新的答案 – androidXP

+0

謝謝你這麼多的幫助,我真的很感激它非常感謝 – Ritu

0

我曾嘗試下面的代碼和它的工作對我來說:

recyclerView.setAdapter(new RecyclerViewAdapter(newList)); 
recyclerView.invalidate(); 

在你的情況下,它不會發生,因爲你是給新數據您以前的活動首先必須填寫容器,然後再次發送適配器並使其無效。

+0

當我在我的fragment方法中調用mRecyclerView時,即使在'findviewbyid','setlayoutmanager'後它總是返回null。所以我不能在我的方法中調用recyclerview – Ritu

+0

粘貼整個代碼以便理解問題。 @Ritu –

+0

'D/==== Fragment_1 ====:以下是位置詳細信息的值:大小2 RecyclerView null位置com.[email protected]4257c1d8項目大小1'執行器後檢查詳細信息您的代碼請參閱RecyclerView的空值 – Ritu

0

你好,你可以嘗試做這樣的事情,而不是每次創建一個新的適配器嘗試清除適配器並添加新的列表。這種方法應該是在你的適配器和mList應該是充滿你的適配器列表:

public void setAll(@NonNull 
        final List<Object> yourCustomObjectList) { 
    mList.clear(); 
    mList.addAll(yourCustomObjectList); 
    notifyDataSetChanged(); 
} 
+0

http:// pastebin.com/A7y6Mwik 請檢查日誌後實施你的代碼 – Ritu