问题描述
我在android的初学者....我需要你的帮助。
在我的应用程序,我创建使用自定义适配器和自定义行布局的列表视图。我成功地这样做与放大器;我加入了一个图像,TextView的和微调下拉中的所有行。在下拉有项目数(从0到9的当量)
但我的问题是,当我选择下拉,..我用值0的字符串设定一定的价值,在滚动的微调本身重置为初始值(这里是0)的列表,以9微调。
我能做些什么来避免这个问题?任何帮助将大大AP preciated ....在此先感谢。
//这是我的自定义适配器code ....
公共类MyProductsArrayAdapter扩展ArrayAdapter<产品> {
上下文语境;
INT RESOURCEID;
产品数据[] = NULL;
公共MyProductsArrayAdapter(上下文的背景下,INT RESOURCEID,
产品数据[]){
超级(上下文,RESOURCEID,数据);
this.context =背景;
this.resourceId = RESOURCEID;
this.data =数据;
}
静态类ProductsItemViewHolder {
ImageView的形象;
TextView的名称;
微调数;
}
串[] MARRAY =新串[] {0,1,2,3,4,5,6,7,
8,9};
公共查看getView(最终诠释的立场,观点convertView,ViewGroup中父){
查看rowView = convertView;
ProductsItemViewHolder支架=无效;
LayoutInflater mInflater =(LayoutInflater)上下文
.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
如果(rowView == NULL){
rowView = mInflater.inflate(R.layout.main2,父母,假);
持有人=新ProductsItemViewHolder();
holder.image =(ImageView的)rowView.findViewById(R.id.item_icon);
holder.name =(TextView中)rowView.findViewById(R.id.tvListItemText);
holder.number =(微调)rowView.findViewById(R.id.NumberSpinner);
rowView.setTag(保持器);
} 其他 {
支架=(ProductsItemViewHolder)rowView.getTag();
}
产品F =数据[位置]
holder.image.setImageResource(f.itemIcon);
holder.name.setText(f.itemName);
ArrayAdapter<字符串>适配器=新的ArrayAdapter<字符串>(背景下,
android.R.layout.simple_spinner_item,MARRAY);
holder.number.setAdapter(适配器);
返回rowView;
}
}
//这里是我的row_layout code
< RelativeLayout的的xmlns:机器人=http://schemas.android.com/apk/res/android
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:方向=横向>
< ImageView的
机器人:ID =@ + ID / item_icon
机器人:layout_width =40dp
机器人:layout_height =40dip
机器人:layout_alignParentLeft =真
机器人:layout_centerVertical =真
机器人:layout_marginLeft =10dip
机器人:重力=center_vertical
机器人:SRC =@可绘制/ ic_launcher>
< / ImageView的>
<的TextView
机器人:ID =@ + ID / tvListItemText
机器人:layout_width =WRAP_CONTENT
机器人:layout_height =WRAP_CONTENT
机器人:layout_centerVertical =真
机器人:layout_marginLeft =10dp
机器人:layout_toRightOf =@ ID / item_icon
机器人:=了maxWidth180dp
机器人:MAXLINES =2
机器人:文本=你好测试
机器人:文字颜色=@色/棕色
机器人:TEXTSIZE =15dp
机器人:字体=正常/>
<微调
机器人:ID =@ + ID /数字增
机器人:layout_width =80dp
机器人:layout_height =WRAP_CONTENT
机器人:layout_alignParentRight =真
机器人:layout_centerVertical =真
机器人:layout_marginLeft =0dp/>
< / RelativeLayout的>
在 getView()
您的名单的适配器,则必须调用 YOUR_SPINNER .setSelected(位置前面设置);
这是发生,因为,一旦列表创建您始终获得 convertView
在 getView
并作为其微调它的默认STAE为0的位置到那里。
此外,你正在做setAdapter在getView方法始终微调器,因此这将始终初始化当过 getView()
将被调用IE浏览器。上滚动的列表中。
我的意思是设置 setOnItemSelectedListener()
在微调
和 onItemSelected这个监听器()
法设置的值作为你的微调对象的标签。 holder.number.setTag(位置)
该语句后 holder.number.setAdapter()
DO holder.number.setSelection(的Integer.parseInt(number.getTag));
此方法要保存你选择的值,和相同的指数要设置到它曾经当 getView()
之称。
Editted:
是的,设定你的方法得到的参数值 onItemSelected(视图V,INT ARG2)
。这ARG2是定位系统告诉你,这个位置被选中你为这个微调(系统提供简单的回调)。现在,在同一个方法的实现,则必须将此值设置为这个视图对象的标签,这是这个回调的第一个参数。
另外,如果你没有得到的 getView该标签值()
您必须在每次初始化,而不是从convertView得到它你的微调,因为它可能包含重复使用视图,其是不考虑的行的
另外,如果你继续得到问题在执行我刚才所说,你面包车遵循另一种方法是使静态哈希表<整数,整数GT;
,其中的关键是行和值的索引将被选中微调项的索引。
所以,在 getView
您可以设置 holder.number.setSelection(hashtable.get(位置));
I am a beginner in android....I need your help.
In my app I created a listview using custom adapter and custom row layout. I am successful in doing so & I have added one image, textview and a spinner drop down in all rows. In the drop down there is number of items (from 0 to 9 for eg.)
But my problem is when I select drop down and set some value, on scrolling the list the spinner resets itself to initial value (here 0)..I am using a String of values 0 to 9 for Spinner.
What can I do to avoid this issue? Any help would be greatly appreciated....Thanks in advance.
//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;
}
}
//And here is my row_layout code
<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>
in the getView()
of your List's adapter, you must call YOUR_SPINNER.setSelected(position you set earlier);
This is happening because, once the list is created you are always getting the convertView
in getView
and as its default stae of spinner in it is 0 position it gets there.
Also, you are doing setAdapter to spinner always in getView method, so this will always initialize it when ever getView()
will be called ie. on scrolling the list.
What I am saying is set setOnItemSelectedListener()
on your spinner
and in onItemSelected()
method of this listener set the value as the tag of your spinner object.holder.number.setTag(POSITION)
After the statement holder.number.setAdapter()
do holder.number.setSelection(Integer.parseInt(number.getTag));
This way you are saving the value you selected, and you are setting the same index to it when ever the getView()
is called.
Editted:
yes, set the value you got as parameter in method onItemSelected(View v, int arg2)
.This arg2 is the position system is telling you that this position is selected by you for this spinner (simple callback provided by the system).Now, in the same method's implementation, you must set this value as the tag of this view object, which is the first argument of this callback.
Also, if you are not getting this tag value in getView()
you must initialize your spinner every time instead of getting it from convertView, as it might contain the reused view of the row which is not in view.
Also, if you continue to get problem in implementing what I have said, you van follow another approach that make a static HashTable<integer, integer>
where key would be the index of row and value will be the index of spinner item selected.
So, in getView
you can set holder.number.setSelection(hashtable.get(position));
这篇关于在自定义列表视图与ArrayAdapter滚动微调复位值时,的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!