【发布时间】:2016-09-23 09:14:20
【问题描述】:
我在Fragment 中有RecyclerView,我正在从Sqlite 填充ArrayList,并且有一个Form 可以让我将条目添加到数据库中。那么会出现什么问题呢?
在我添加任何新条目之前它工作正常,但是当我通过表单添加新条目时,它会重定向回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;
}
所以我的问题是,在从Fragment 回到RecyclerView Fragment 后,如何避免Arraylist 以增加其大小?
每当每次调用RecyclerView Fragment's onCreateView 时,我如何从数据库中获取完整的行。
【问题讨论】:
-
你没有添加 list.clear() 这就是为什么存在重复
-
在
onCreate()调用它 -
list=new ArrayList();在从数据库获取所有数据之前添加这一行
-
在将它添加到 arraylist 之前从 do.....while 循环中清除您的 arraylist
标签: java android sqlite android-fragments arraylist