Spinner selection not working Listview Android

When the ListView is loaded with Spinner in each row then the spinner selection is not static. It change. because of the recycling property of ListView.
Sample MainActivity.java:

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Spinner;

public class MainActivity extends Activity {
SpinnerAdapter ratingAdapter;
ListView listView;
List listString;
ArrayAdapter dataAdapter;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listString = new ArrayList();
for(int listCount = 0; listCount < 20 ; listCount++){
listString.add("ListCount"+listCount);
}

List spinnerData = new ArrayList();
for (int i = 0; i < listString.size(); i++) {
spinnerData.add("MyTest="+i);
}

dataAdapter = new ArrayAdapter(this,android.R.layout.simple_expandable_list_item_1, spinnerData);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

fillAdapter();
}

private void fillAdapter() {
ratingAdapter = new SpinnerAdapter(getApplicationContext());
listView = (ListView) findViewById(R.id.listView1);
listView.setAdapter(ratingAdapter);
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

class ViewHolder {
Spinner spinner;
}

class SpinnerAdapter extends ArrayAdapter {
Context context;
HashMap selectedItems = new HashMap();

public SpinnerAdapter(Context context) {
super(context, R.layout.item_list_spinner_adaoter, listString);
this.context = context;
}

public int getCount() {
return listString.size();
}
public Object getItem(int position) {
return listString.get(position);
}
public long getItemId(int position) {
return position;
}

@Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = LayoutInflater.from(context).inflate(R.layout.item_list_spinner_adaoter, null);
holder.spinner = (Spinner) convertView.findViewById(R.id.spinner1);

convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}

holder.spinner.setAdapter(dataAdapter);
if ( selectedItems.get( position ) != null ) {
holder.spinner.setSelection( selectedItems.get( position ) );
}
holder.spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

@Override
public void onItemSelected(AdapterView arg0, View arg1,
int arg2, long arg3) {
selectedItems.put( position, arg2 );
}

@Override
public void onNothingSelected(AdapterView arg0) {
// TODO Auto-generated method stub

}
});

return convertView;
}
}
}

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s