0
My app has a Listview, in which you receive information from a Json (URL), and each line has an Imageview that when you click, the line information will be added as "Favorite".
For this, I created two images (iconfav_off and iconfav_on),
So I created the layout by getting the iconfav_off as a starter.
<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 clicking on this specific object (not in the Listview line), I need it to be changed 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 (whether off or on) and performs the exchange.
However MY DIFFICULTY is being that the switch is happening on several lines, when it should be happening only on the object of the clicked line.
I tried it this 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 clicked line, but I don’t know how to set the image of the object of that position, and in this case, I lose the "Tag" control "off" or "on".
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 anyone can help me, in advance I’d appreciate it
UPDATE
Below is the complete 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;
}
I believe we would need to see the code of
Adapter
. I do not recommend making changes only inView
, because theListView
uses a recycling standard ofViews
, if you do not treat the attributeFavStatus
of the model in itsAdapter,
will not have much solution. My recommendation is to leave for theRecyclerView
, because you won’t need to treatView
outside theAdapter
.– Wakim
@Wakim, I put an Adapter code update. How would I use Recyclerview in my code? Is it really the case to use Recyclerview ? What is the use of this function ?
– Leandro Santana
So, as its layout is very simple, it is convenient to keep using Listview. But for more complex layouts it is recommended to make use of it instead of Listview. Getting back to the problem, you need to update your Imageview also within getView of your Adapter, based on the Agenda item. For this you need to update the Agenda item instead of adding a View Tag. If you can’t, I can give you an answer early tomorrow.
– Wakim