2016-09-23 75 views
0

我有RecyclerViewFragment,我從Sqlite填充ArrayList,並且有一個Form允許我將條目添加到數據庫中。那麼會出現什麼問題?ArrayList大小增加每個onCreateView調用

它工作正常,直到我添加任何新條目,但是當我通過表單添加新條目時,它會重定向回RecyclerView's Fragment,而不會顯示任何新添加的條目。當我再次按下按鈕添加條目並返回而無需通過按返回按鈕添加任何條目時,它會顯示新條目,並且還會複製以前的條目。我不知道自己出錯的地方,我已經嘗試過解決問題,但無法解決問題。

這是我fragment在那裏我有RecyclerView

public class fragment_1 extends Fragment { 

    CRUD_database db; 
    SQLiteDatabase sqLiteDatabase; 
    RecyclerView mRecyclerView; 
    RecyclerView.Adapter adapter; 
    RecyclerView.LayoutManager layoutManager; 
    static List<PPL_list_wrapper> Loc_details=new ArrayList<PPL_list_wrapper>(); 
    FloatingActionButton fab; 
    PPL_list_wrapper PPL_wrapper; 
    int ID; 
    ArrayList<PPL_list_wrapper> list=new ArrayList<>(); 
    public static final String TAG="====Fragment_1===="; 
    String name,title,address,passing_year,description; 

    @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); 
     TextView textView= (TextView) view.findViewById(R.id.listNULL); 
     mRecyclerView= (RecyclerView) view.findViewById(R.id.ppl_RecyclerView); 
     layoutManager=new LinearLayoutManager(getActivity()); 
     mRecyclerView.setLayoutManager(layoutManager); 
     adapter=new ppl_Recycler_Adapter(getActivity(),list); 
     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(); //This Fragment Has Form To Add Entry into Database 
       fragmentTransaction.replace(R.id.Navigation_Main_Layout,add_ppl); 
       fragmentTransaction.addToBackStack(null); 
       fragmentTransaction.commit(); 
      } 
     }); 

     Log.d(TAG,"Value of Context Oncreate "+" Value of GetActivity"+" "+getActivity()); 
     db=new CRUD_database(getActivity()); 
     Cursor cursor=db.getALL_PPL(); 
      Log.d(TAG,"Cursor Position To First :"+cursor.moveToFirst()); 
      Log.d(TAG,"Cursor and List Value :"+"Cursor: "+cursor.getCount()+" "+"List: "+list.size()); 
      if (cursor.moveToFirst()&& !(list.size()==cursor.getCount())){ // This Condition to avoid Array to increase its size as soon as database has new row it will call 
       do{ 

         Log.d(TAG,"Cursor Position: "+cursor.getColumnCount()+" "+"Value of Rows "+cursor.getCount()); 
         ID=cursor.getInt(cursor.getColumnIndex("_ID")); 
         name=cursor.getString(cursor.getColumnIndex("PPL_NAME")); 
         title=cursor.getString(cursor.getColumnIndex("PPL_TITLE")); 
         address=cursor.getString(cursor.getColumnIndex("PPL_ADDRESS")); 
         passing_year=cursor.getString(cursor.getColumnIndex("PPL_TIME")); 
         description=cursor.getString(cursor.getColumnIndex("PPL_DESCRIPTION")); 

         Log.d(TAG,title+" "+address+" "+ passing_year+" "+description+" "+ID); 
         PPL_list_wrapper fromDatabase=new PPL_list_wrapper(ID,title,name,passing_year,address); 
         list.add(fromDatabase); 
         adapter=new ppl_Recycler_Adapter(getActivity(),list); 
         // adapter.notifyItemChanged(cursor.getCount()); 

       } 
       while (cursor.moveToNext()); 
       adapter.notifyItemChanged(cursor.getCount()); 
       cursor.close(); 
      } 
     else if (cursor.getColumnCount()==0 || list.size()==0){ 
       textView.setText("NO RECORD FOUND"); 
      } 

     return view; 
    } 

所以我的問題是,如何避免Arraylist爲了從形式Fragment回來後RecyclerView Fragment增加它的大小?

每次調用RecyclerView Fragment's onCreateView時,如何從數據庫中獲得完整的行。

+0

您沒有添加list.clear()這就是爲什麼duplicacy有 –

+0

叫它'的onCreate()' – SripadRaj

+0

名單=新的ArrayList <>() ;在你從數據庫獲取所有數據之前添加此行 – Vickyexpert

回答

0

從數據庫得到光標後添加

list.clear(); 

+0

是填充列表的最佳方式,因爲它會每次調用RecyclerView片段調用(onCreateView) 第一列表將清除,然後獲得回到它的條目。我只能添加單一條目(新條目),這是剛剛添加形式 – Ritu

+0

是啊。你可以測試它。 –

+0

Thx它有幫助,但在我的腦海裏仍然有一個問題,它是填充arraylist cuz清除和添加的最佳方式將在每個onCreateView – Ritu

0

你必須初始化OnCreateView

list=new ArrayList<>(); 
+0

它將如何解決問題? – androidXP

+0

每次調用Fragment的onCreateView時,它將創建新的ArrayList。 –