Values in Recyclerview are changed by scrolling down and returning

0
    public class RecyclerViewAdapter_Accueil extends RecyclerView.Adapter<RecyclerViewAdapter_Accueil.RestaurantViewHolder> {
    private Double latitude;
    private Double longitude;
    private List<Restaurant> restaurantList;

    private int descuento;



    public RecyclerViewAdapter_Accueil( List<Restaurant> restaurant, Double latitude, Double longitude) {
        this.restaurantList=restaurant;
        this.latitude=latitude;
        this.longitude=longitude;
    }


    @Override
    public RecyclerViewAdapter_Accueil.RestaurantViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.row_restaurants, parent, false);
        RestaurantViewHolder viewHolder = new RestaurantViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindViewHolder(final RestaurantViewHolder holder, final int position) {
        final Restaurant mItems=this.restaurantList.get(position);

        String nombreRestaurant=mItems.getName();
        holder.tvTituloRestaurant.setText(nombreRestaurant);

        descuento=mItems.tomarPromocion();

        if (descuento>0){
            holder.tvRestaurantPromocion.setVisibility(View.VISIBLE);
            holder.tvRestaurantPromocion.setText(descuento+"%");
        }
        holder.test10(mItems);

       // holder.test(restaurantList);
        holder.test2(restaurantList);
        holder.test3(restaurantList,latitude,longitude);





    }



    @Override
    public int getItemCount() {
        return (restaurantList == null) ? 0 : restaurantList.size();
    }



    public static class RestaurantViewHolder extends RecyclerView.ViewHolder {

//        public ImageView imageView;
        CustomFontTextView tvTituloRestaurant;
        CustomFontTextView tvStateReservation;
        CustomFontTextView tvKm;
        CustomFontTextView tvRestaurantPromocion;

        CustomFontTextView [] tvTag =new CustomFontTextView[6];

        ImageView ivRestaurantList;

        public RestaurantViewHolder(View itemView) {
            super(itemView);
//            imageView= (ImageView) itemView.findViewById(R.id.ivRestaurant);

            tvKm= (CustomFontTextView) itemView.findViewById(R.id.tvKm);
            tvTituloRestaurant = (CustomFontTextView)itemView.findViewById(R.id.tvTituloRestaurant);
            tvStateReservation = (CustomFontTextView)itemView.findViewById(R.id.tvStateReservation);
            ivRestaurantList = (ImageView) itemView.findViewById(R.id.ivRestaurantList);
            tvRestaurantPromocion = (CustomFontTextView) itemView.findViewById(R.id.tvRestaurantPromocion);

            tvTag[0] = (CustomFontTextView) itemView.findViewById(R.id.tvTag1);
            tvTag[1] = (CustomFontTextView) itemView.findViewById(R.id.tvTag2);
            tvTag[2] = (CustomFontTextView) itemView.findViewById(R.id.tvTag3);
            tvTag[3] = (CustomFontTextView) itemView.findViewById(R.id.tvTag4);
            tvTag[4] = (CustomFontTextView) itemView.findViewById(R.id.tvTag5);
            tvTag[5] = (CustomFontTextView) itemView.findViewById(R.id.tvTag6);


        }

        public void test10(Restaurant restaurant){
            if(restaurant.getTags()!=null) {
                for (int x = 0; x < restaurant.getTags().size(); x++) {
                    String tag = restaurant.getTags().get(x);
                    tvTag[x].setVisibility(View.VISIBLE);
                    tvTag[x].setText(tag);
                }
            }
        }



        public void test3(List<Restaurant> restaurantList,Double latitude,Double longitude){


            Location locationMain = new Location("");
            locationMain.setLatitude(latitude);
            locationMain.setLongitude(longitude);

            Location locationRestaurant = new Location("");
            locationRestaurant.setLatitude(restaurantList.get(getAdapterPosition()).getLatitude());
            locationRestaurant.setLongitude(restaurantList.get(getAdapterPosition()).getLongitude());

            float distanceInMeters = locationMain.distanceTo(locationRestaurant);
            double distanceInKm= distanceInMeters/1000;

            DecimalFormat df = new DecimalFormat("0.00");
            tvKm.setText(String.valueOf(df.format(distanceInKm))+" Km");
        }

        public void test2(List<Restaurant> restaurantList){

            switch (restaurantList.get(getAdapterPosition()).getStateOrder()){
                case 0:
                    tvStateReservation.setText("Sur place/ À emporter");
                    break;
                case 1:
                    tvStateReservation.setText("Sur place");
                    break;
                case 2:
                    tvStateReservation.setText("À emporter");
                    break;
            }

        }

    }

}

The problem is in the method called test10 Where I check the size of the list of tags from the object in the list of objects, to make textviews appear and show the corresponding tags

It works, but when I go through the whole list, I scroll downwards, when I return, tags that should not go in which already showed well appear, the correct tags appear and 1 tag that should not go.

For example, in the restaurant were the Italian and Pizza tags, when climbing Italian, pizza and traditional appears

Alado was a Japanese restaurant, before he said Japanese, now he says, Japanese and pizza.

    
asked by Eduardo Ricardez 27.04.2017 в 15:21
source

1 answer

1

I have no idea why, but it seems that when returning the TextViews that they change their visibility to Visible, they remained that way for the following ones, so add this:

public void test10(Restaurant restaurant){
        if(restaurant.getTags()!=null) {
            for (int x = 0; x < restaurant.getTags().size(); x++) {
                String tag = restaurant.getTags().get(x);
                tvTag[x].setVisibility(View.VISIBLE);
                tvTag[x].setText(tag);
      --->Esto          for(int y = restaurant.getTags().size();y<6;y++){
                    tvTag[y].setVisibility(View.GONE);
                }
            }
        }
    }

And he fixed it for the moment.

    
answered by 27.04.2017 / 15:45
source