setOnClickListener Adapter on an object in the row of a ListView

0

My app has a ListView, in which it receives information from a Json (URL), and each line has an ImageView that when clicked, line information will be added as "Favorite".

For this, I created two images (iconfav_off and iconfav_on),

Then I created the layout by getting iconfav_off as the initial.

<ImageView
    android:layout_width="30dp"
    android:src="@drawable/iconfav_off"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_height="30dp"
    android:id="@+id/starFav"
    android:tag="off"/>

When I click on this specific object (not on the ListView line), I need to use it to change to "iconfav_on".

For this, I created a setOnClickListener for the object inside my Adapter

    ImageView Fav = (ImageView) convertView.findViewById(R.id.starFav);
    Fav.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            FavStatus = String.valueOf(view.getTag());
            if(FavStatus.equals("off")){
                Toast.makeText(getContext(), "ADICIONADO AOS FAVORITOS", Toast.LENGTH_SHORT).show();
                view.setBackgroundResource(R.drawable.iconfav_on);
                view.setTag("on");
            }
            if(FavStatus.equals("on")){
                Toast.makeText(getContext(), "REMOVIDO DOS FAVORITOS", Toast.LENGTH_SHORT).show();
                view.setBackgroundResource(R.drawable.iconfav_off);
                view.setTag("off");
            }

        }
    });

It checks the tag (if off or on) and makes the switch.

But MY DIFFICULTY is that the exchange is happening on multiple lines, when it should happen only on the object of the clicked line.

I tried the following way and the result was the same

    ImageView Fav = (ImageView) convertView.findViewById(R.id.starFav);
    Fav.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            ImageView FavStatus = (ImageView) ((View) view.getParent()).findViewById(R.id.starFav);
            FavStatus.setBackgroundResource(R.drawable.iconfav_on);
        }
    });

I also adjusted the code to get the "position" of the line clicked, but I do not know how to set the image of the object of that position, in which case I lose the "on" control tag.

    ImageView Fav = (ImageView) convertView.findViewById(R.id.starFav);
    Fav.setTag(position);
    Fav.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            View parentRow = (View) view.getParent();
            ListView listView = (ListView) parentRow.getParent();
            final int position = listView.getPositionForView(parentRow);

            Toast.makeText(getContext(), "botão da posição: " + view.getTag(), Toast.LENGTH_LONG).show();

            ?????

        }
    });

If someone can help me, I'll thank you in advance.

UPDATE

Below is the full Adapter

class AgendaAdapter extends ArrayAdapter<Agenda> {

AgendaAdapter(Context context, ArrayList<Agenda> agenda) {
    super(context, 0, agenda);
}

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

    final Agenda agenda = getItem(position);

    if (convertView == null) {
        convertView = LayoutInflater.from(getContext()).inflate(R.layout.agenda_linha, parent, false);
    }

    TextView tvTitulo = (TextView) convertView.findViewById(R.id.clubeAgendaTitulo);
    TextView tvData = (TextView) convertView.findViewById(R.id.clubeAgendaData);
    TextView tvHora = (TextView) convertView.findViewById(R.id.clubeAgendaHora);

    assert agenda != null;

    tvTitulo.setText(agenda.Nome);
    tvData.setText(agenda.Data);
    tvHora.setText(agenda.Hora);


/***********************************************/
/******TRECHOS DAS TENTATIVAS ANTERIORES *******/
/***********************************************/

    return convertView;
}

RESOLVED IN ANSWER

    
asked by anonymous 22.02.2017 / 02:35

1 answer

2

For those who had the same doubt as me, just as they helped me, I will leave below the code that solved my problem, so that I can help others.

I was able to resolve, from the orientation of @Wakim, in the comments:

  

... you need to update your ImageView also within the getView of your Adapter, based on the Agenda item. For this you need to update Agenda item instead of adding a Tag in the View.

As a beginner, I do not know if the code is written properly, but it worked.

In front of the question, the code snippet below, enters the commented space "TRENDS OF THE PREVIOUS TRIES" of the Adapter.

    ImageView Fav = (ImageView) convertView.findViewById(R.id.starFav);
    Fav.setBackgroundResource(R.drawable.iconfav_off);
    if(agenda.Fav.equals("on")){ Fav.setBackgroundResource(R.drawable.iconfav_on); }

    Fav.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            String FavSatus = agenda.Fav;
            if(FavSatus.equals("off")){
                Toast.makeText(getContext(), "ADICIONADO AOS FAVORITOS", Toast.LENGTH_SHORT).show();
                view.setBackgroundResource(R.drawable.iconfav_on);
                agenda.Fav = "on";
            }
            if(FavSatus.equals("on")){
                Toast.makeText(getContext(), "REMOVIDO DOS FAVORITOS", Toast.LENGTH_SHORT).show();
                view.setBackgroundResource(R.drawable.iconfav_off);
                agenda.Fav = "off";
            }
        }
    });

By clicking on the Favorite button, I change the image of the object, and set the value of agenda.Fav.

To correct the problem that the object was being changed in the other lines, what I did was to set the "off" by default for all items in the list, and change to "on" if my Agenda.Fav item was "on";

Fav.setBackgroundResource(R.drawable.iconfav_off);
if(agenda.Fav.equals("on")){ Fav.setBackgroundResource(R.drawable.iconfav_on); }

Now the default value and checking of each item will happen whenever the item is displayed / unrecognized when scrolling in the ListView.

Once set and explained, it may seem obvious the code, but for anyone who is starting in Java / Android (as I am), it could be detrimental to a few hours of work.

Thanks again to @Wakim for guidance.

    
22.02.2017 / 05:28