1

伙计们。

我正在尝试在 android 中使用自定义微调器实现动态图像。一切顺利,除了我的情况没有显示图像,它仅作为我的布局中的默认图像出现。谁能指出我的错误是什么?下面是我的代码。

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();

String[] headers2 = new 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);
4

1 回答 1

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) 
{


    }

处理微调器的选择

于 2013-07-31T23:57:07.327 回答