-1

I've spent hours searching for a solution but everything I've tried doesn't work, it simply yields the same results as the code below does.

I am trying to have an onClickListener change the visibility of a TextView but this code always sets the one I click and then another one below.

class EventiAdapter extends ArrayAdapter<CustomEventi> {

    List<CustomEventi> customEventi;
    LayoutInflater inflater;

    EventiAdapter(@NonNull Context context, @LayoutRes int resource, List<CustomEventi> objects) {
        super(context, resource, objects);

        customEventi = objects;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View view = convertView;
        final EventiViewHolder eventiViewHolder;

        if(view == null) {

            eventiViewHolder = new EventiViewHolder();
            view = inflater.inflate(R.layout.eventi_list, parent, false);

            eventiViewHolder.imageTV = (ImageView) view.findViewById(R.id.image);
            eventiViewHolder.nameTV = (TextView) view.findViewById(R.id.name);
            eventiViewHolder.categoryTV = (TextView) view.findViewById(R.id.category);
            eventiViewHolder.latLngTV = (TextView) view.findViewById(R.id.latLng);
            eventiViewHolder.placeTV = (TextView) view.findViewById(R.id.place);
            eventiViewHolder.dateTV = (TextView) view.findViewById(R.id.date);
            eventiViewHolder.timeTV = (TextView) view.findViewById(R.id.time);;
            eventiViewHolder.viewInfo = (Button) view.findViewById(R.id.viewInfo);
            eventiViewHolder.informationTV = (TextView) view.findViewById(R.id.information);
            eventiViewHolder.mapImage = (ImageView) view.findViewById(R.id.mapImage);

            view.setTag(eventiViewHolder);
        } else {

            eventiViewHolder = (EventiViewHolder) view.getTag();
        }

        eventiViewHolder.viewInfo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if(eventiViewHolder.informationTV.getVisibility() == View.VISIBLE) {

                    eventiViewHolder.informationTV.setVisibility(View.GONE);
                    eventiViewHolder.mapImage.setVisibility(View.GONE);
                    eventiViewHolder.viewInfo.setText("VISUALIZZA INFORMAZIONI");
                }
                else {

                    eventiViewHolder.informationTV.setVisibility(View.VISIBLE);
                    eventiViewHolder.mapImage.setVisibility(View.VISIBLE);

                    eventiViewHolder.viewInfo.setText("NASCONDI INFORMAZIONI");
                }
            }
        });

        System.out.println("POSITION: "+position);
        CustomEventi cstEventi = customEventi.get(position);
        eventiViewHolder.setDataIntoViewHolder(cstEventi);

        return view;
    }
}

Thank you in advance to anyone who'll try to help me.

PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
DomeWTF
  • 2,342
  • 4
  • 33
  • 46
  • 1
    do you mean it your changes just take effect on few items and you want it to be on all items? – Pouya Danesh May 31 '17 at 05:10
  • I mean that when i click the first item in a list of 20, the first does what it has to do, but the Changes happen to the view just below the screen (in my case the third) and then to others below, as if the code wasnt acting only on the clicked item – DomeWTF May 31 '17 at 05:16
  • you just have to reset your adapter . there is no other way 'recyclerView' just binds the view items in the screen and the other items simply are not present at that moment – Pouya Danesh May 31 '17 at 05:22

2 Answers2

1

Try

1. Add a boolean field visible to CustomEventi class

private boolean visible;

public boolean isVisible { return visible; }

public void setVisible(boolean visible) { this.visible = visible; }

2. Replace your adapter code with this one:

class EventiAdapter extends ArrayAdapter<CustomEventi> {

    List<CustomEventi> customEventi;
    LayoutInflater inflater;

