What have I done wrong here? Think I need another set of eyeballs as I've been looking at it too long...
I have a custom spinner adapter that shows an image and text to select an attitude. I'm pulling both the string for the text and the icons from the resources I have. The text is correct, but the images are not just out of place, but sometimes completely crazy - not even in the set of images I select.
public View getCustomView(int position, View convertView, ViewGroup parent) {
String[] attitude = new String[]{
this.context.getResources().getString(R.string.happy),
this.context.getResources().getString(R.string.sad),
this.context.getResources().get开发者_开发技巧String(R.string.angry),
this.context.getResources().getString(R.string.difficult),
this.context.getResources().getString(R.string.hurt),
this.context.getResources().getString(R.string.confused),
this.context.getResources().getString(R.string.stubborn)};
LayoutInflater inflater=(LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row=inflater.inflate(R.layout.spinner_row, parent, false);
TextView label=(TextView)row.findViewById(R.id.attitude_label);
label.setText(attitude[position]);
ImageView icon=(ImageView)row.findViewById(R.id.emoticon);
switch (position) {
case 0:
icon.setImageResource(R.drawable.happy);
break;
case 1:
icon.setImageResource(R.drawable.sad);
break;
case 2:
icon.setImageResource(R.drawable.angry);
break;
case 3:
icon.setImageResource(R.drawable.difficult);
break;
case 4:
icon.setImageResource(R.drawable.hurt);
break;
case 5:
icon.setImageResource(R.drawable.confused);
break;
case 6:
icon.setImageResource(R.drawable.stubborn);
break;
}
return row;
}
the picture below shows it all - the text is correct, but the images are not. Happy is clearly sad, and what should be sad is another image resource I use for importing data. Angry and difficult and confused are fine, but hurt and stubborn are completely different resources, too.
What the biff? I suspect I've done something trivial and just can't see it, so please kick me.
Hello may be its helpful to you. pls try this.
public class ViewHolder {
public TextView label;
public ImageView icon;
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
ViewHolder holder;
if (convertView == null) {
vi = inflater.inflate(R.layout.spinner_row, null);
holder = new ViewHolder();
holder.label = (TextView) vi.findViewById(R.id.attitude_label);
holder.icon = (ImageView) vi.findViewById(R.id.emoticon);
vi.setTag(holder);
} else
holder = (ViewHolder) vi.getTag();
holder.label.setText(attitude[position]);
switch (position) {
case 0:
holder.icon.setImageResource(R.drawable.happy);
break;
case 1:
holder.icon.setImageResource(R.drawable.sad);
break;
case 2:
holder.icon.setImageResource(R.drawable.angry);
break;
case 3:
holder.icon.setImageResource(R.drawable.difficult);
break;
case 4:
holder.icon.setImageResource(R.drawable.hurt);
break;
case 5:
holder.icon.setImageResource(R.drawable.confused);
break;
case 6:
holder.icon.setImageResource(R.drawable.stubborn);
break;
}
return vi;
}
Is it possible that you needed to rebuild the project from scratch - in eclipse that means using project->clean?
精彩评论