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

我建議重寫'公衆詮釋getCount將()'方法在適配器中,返回'roomType .length' – NSimon

+0

嗨尼古拉斯你能否提供一個例子 – james

回答

2

適配器已經有了方法,只需添加一個getCount

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中,您應該傳遞項目列表以填充到構造函數super實現。 這樣做:

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