james
james

Reputation: 303

Android custom spinner adapter not displaying content

I have a small problem where my spinner doesn't crash the application but at the same time doesn't display my custom adapter inside of it. I have spent ages not trying to look where the problem is but I still cannot seem to find it, can anyone spot my error?

My Adapter

public class AccomdationAdapter extends ArrayAdapter <String> {

Context c;
String[] roomType;
int[] images;





public AccomdationAdapter(Context ctx, String[] roomType, int[] images) {

    super(ctx, R.layout.custom_room_spinner);
    this.c=ctx;
    this.roomType=roomType;
    this.images=images;






}

@Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {


    if(convertView==null){


        LayoutInflater inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.custom_room_spinner,null);


    }

    TextView roomTypes = (TextView) convertView.findViewById(R.id.txtRoom);
    ImageView roomIcon = (ImageView) convertView.findViewById(R.id.imgRoom);

    //set data
    roomTypes.setText(roomType[position]);
    roomIcon.setImageResource(images[position]);

    return convertView;

}


@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if(convertView==null){


        LayoutInflater inflater = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.custom_room_spinner,null);


    }

    TextView roomTypes = (TextView) convertView.findViewById(R.id.txtRoom);
    ImageView roomIcon = (ImageView) convertView.findViewById(R.id.imgRoom);

    //set data
    roomTypes.setText(roomType[position]);
    roomIcon.setImageResource(images[position]);

    return convertView;

}


}

Main Activity

Spinner roomTypeSpinner;

String[]roomType = {"Single", "Double"};
int[] roomimages = {R.drawable.ic_menu_camera, R.drawable.ic_menu_gallery};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    roomTypeSpinner = (Spinner) findViewById(R.id.spinner);


    AccomdationAdapter adapter = new AccomdationAdapter(this, roomType, roomimages);
    roomTypeSpinner.setAdapter(adapter);

Custom room spinner xml

    <ImageView
    android:layout_width="50dp"
    android:layout_height="50dp"
    android:id="@+id/imgRoom"
    android:background="@drawable/abc_ic_star_black_16dp" />

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceMedium"
    android:text="Medium Text"
    android:id="@+id/txtRoom"
    android:layout_alignParentTop="true"
    android:layout_toEndOf="@+id/imgRoom"
    android:textSize="50dp" />
  </RelativeLayout>

Content main

<Spinner
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/spinner" />

Upvotes: 2

Views: 1115

Answers (2)

NSimon
NSimon

Reputation: 5287

Your adapter already has methods, simply add the getCount one :

public class AccomdationAdapter extends ArrayAdapter <String> {
  Context c;
  String[] roomType;
  int[] images;
  @Override
  public int getCount() {
    return (roomType == null ? 0 : roomType.length);
  }

Upvotes: 2

NezSpencer
NezSpencer

Reputation: 670

Inside your custom arrayAdapter, you should pass the list of items to populate to the constructor super implementation. Do this:

public AccomdationAdapter(Context ctx, String[] roomType, int[] images) {

super(ctx, R.layout.custom_room_spinner,roomType);
this.c=ctx;
this.roomType=roomType;
this.images=images; }

Upvotes: 0

Related Questions