【问题标题】:ArrayList Size Increasing every onCreateView callArrayList 大小增加每个 onCreateView 调用
【发布时间】: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


【解决方案1】:

添加

list.clear();

从 db 获得 cursor 之后。

【讨论】:

  • 这是填充列表的最佳方式吗因为它会在每次 RecyclerView 片段调用 (onCreateView) 时调用第一个列表将清除然后将条目返回给它。我只能添加单个条目(新条目)吗?表单添加
  • 是的。你可以测试一下。
  • 谢谢,它有帮助,但我心中仍有一个问题是它是填充 arraylist 的最佳方法,因为 clear 和 add 将在每个 onCreateView 中多次调用
  • 是的。这是最好的。
【解决方案2】:

你必须在OnCreateView

中初始化
list=new ArrayList<>();

【讨论】:

  • 每次调用 Fragment 的 onCreateView 时都会创建新的数组列表。
猜你喜欢
  • 2011-05-25
  • 1970-01-01
  • 1970-01-01
  • 2021-05-06
  • 2019-11-20
  • 2019-08-06
  • 2012-02-10
  • 2016-05-10
相关资源
最近更新 更多