2016-01-20 70 views
0

我的自定義適配器微調元件有問題。 Spinner從我的適配器中顯示一個列表,但是當我嘗試選擇項目時它不起作用。Android微調控制器適配器選擇無法使用自定義BaseAdapter

這裏是我的適配器代碼:

public class CategoryAdapter extends BaseAdapter{ 

    private final ArrayList<String> categories; 
    private Context context; 
    LayoutInflater inflater; 


    public CategoryAdapter(Context context, ArrayList catList){ 
     this.context = context; 
     inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
     categories = catList; 
    } 

    @Override 
    public int getCount() { 
     return categories.size(); 
    } 

    @Override 
    public String getItem(int position) { 
     return categories.get(position); 
    } 

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

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     if(convertView == null) { 
      convertView = inflater.inflate(android.R.layout.simple_spinner_item, null); 
     } 
     TextView textView = (TextView) convertView.findViewById(android.R.id.text1); 
     textView.setTextColor(Color.RED); 
     textView.setText(getItem(position)); 

     return convertView; 
    } 
} 

我在做什麼錯?

+1

define:*但是當我嘗試選擇它不工作*在問題 – Selvin

+0

@Se lvin,當我運行我的應用程序時,微調器沒有選擇任何項目,當我嘗試從微調列表中選擇一個時,不會有任何更改。當我嘗試使用方法spinner.getSelected()時,它返回null。 –

+0

'getSelected()'微調沒有這樣的方法也適配器實現看起來大體上好的...所以問題不在於此代碼... – Selvin

回答

0

,如果你想在微調出現在屏幕預選的任何項目,您可以使用

spinner.setSelection(position); 

,如果你想獲得任何項目的位置,然後用下面的代碼,

position = arrayList.indexOf("your item"); 

menual方式,

spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { 
     @Override 
     public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { 
      spinner.setSelection(i); 
     } 

     @Override 
     public void onNothingSelected(AdapterView<?> adapterView) { 

     } 
    }); 
相關問題