2011-04-21 47 views
2

在我的應用程序中,我有一個Spinner,可以填充兩個Array s的Strings,存儲在我的values/strings.xml資源中。根據兩個RadioButton的狀態,選擇正確的Array的值,並且我的Spinner被填充。帶文本和圖標的微調框

對於每個字符串數組,我有一個具有相同大小的圖標數組。我所有的圖標都以「A」開頭,後面跟着一個數字。我無法改變這一點,它像這樣存儲在數據庫中。我在Strings.xml中有一個數組,其中包含繪製圖標所需的所有數字。所有的實際圖標都可以在drawable資源文件夾中找到。

現在我想從Spinner選擇的列表中顯示字符串旁邊的相應項目。當一個項目被選中時,我只需要看到文本。

我已經在互聯網上搜索了一個很好的教程,但是我發現我沒有成功做到這一點。我剛剛開始使用Android編程,所以我認爲我需要一點幫助。我希望有人能指引我走向正確的方向。

我把實際陣列內我的字符串數組,如:

// Arrays with each function in text 
arbeiderjobs = getResources().getStringArray(R.array.arbeiders); 
bediendejobs = getResources().getStringArray(R.array.bediende); 

// Arrays with each function ID 
arbeiderjobsid = getResources().getIntArray(R.array.arbeidersid); 
bediendejobsid = getResources().getIntArray(R.array.bediendeid); 

當單選按鈕中的一個被選中,我用下面的事件處理代碼:

// RadioButton 'Arbeider': If a value gets chosen in this list, 
// the corresponding ID is put in the below variable 
RadioButton rbArbeider = (RadioButton) findViewById(R.id.rbArbeider); 
rbArbeider.setOnCheckedChangeListener(new OnCheckedChangeListener() 
{ 
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) 
    { 
    if (isChecked) 
    { 
     showSpinner(arbeiderjobs, R.id.cmbFuncties); 
     s.setOnItemSelectedListener(new OnItemSelectedListener() 
     { 
     public void onItemSelected(AdapterView<?> parentView, 
            View selectedItemView, int position, long id) 
     { 
      functie = arbeiderjobsid[position]; 
      statuut = 1; 
      visible = false; 
     } 
     }); 

     visible = true; 
    } 
    } // s.setOnItemSelectedListener 

}); // rbArbeider.setOnCheckedChangeListener 

這是我showSpinner方法:

private void showSpinner(String [] jobs, int id) 
{  
    MyCustomAdapter adapter = new MyCustomAdapter 
    (
    FindJob.this, 
    R.layout.spinnerrow, 
    jobs 
); 

    adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); 

    for (int i = 0; i < jobs.length; i++) 
    { 
    adapter.add(jobs[i]); 
    } 

    Spinner s = (Spinner) findViewById(id); 
    s.setClickable(true); 
    s.setAdapter(adapter); 
} 

我製作了一個基於的自定義適配器3210,但我有種在這糾結......我需要一些幫助:

public class MyCustomAdapter extends ArrayAdapter<String> 
{ 

    public MyCustomAdapter(Context context, int textViewResourceId, 
         String[] objects) 
    { 
    super(context, textViewResourceId, objects); 
    } 

    @Override 
    public View getDropDownView(int position, View convertView, ViewGroup parent) 
    { 
    return getCustomView(position, convertView, parent, 
         arbeiderjobs, arbeiderjobsid); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) 
    { 
    return getCustomView(position, convertView, parent, 
         arbeiderjobs, arbeiderjobsid); 
    } 

    public View getCustomView(int position, View convertView, ViewGroup parent,               
          String jobs[], int jobsid[]) 
    { 
    // return super.getView(position, convertView, parent); 

    LayoutInflater inflater = getLayoutInflater(); 
    View row = inflater.inflate(R.layout.spinnerrow, parent, false); 
    TextView label = (TextView) findViewById(R.id.functie); 
    label.setText(jobs[position]); 

    ImageView icon = (ImageView) row.findViewById(R.id.icon); 

    String uri = "@drawable/a" + jobsid[position]; 
    int imageResource = getResources().getIdentifier 
    ( 
     uri, 
     null, 
     getPackageName() 
    ); 
    icon.setImageResource(imageResource); 

    if (visible) 
    { 
     icon.setVisibility(View.GONE); 
    } 

    return row; 
    } 

} 

這是我希望它看起來像:

Result

回答

4

您需要實現自定義適配器併爲列表項目定義自定義佈局。 Check this tutorial

UPDATE

嘗試以下。我沒有測試過代碼,但我認爲它應該可以這樣工作。

public class MyCustomAdapter extends ArrayAdapter<String>{ 

    private String[] mIcons; 

    public MyCustomAdapter(Context context, int textViewResourceId, 
    String[] objects, String[] icons) { 
     super(context, textViewResourceId, objects); 
     mIcons = icons; 
    } 

    @Override 
    public View getDropDownView(int position, View convertView, ViewGroup parent) { 
     return getCustomView(position, convertView, parent); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
     return getCustomView(position, convertView, parent); 
    } 

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

     LayoutInflater inflater=getLayoutInflater(); 
     View row=inflater.inflate(R.layout.spinnerrow, parent, false); 
     TextView label=(TextView) findViewById(R.id.functie); 
     label.setText(getItem(position); 

     ImageView icon=(ImageView)row.findViewById(R.id.icon); 

     String uri = "@drawable/a" + mIcons[position]; 
     int imageResource = getResources().getIdentifier(uri, null, getPackageName()); 
     icon.setImageResource(imageResource); 

     return row; 
    } 
} 
+1

是的,文本和圖標,自定義適配器和幾個getText()和setText()附件方法的線性佈局。還有一個徹底的教程(列表,但是相同的概念)在這裏:http://www.anddev.org/iconified_textlist_-_the_making_of-t97.html – Aleadam 2011-04-21 12:53:25

+0

我已經嘗試過這個教程,但我仍然有點糾結於我的兩個陣列......你會介意幫助我嗎?提前致謝 – Hannelore 2011-04-21 13:12:45

+0

目前有什麼問題?正如我在你看到的編輯問題,你已經在你的適配器中訪問這兩個數組。 label.setText(作業[位置]);和String uri =「@ drawable/a」+ jobsid [position]; – Flo 2011-04-21 14:04:21