ArrayList Size增加每个onCreateView调用

时间:2016-09-23 09:14:20

标签: java android sqlite android-fragments arraylist

我在RecyclerView中有Fragment,我正在ArrayList填充Sqlite,并且有一个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;
    }

所以我的问题是,从格式Arraylist返回RecyclerView Fragment后,如何避免Fragment以增加其大小?

每次调用RecyclerView Fragment's onCreateView时,如何从数据库中获取完整的行。

2 个答案:

答案 0 :(得分:0)

添加

list.clear();
从db。

获取游标

答案 1 :(得分:0)

您必须在 OnCreateView

中进行初始化
ReadToFollowing('<Barcodes><Barcode><Code>')