I have been working pretty much with ListViews and ExpandableListViews in android. And I know how to populate the listviews with images and text by making one customAdaper - and adapterclass extending for instance Arrayadapter.
In this app I am developing now I am using a Spinner togehter with an ArrayAdapter. In the subclass of ArrayAdapter I am overriding the getView-method to populate the views with text and images.
But the problem is that the getView is only called once - for the first listitem showed when app starts and when selecting an item from the list. Here it seem to work. But when I expanding the list through the list the getView-method is NOT called - Only the names from the string is put in the list
String[] string = {"ssun", "venus", "earth", "march"};
What I want is to have full control over the views so I can populate the whole list with my own icons, text and so on - just what used to do with ListView and Expandable ListView objects.
I know I am doing something wrong here and I can see the logic that only the first/selected item is called by the getView-method. But I want all Views to be called so I have a nice list with imageicons and manipulated text.
In the class that extends Activity
Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
String[] string = {"sun", "venus", "earth", "march"};
MyArrayAdapter adapter = new MyArrayAdapter(this, string);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
And the CustomAdapter
public class MyArrayAdapter extends ArrayAdapter <String> {
private final String[] values;
private final Context context;
public MyArrayAdapter(Context context, String[] values) {
super (context, R.layout.celestial_list, values);
this.values = values;
this.context = context;
}
private static class ViewHolder {
TextView textView;
ImageView imageView;
ImageView infoView;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = null;
ViewHolder viewHolder;
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.celestial_list, parent, false);
viewHolder = new ViewHolder();
viewHolder.textView = (TextView) view.findViewById(R.id.label);
viewHolder.imageView = (ImageView) view.findViewById(R.id.icon);
view.setTag(viewHolder);
} else {
view = convertView;
viewHolder = (ViewHolder) view.getTag();
}
System.out.println("view = " + view + " för position " + position);
viewHolder.textView.setText(values[position]);
switch (position) {
case 0:
viewHolder.imageView.setImageResource(R.drawable.sun);
break;
case 1:
viewHolder.imageView.setImageResource(R.drawable.sun);
break;
case 2:
viewHolder.imageView.setImageResource(R.drawable.sun);
break;
case 3:
viewHolder.imageView.setImageResource(R.drawable.sun);
break;
}
return view;
}
}
Screenshot 1. It seems ok - getView is called ...
Screenshot 2 but only for the selected listobject - not for the rest. Only the String[] is used here