2

私のスピナーがアプリケーションをクラッシュさせないと同時に、自分のカスタムアダプターを表示しない小さな問題があります。私は年齢を過ごして、どこに問題があるのか​​を見出そうとしなかったが、まだそれを見つけることができなかった。Androidカスタムスピナーアダプターのコンテンツが表示されない

マイアダプタ

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; 

} 


} 

主な活動

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); 

カスタム部屋スピナー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> 

コンテンツメイン

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

私は 'roomTypeを返す、あなたのアダプタで'公共のint同様にgetCount() 'メソッドをオーバーライドお勧めする は、これを行います.length' – NSimon

+0

こんにちはニコラスは、例を提供することができます – james

答えて

2

あなたのアダプタがすでにメソッドを持って、単にgetCount 1を追加します:

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

完璧な答えは非常にありがとう – james

0

カスタムarrayAdapterの内部には、スーパー実装のコンストラクタに移入するアイテムのリストを渡す必要があります。

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; } 
+0

別の華麗な答え – james

関連する問題