应用重新启动/重新加载时保持复选框状态

时间:2019-06-27 08:18:29

标签: android checkbox state reload android-viewholder

我正在创建自己的应用程序。 目标是拥有一个产品列表,每个产品都有3个复选框。 1个复选框:“我有” 1个checbox:“我想要”,1个代表“我想要交换”。

但是单击功能可以,但是当我重新启动应用程序时,我的复选框没有完成。他们都是假的。

我在Firebase中存储了每个用户在“拥有”中的复选框的状态。看到图片)

能帮我吗?我尝试了很多东西,但没有任何效果

@Override
protected void onStart() {
    super.onStart();
    FirebaseRecyclerOptions<Products> options =
            new FirebaseRecyclerOptions.Builder<Products>()
                    .setQuery( ProductsRef, Products.class )
                    .build();

    FirebaseRecyclerAdapter<Products, ProductViewHolder> adapter =
            new FirebaseRecyclerAdapter<Products, ProductViewHolder>( options ) {
                @Override
                protected void onBindViewHolder(@NonNull final ProductViewHolder holder, int position, @NonNull final Products model) {

                    holder.txtProductName.setText( model.getTitle() );
                    possedeCheckbox = (CheckBox) holder.itemView.findViewById( R.id.pin_posseder_checkbox );
                    echangeCheckbox = (CheckBox) holder.itemView.findViewById( R.id.pin_echanger_checkbox );
                    desireCheckbox = (CheckBox) holder.itemView.findViewById( R.id.pin_desirer_checkbox );

                    DatabaseReference UserRef = FirebaseDatabase.getInstance().getReference().child( "Users" );
                    UserRef.child( Prevalent.currentOnlineUser.getPseudo() ).child( "possede" );
                    ValueEventListener valueEventListener = new ValueEventListener() {
                        @Override
                        public void onDataChange(DataSnapshot dataSnapshot) {
                            ArrayList<String> possedePins = new ArrayList<String>(  );
                            for (DataSnapshot ds : dataSnapshot.child(Prevalent.currentOnlineUser.getPseudo()).child( "possede" ).getChildren()) {
                                String name = ds.getKey();
                                possedePins.add( name );
                            }
                            System.out.println(model.getTitle());
                            if(possedePins.contains( model.getTitle() )){
                                possedeCheckbox.setChecked( true );
                                System.out.println("YEEESSSSS");
                            } else {
                                System.out.println("NOOOOOOOO");
                            };
                        }

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

                    UserRef.addValueEventListener( valueEventListener );



                    // initialiser les Checkbox
                    Picasso.get().load( model.getImage() ).into( holder.imageView );

                    possedeCheckbox.setOnLongClickListener( new View.OnLongClickListener() {
                        @Override
                        public boolean onLongClick(View v) {
                            Toast.makeText(getApplicationContext(),"clique si tu possède ce pin", Toast.LENGTH_SHORT).show();
                            return false;
                        }
                    } );
                    holder.itemView.findViewById( R.id.pin_posseder_checkbox ).setOnClickListener( new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            // quand je deselectionne
                            if (possedeCheckbox.isChecked()) {
                                possedeCheckbox.setChecked( false );
                                AddPosedePins(model.getTitle(), "remove");
                                Toast.makeText( HomeActivity.this, "possede pas", Toast.LENGTH_SHORT ).show();
                            } else {
                                // quand je selectionne
                                possedeCheckbox.setChecked( true );
                                AddPosedePins(model.getTitle(), "add");
                                Toast.makeText( HomeActivity.this, "possede ", Toast.LENGTH_SHORT ).show();
                            }
                        }
                    } );
                    holder.itemView.findViewById( R.id.pin_echanger_checkbox ).setOnClickListener( new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (echangeCheckbox.isChecked()) {
                                possedeCheckbox.setChecked( false );
                                Toast.makeText( HomeActivity.this, "echange pas", Toast.LENGTH_SHORT ).show();
                            } else {
                                possedeCheckbox.setChecked( true );
                                Toast.makeText( HomeActivity.this, "echange", Toast.LENGTH_SHORT ).show();
                            }
                        }
                    } );
                    holder.itemView.findViewById( R.id.pin_desirer_checkbox ).setOnClickListener( new View.OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            if (desireCheckbox.isChecked()) {
                                possedeCheckbox.setChecked( false );
                                Toast.makeText( HomeActivity.this, "desire pas", Toast.LENGTH_SHORT ).show();
                            } else {
                                possedeCheckbox.setChecked( true );
                                Toast.makeText( HomeActivity.this, "desire ", Toast.LENGTH_SHORT ).show();
                            }
                        }
                    } );
                }

                @NonNull
                @Override
                public ProductViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
                    View view = LayoutInflater.from( parent.getContext() ).inflate( R.layout.product_items_layout, parent, false );
                    final ProductViewHolder holder = new ProductViewHolder(view);
                    return holder;
                }
            };
    recyclerView.setAdapter( adapter );
    adapter.startListening();

}

Image of 1 product with its checkboxes image of my database here

0 个答案:

没有答案