2
I’m populating my spinner through an Arrayadapter but now to display it always shows the same image for all items. Can anyone explain where the bug is? Here’s my mainactivity
public class MainActivity extends FragmentActivity {
GoogleMap mGoogleMap;
GPSTracker gps;
AlertDialogManager alert = new AlertDialogManager();
Spinner mSprPlaceType;
Button mBtnFind=null;
TextView textview=null;
Place[] mPlaces = null;
String[] mPlaceType=null;
String[] mPlaceTypeName=null;
LatLng mLocation=null;
HashMap<String, Place> mHMReference = new HashMap<String, Place>();
private int arr_images[] = { R.drawable.aeroporto_img,
R.drawable.pe_img, R.drawable.atm_img,
R.drawable.banco, R.drawable.autocarros, R.drawable.igreja,
R.drawable.hospital, R.drawable.cinema, R.drawable.restaurante,
R.drawable.escola, R.drawable.museu, R.drawable.cafe, R.drawable.bar };
private static final float UNDEFINED_COLOR = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPlaceType = getResources().getStringArray(R.array.place_type);
mPlaceTypeName = getResources().getStringArray(R.array.place_type_name);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this, R.layout.mostra_spinner, R.id.mPlaceType, mPlaceTypeName);
mSprPlaceType = (Spinner) findViewById(R.id.spr_place_type);
mSprPlaceType.setAdapter(adapter);
mBtnFind = ( Button ) findViewById(R.id.btn_find);
class MyAdapter extends ArrayAdapter<String>{
public MyAdapter(Context context, int textViewResourceId, String[] objects) {
super(context, textViewResourceId, objects);
}
@Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=getLayoutInflater();
View row=inflater.inflate(R.layout.mostra_spinner, parent, false);
TextView label=(TextView)row.findViewById(R.id.mPlaceType);
if(position==0){
// Default selected Spinner item
label.setText("Please select company");
}
else
{
ImageView icon=(ImageView)row.findViewById(R.id.image);
icon.setImageResource(arr_images[position]);
}
return row;
}
}
Here’s my activity_main.xml
<Spinner
android:id="@+id/spr_place_type"
android:layout_toRightOf="@+id/image"
android:layout_width="240dp"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:prompt="@string/prompt" />
And my show_description.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3dip"
>
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/autocarros"/>
<TextView
android:layout_toRightOf="@+id/image"
android:layout_marginTop="2dip"
android:textColor="@drawable/black"
android:textStyle="bold"
android:id="@+id/mPlaceType"
android:layout_marginLeft="5dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
I appreciate the help
Missing the content of
R.array.place_type_name
. Are you sure this array has all the place type you expect?– Tulio F.
The contents of R.array.place_type_name is in the "values"->"strings" folder. But only the drawable that is defined in "my_description.xml" appears in Imageview and this image appears for all spinner items...
– Dan_AD