我是 android 的初学者....我需要你的帮助。
在我的应用程序中,我使用自定义适配器和自定义行布局创建了一个列表视图。我成功地这样做了,我在所有行中添加了一个图像、文本视图和一个微调器下拉菜单。在下拉列表中有项目数(例如从 0 到 9)
但我的问题是,当我选择下拉菜单并设置一些值时,在滚动列表时,微调器会将自身重置为初始值(此处为 0)。我为微调器使用值 0 到 9 的字符串。
我能做些什么来避免这个问题?任何帮助将不胜感激....提前致谢。
//This is my custom adapter code....
public class MyProductsArrayAdapter extends ArrayAdapter<Products> {
Context context;
int resourceId;
Products data[] = null;
public MyProductsArrayAdapter(Context context, int resourceId,
Products data[]) {
super(context, resourceId, data);
this.context = context;
this.resourceId = resourceId;
this.data = data;
}
static class ProductsItemViewHolder {
ImageView image;
TextView name;
Spinner number;
}
String[] mArray = new String[] { "0", "1", "2", "3", "4", "5", "6", "7",
"8", "9" };
public View getView(final int position, View convertView, ViewGroup parent) {
View rowView = convertView;
ProductsItemViewHolder holder = null;
LayoutInflater mInflater = (LayoutInflater) context
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if (rowView == null) {
rowView = mInflater.inflate(R.layout.main2, parent, false);
holder = new ProductsItemViewHolder();
holder.image = (ImageView) rowView.findViewById(R.id.item_icon);
holder.name = (TextView) rowView.findViewById(R.id.tvListItemText);
holder.number = (Spinner) rowView.findViewById(R.id.NumberSpinner);
rowView.setTag(holder);
} else {
holder = (ProductsItemViewHolder) rowView.getTag();
}
Products f = data[position];
holder.image.setImageResource(f.itemIcon);
holder.name.setText(f.itemName);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context,
android.R.layout.simple_spinner_item, mArray);
holder.number.setAdapter(adapter);
return rowView;
}
}
//这是我的row_layout代码
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="@+id/item_icon"
android:layout_width="40dp"
android:layout_height="40dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="10dip"
android:gravity="center_vertical"
android:src="@drawable/ic_launcher" >
</ImageView>
<TextView
android:id="@+id/tvListItemText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@id/item_icon"
android:maxWidth="180dp"
android:maxLines="2"
android:text="Hello testing"
android:textColor="@color/brown"
android:textSize="15dp"
android:typeface="normal" />
<Spinner
android:id="@+id/NumberSpinner"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="0dp" />
</RelativeLayout>