    EventiAdapter(@NonNull Context context, @LayoutRes int resource, List<CustomEventi> objects) {
        super(context, resource, objects);

        customEventi = objects;
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

    @NonNull
    @Override
    public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {

        View view = convertView;
        final EventiViewHolder eventiViewHolder;

        System.out.println("POSITION: "+position);
        CustomEventi cstEventi = customEventi.get(position);

        if(view == null) {

            eventiViewHolder = new EventiViewHolder();
            view = inflater.inflate(R.layout.eventi_list, parent, false);

            eventiViewHolder.imageTV = (ImageView) view.findViewById(R.id.image);
            eventiViewHolder.nameTV = (TextView) view.findViewById(R.id.name);
            eventiViewHolder.categoryTV = (TextView) view.findViewById(R.id.category);
            eventiViewHolder.latLngTV = (TextView) view.findViewById(R.id.latLng);
            eventiViewHolder.placeTV = (TextView) view.findViewById(R.id.place);
            eventiViewHolder.dateTV = (TextView) view.findViewById(R.id.date);
            eventiViewHolder.timeTV = (TextView) view.findViewById(R.id.time);;
            eventiViewHolder.viewInfo = (Button) view.findViewById(R.id.viewInfo);
            eventiViewHolder.informationTV = (TextView) view.findViewById(R.id.information);
            eventiViewHolder.mapImage = (ImageView) view.findViewById(R.id.mapImage);

            view.setTag(eventiViewHolder);
        } else {

            eventiViewHolder = (EventiViewHolder) view.getTag();
        }


        eventiViewHolder.informationTV.setVisibility(cstEventi.isVisible() ? View.VISIBLE : GONE);
        eventiViewHolder.mapImage.setVisibility(cstEventi.isVisible() ? View.VISIBLE : View.GONE);
        eventiViewHolder.viewInfo.setText(cstEventi.isVisible() ? "NASCONDI INFORMAZIONI": "VISUALIZZA INFORMAZIONI");

        eventiViewHolder.viewInfo.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                boolean isVisible = customEventi.isVisible();
                customEventi.setVisible(!isVisible);
                notifyDataSetChanged();
            }
        });

        eventiViewHolder.setDataIntoViewHolder(cstEventi);

        return view;
    }
}
PEHLAJ
  • 9,980
  • 9
  • 41
  • 53
  • Thanks, this works great. Is this really the only way to get the right row in this kind of listviews? This seems more like a hack. – DomeWTF May 31 '17 at 12:16
  • Yes, always play with data/entities so that whenever data changes, UI should also update. – PEHLAJ May 31 '17 at 12:23
-2

Try this one....

public View getView(final int position, @Nullable View convertView, @NonNull ViewGroup parent) {

    View view = convertView;


    if(view == null) {


        view = inflater.inflate(R.layout.eventi_list, parent,false);

        imageTV = (ImageView) view.findViewById(R.id.image);
        nameTV = (TextView) view.findViewById(R.id.name);
        categoryTV = (TextView) view.findViewById(R.id.category);
        latLngTV = (TextView) view.findViewById(R.id.latLng);
        placeTV = (TextView) view.findViewById(R.id.place);
        dateTV = (TextView) view.findViewById(R.id.date);
        timeTV = (TextView) view.findViewById(R.id.time);;
        viewInfo = (Button) view.findViewById(R.id.viewInfo);
        informationTV = (TextView) view.findViewById(R.id.information);
        mapImage = (ImageView) view.findViewById(R.id.mapImage);



    viewInfo.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {

            if(informationTV.getVisibility() == View.VISIBLE) {

                informationTV.setVisibility(View.GONE);
                mapImage.setVisibility(View.GONE);
                viewInfo.setText("VISUALIZZA INFORMAZIONI");
            }
            else {

                informationTV.setVisibility(View.VISIBLE);
                mapImage.setVisibility(View.VISIBLE);

                viewInfo.setText("NASCONDI INFORMAZIONI");
            }
        }
    });

    System.out.println("POSITION: "+position);
    CustomEventi cstEventi = customEventi.get(position);
    setDataIntoViewHolder(cstEventi);

    return view;
}
Kiran Koravi
  • 164
  • 2
  • 11