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;
}