我有一個小問題,我的微調不會崩潰應用程序,但同時不會顯示我的自定義適配器。我花了很多年不試圖看問題出在哪裏,但我仍然無法找到它,任何人都可以發現我的錯誤?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" />
我建議重寫'公衆詮釋getCount將()'方法在適配器中,返回'roomType .length' – NSimon
嗨尼古拉斯你能否提供一個例子 – james