Reputation: 945
I have a customAdapter with a viewHolder which contains a customView(circle shape) I need to retrieve the color of the view which is contained in the view holder and not the Object item
How can i do this ? Thank you very much
My adapter:
public class WelcomeAdapter extends BaseAdapter {
// contex
private Context context;
// Liste a affichée
private RealmList<Measure> welcomeList;
public RealmList<WelcomeList> getWelcomeList() {
return welcomeList;
}
private LayoutInflater inflater = null;
private int color1;
private int color2;
private int color3;
public WelcomeAdapter (Context context, RealmList<Welcome> welcomeList) {
this.context = context;
this.welcomeList= welcomeList;
color1= ContextCompat.getColor(context, R.color.green);
color2= ContextCompat.getColor(context, R.color.yellow);
color3= ContextCompat.getColor(context, R.color.blue);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return welcomeList== null ? 0 : welcomeList.size();
}
@Override
public Object getItem(int position) {
return welcomeList== null ? null : welcomeList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
if (convertView == null) {
convertView = inflater.inflate(R.layout.row_welcome, parent, false);
viewHolder = new ViewHolder();
viewHolder.cs = (CircleShape) convertView.findViewById(R.id.row_welcome_cv);
convertView.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) convertView.getTag();
}
Welcome welcome= WelcomeList.get(position);
if (welcome.getWelcomePeople() != null) {
for (People p : welcome.getWelcomePeople()) {
if (p.getNumber() > 5) {
viewHolder.cs.setColor(color1);
} else if (p.getNumber() > 15) {
viewHolder.cs.setColor(color2);
} else if (p.getNumber() > 25) {
viewHolder.cs.setColor(color3);
}
}
}
return convertView;
}
class ViewHolder {
CircleShape cs;
}
The activity
protected AdapterView.OnItemClickListener welcomeClickListener = new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// need to retrieve the current color of the CircleShape of the view holder associated with the item clicked there
}
};
Thank you very much
Upvotes: 2
Views: 868
Reputation: 5146
If I'm not mistaken, you should be able to retrieve the view holder by calling getTag()
on the view being returned to you, since you are setting the tag to the view holder, using convertView.setTag(viewHolder)
Therefore, your onItemClick
method to get the CircleShape
should look as follows:
ViewHolder vh = (ViewHolder) view.getTag();
CircleShape cs = vh.cs;
Let me know if that works, I'm a bit rusty with adapters.
Upvotes: 3