Подтвердить что ты не робот

Пользовательский адаптер Spinner

Я хотел применить собственный шрифт к моему счетчику. Единственный способ узнать, что создать пользовательский адаптер. Вот мой код

    private class CustomAdapter extends ArrayAdapter {

    private Context context;
    private List<CharSequence> itemList;
    public CustomAdapter(Context context, int textViewResourceId,List<CharSequence> itemList) {

        super(context, textViewResourceId);
        this.context=context;
        this.itemList=itemList;
    }

    public TextView getView(int position, View convertView, ViewGroup parent) {

        TextView v = (TextView) super
                .getView(position, convertView, parent);
        Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(),
                "fonts/gilsanslight.otf");
        v.setTypeface(myTypeFace);
        v.setText(itemList.get(position));
        return v;
    }

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

        TextView v = (TextView) super
                .getView(position, convertView, parent);
        Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(),
                "fonts/gilsanslight.otf");
        v.setTypeface(myTypeFace);
        v.setText(itemList.get(position));
        return v;
    }

}

Затем я использую

List<CharSequence> itemList = new ArrayList<CharSequence>(
            Arrays.asList(items));

    mySpinnerArrayAdapter = new   CustomAdapter(context,android.R.layout.simple_spinner_item,itemList); 
    spinner.setAdapter(mySpinnerArrayAdapter);

После этого мой адаптер пуст. Кто-нибудь может мне помочь, пожалуйста? Элементы содержат список стран.

С уважением,

4b9b3361

Ответ 1

Try

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

    LayoutInflater inflater = getLayoutInflater();
            View row = inflater.inflate(yourRowlayout, parent,
                    false);
       TextView make = (TextView) row.findViewById(R.id.Make);
        Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(),
                "fonts/gilsanslight.otf");
        v.setTypeface(myTypeFace);
        v.setText(itemList.get(position));
        return row;
    }


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

        LayoutInflater inflater = getLayoutInflater();
                View row = inflater.inflate(yourRowlayout, parent,
                        false);
           TextView make = (TextView) row.findViewById(R.id.Make);
            Typeface myTypeFace = Typeface.createFromAsset(context.getAssets(),
                    "fonts/gilsanslight.otf");
            v.setTypeface(myTypeFace);
            v.setText(itemList.get(position));
            return row;
        }

Ответ 2

Попробуйте это

Внутри файла макета:

<Spinner
     android:id="@+id/spinnerview"
     android:layout_width="180dp"
     android:layout_height="42dp"
     android:layout_marginLeft="105dp"
     android:layout_marginTop="45dp"
     android:background="@drawable/spinner_back"
     android:paddingLeft="5dp"
     android:spinnerMode="dropdown"
     android:visibility="visible" />

внутри string.xml:

 <string-array name="spinner_array_environtment">
        <item>Test</item>
        <item>Production</item>
 </string-array>

внутри вас Файл Java в onCreate() Метод:

spinner_environment = (Spinner) findViewById(R.id.spinnerview);
            adapter = ArrayAdapter.createFromResource(this,
                    R.array.spinner_array_environtment, R.layout.spinner);
            adapter.setDropDownViewResource(R.layout.spinner);
            spinner_environment.setAdapter(adapter);

Сделайте новый spinner.xml файл в папку макета:

внутри spinner.xml файла:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/spinnerTarget"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:textSize="25dp"
    android:textColor="#4C4646" />

Вот оно!!!

Ответ 3

передать элемент itemList в качестве параметра в конструкторе суперкласса

public CustomAdapter(Context context, int textViewResourceId,List<CharSequence> itemList) {

    super(context, textViewResourceId, itemList);
    this.context=context;
    this.itemList=itemList;
}

Ответ 4

Самый простой, я думаю:)

List<String> listOfItems = getListOfItems(); // returns ArrayList<String>

ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, listOfItems);

targetSpinner.setAdapter(spinnerAdapter);

Хорошо, просто поместить список строк в счетчик не должен заставлять нас реализовывать Адаптер. Я думаю, что кодовое пятно и получение сумасшедшего шаблона.

Trick - это простой_spinner_item id - damn, мне нравится механизм R.id, но это не интуитивно понятно из документов.

Ответ 5

это сработало для меня (ussing android.R.layout. simple_spinner_dropdown_item):

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    CheckedTextView checkedTextView = (CheckedTextView) super.getView(position, convertView, parent);
    checkedTextView.setText(itemList.get(position));
    return checkedTextView;
}

Я считаю это лучшим решением, потому что вы не раздуваете несколько раз.