2013-07-31 72 views
1

Folks。Spinner圖像未顯示

我正試圖在android中使用自定義微調器來實現動態圖像。一切都很順利excite圖像不顯示與我的條件,它只顯示爲我的佈局中的默認圖像。任何人都可以指出我的錯誤是什麼?以下是我的代碼。

public class CustomSpinnerAdapter extends SimpleCursorAdapter { 

int layoutn; 
LayoutInflater mInflater; 

private final Cursor mCursor; 
private final int mLayout; 
private final LayoutInflater mLayoutInflater; 
private final Context mContext; 


int arr_images[] = { R.drawable.us, 
     R.drawable.uk, R.drawable.eur, 
     R.drawable.cn, R.drawable.ml, R.drawable.mr}; 

public CustomSpinnerAdapter (Context context, int layout, Cursor c, 
     String[] from, int[] to) { 

    super(context, R.layout.spinnertext, c, from, to); 

    this.mContext = context; 
    this.mCursor = c; 
    this.mLayout = layout; 
    this.mLayoutInflater = LayoutInflater.from(context); 

} 

private final class ViewHolder { 

    public TextView Title; 
    public ImageView flag; 

} 

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



     final ViewHolder viewHolder; 

     if (convertView == null) { 


      convertView = mLayoutInflater.inflate(mLayout, parent, false); 

      viewHolder = new ViewHolder(); 



      viewHolder.Title = (TextView) convertView 
        .findViewById(R.id.currencytitle); 

      viewHolder.flag = (ImageView) convertView 
        .findViewById(R.id.imageView1); 

     } 

     else { 

      viewHolder = (ViewHolder) convertView.getTag(); 

     } 




     viewHolder.flag.setImageResource(arr_images[position]); 

    return convertView; 
} 

}

編輯:我的監聽器代碼和光標:

mDb = mHelper.getWritableDatabase(); 

字符串[] headers2 =新的String [] {MyDbHelper.COL_Currfirst,MyDbHelper.COL_Titleone};

 cursl = mDb.query(MyDbHelper.TABLE_NAME, headers2, MyDbHelper.COL_Currsecond + "=" + "?", 
      new String[] { "EUR" }, null, null, null); 






    adapters1 = new CustomSpinnerAdapter(this, R.layout.spinnertext, cursl, 
      headers2, new int[] { R.id.currencytitle , R.id.titlesub}); 



    fromc.setAdapter(adapters1); 
    toc.setAdapter(adapters1); 
+0

當我在我的微調列表中選擇任何項目的標誌是正確顯示在微調的小縮略圖,但不是在開放的微調列表!任何線索? –

+0

你可以發佈的代碼,你聽的微調 – JRowan

+0

當然,我已經添加到我的主代碼 –

回答

0

row.xml在佈局文件夾

<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:orientation="vertical" 
android:padding="3dip" 
> 
    <ImageView 
     android:id="@+id/image" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     /> 
    <TextView 
     android:layout_toRightOf="@+id/image" 
     android:padding="3dip" 
     android:layout_marginTop="2dip" 
     android:textStyle="bold" 
     android:textColor="#000000" 
     android:id="@+id/company" 
     android:text="CoderzHeaven" 
     android:layout_marginLeft="5dip" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content"/> 


</RelativeLayout> 

然後進行微調的一個.xml佈局並在活動中使用它來填充他們\

public class MyAdapter extends ArrayAdapter<String>{ 

    public MyAdapter(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); 
    } 

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


int arr_images[] = { R.drawable.us, 
    R.drawable.uk, R.drawable.eur, 
    R.drawable.cn, R.drawable.ml, R.drawable.mr}; 
public View getCustomView(int position, View convertView, ViewGroup parent) { 

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

    ImageView icon=(ImageView)row.findViewById(R.id.image); 
    icon.setImageResource(arr_images[position]); 

    return row; 
    } 
} 
在OnCreate中

( )像這樣使用它

Spinner spin; 
spin = (Spinner)findViewById(R.id.iconspin);//or whatever the id of your spinner is 
String[] strings = new String[] {MyDbHelper.COL_Currfirst ,MyDbHelper.COL_Titleone}; 
spin.setAdapter(new MyAdapter(MainActivity.this, R.layout.row, strings)); 
spin.setOnItemSelectedListener(this); 

,讓您的活動

implement OnItemSelectedListener 

,那麼你可以重寫此方法

@Override 
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, 
     long arg3) 
{ 


    } 

應對微調的選擇

+0

謝謝..我會嘗試並且會讓你知道。 –

+0

感謝您的幫助...我試圖排除故障,直到我得到了一個工作場景...我會發布它爲所有人提供幫助! –

+0

沒問題,很高興你得到它的工作 – JRowan