recyclerview屏幕外edittext值为null

时间:2019-07-16 19:05:34

标签: android firebase android-recyclerview recycler-adapter

我有一个带有编辑文本的recyclerview,用户在编辑文本中输入数字,当他单击“保存”按钮时,一个for循环遍历所有recyclerview编辑文本并将数据保存到Firebase,我的问题是,如果有列表大于屏幕大小的数据将忽略所有屏幕外的值,并将它们的值另存为空值,仅保存屏幕上显示的项目,这是我的代码

@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.purchases_list_activity, container, false);
    mItem = new ArrayList<>();
    mAdapter = new PurchasesAdapter(getContext(), mItem, this);
    re = view.findViewById(R.id.itemsPListView);
    re.setLayoutManager(new LinearLayoutManager(getActivity()));
    re.setAdapter(mAdapter);

    FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
    assert user != null;
    userid = user.getUid();
    usersDatabase = FirebaseDatabase.getInstance().getReference().child("users").child(userid);
    usersDatabase.keepSynced(true);
    usersDatabase.child("p").addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.getValue() != null) {

                p = (dataSnapshot.child("p").getValue()).toString();
                purchasesDatabase = usersDatabase.child("purchases").child("p").child(p).child("Cigarette");
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError databaseError) {

        }
    });
    itemsDatabase = FirebaseDatabase.getInstance().getReference().child("users").child(userid).child("items").child("Cigarette");
    itemsDatabase.keepSynced(true);
    itemsDatabase.addListenerForSingleValueEvent(new ValueEventListener() {
        @Override
        public void onDataChange(DataSnapshot dataSnapshot) {
            mItem.clear();
            for (DataSnapshot teacherSnapshot : dataSnapshot.getChildren()) {
                ItemsAdapter upload = teacherSnapshot.getValue(ItemsAdapter.class);
                upload.setKey(teacherSnapshot.getKey());
                mItem.add(upload);
            }
            mAdapter.notifyDataSetChanged();
        }

        @Override
        public void onCancelled(DatabaseError databaseError) {
            Toast.makeText(getActivity(), databaseError.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

    fabItem = view.findViewById(R.id.fab_purchases_add_item);
    fabItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            addItem();

        }
    });


    fab = view.findViewById(R.id.fab_purchases);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            for (int i = 0; i < mItem.size(); i++) {
                final ItemsAdapter mylist = mItem.get(i);
              -------------------------------------
              - View childView = re.getChildAt(i);-
              ------------------------------------- 
                i think my problem is her inside the dash box


                final EditText Qty = childView.findViewById(R.id.itemNewPcs);
                final String qty = Qty.getText().toString();

                    final Map<String, Object> note = new HashMap<>();
                    note.put(KEY_ITEMID, mylist.getItemID());
                    note.put(KEY_ITEMSQTY, qty);
                    note.put(KEY_ITEMNAME, mylist.getItemName());
                    usersDatabase.child("inventory").child("p").child(p).child("Cigarette")
                            .child(mylist.getItemID()).addListenerForSingleValueEvent(new ValueEventListener() {
                        @Override
                        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
                            if (dataSnapshot.getValue() == null) {
                                dataSnapshot.getRef().updateChildren(note);
                            }
                        }

                        @Override
                        public void onCancelled(@NonNull DatabaseError databaseError) {
                        }
                    });
                }

1 个答案:

答案 0 :(得分:0)

使用RecyclerView时,屏幕外的视图将被回收(因此称为小部件的名称)到视图池中,并在以后新项目滚动到视图中时重新使用。在将它们从视图中删除后,您不应该使用它们,因为您不知道它们将附加哪些数据。

如果即使从视图上移开后仍需要保留整个视图集,请不要使用RecyclerView。只需将整个视图集合到一个ScrollView中,以便在滚动视图时它们始终在内存中。