如何在Android中自定义微调器

时间:2015-11-25 08:25:47

标签: android android-spinner android-adapter

我需要在Android中自定义微调器。
我有我的简单适配器:

public class MonthsSpinnerAdapter extends BaseAdapter {

private Activity context;
private String [] itemList;

public MonthsSpinnerAdapter(Activity context,String [] monthArray) {
    this.context=context;
    this.itemList=monthArray;
}

@Override
public int getCount() {
    return 0;
}

@Override
public Object getItem(int position) {
    return itemList[position];
}

@Override
public long getItemId(int position) {
    return position;
}

public View getView(int position, View convertView, ViewGroup parent) {
    LayoutInflater inflater = context.getLayoutInflater();
    View row = inflater.inflate(R.layout.spinner_item, parent,
            false);
    RobotoTextView make = (RobotoTextView) row.findViewById(R.id.spinner_title);
    make.setText(itemList[position]);
    Log.d("SpinnerA", "getView");
    return row;
}


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

    LayoutInflater inflater = context.getLayoutInflater();
    View row = inflater.inflate(R.layout.spinner_dropdown_item, parent,
            false);
    RobotoTextView month = (RobotoTextView) row.findViewById(R.id.spinner_month);
    month.setText(itemList[position]);
    RobotoTextView year = (RobotoTextView) row.findViewById(R.id.spinner_year);
    year.setText(context.getString(R.string.year));
    Log.d("SpinnerA", "getDropDownView");
    return row;
}

}

但是当我把它添加到我的微调器中并且向上运行时,我在旋转器中看不到任何东西。

MonthsSpinnerAdapter adapter = new MonthsSpinnerAdapter(GeneralActivity.this
                                , getResources().getStringArray(R.array.month_list));
spinner.setAdapter(adapter);

我认为它会很好用,但我不知道哪里有错误。这是我在运行应用程序时看到的示例: enter image description here
在这张照片中,我需要的是:enter image description here

如果您需要,这是我的xml文件:

<com.devspark.robototextview.widget.RobotoTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    app:typeface="roboto_medium"
    android:padding="5dp"
    android:textColor="#fff"
    android:id="@+id/spinner_title"
    android:text="month"
    android:background="@color/primary_color"
    xmlns:android="http://schemas.android.com/apk/res/android" />

DropDown布局:

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto" >

<com.devspark.robototextview.widget.RobotoTextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:typeface="roboto_medium"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="12dp"
    android:id="@+id/spinner_month"
    android:text="month"
    android:textColor="#545454"
    android:background="#fafafa" />

    <com.devspark.robototextview.widget.RobotoTextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:typeface="roboto_regular"
        android:layout_marginTop="16dp"
        android:layout_marginRight="12dp"
        android:layout_alignParentRight="true"
        android:text="year"
        android:id="@+id/spinner_year"
        />

</RelativeLayout>

请帮我解决我的问题

1 个答案:

答案 0 :(得分:1)

尝试在适配器的getCount函数中返回itemList.length。

@Override
public int getCount() {
    return itemList.length;
}