民间。
我正在尝试使用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);
答案 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)
{
}
处理微调器的选择