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 callYOUR_SPINNER.setSelected(position you set earlier);This is happening because, once the list is created you are always getting the
convertViewingetViewand 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 yourspinnerand inonItemSelected()method of this listener set the value as the tag of your spinner object.holder.number.setTag(POSITION)After the statement
holder.number.setAdapter()doholder.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
getViewyou can setholder.number.setSelection(hashtable.get(position));