我有RecyclerView
在Fragment
,我從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
時,如何從數據庫中獲得完整的行。
您沒有添加list.clear()這就是爲什麼duplicacy有 –
叫它'的onCreate()' – SripadRaj
名單=新的ArrayList <>() ;在你從數據庫獲取所有數據之前添加此行 – Vickyexpert