我有一个小问题,我的微调器不会使应用程序崩溃,但同时不会在其中显示我的自定义适配器。我花了很多年没有试图找出问题所在,但我似乎仍然无法找到它,有人能发现我的错误吗?
我的适配器
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 :(得分: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 :(得分: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; }