Spinner in ListView :
Adapter Class :
Adapter Class :
/** * Created on 05/01/17. */public class WorkListAdapter extends BaseAdapter { private Context context; private ArrayList<WorkModel> workModelArrayList; public WorkListAdapter(final Context context, final ArrayList<WorkModel> workModelArrayList) { this.context = context; this.workModelArrayList = workModelArrayList; } @Override public int getCount() { return workModelArrayList.size(); } @Override public WorkModel getItem(int i) { return workModelArrayList.get(i); } @Override public long getItemId(int i) { return i; } @Override public View getView(final int i, View view, ViewGroup viewGroup) { final ViewHolder viewHolder; if (view == null) { final LayoutInflater inflater = ((Activity) context).getLayoutInflater(); view = inflater.inflate(R.layout.row_worker, viewGroup, false); viewHolder = new ViewHolder(view); view.setTag(viewHolder); } else { viewHolder = (ViewHolder) view.getTag(); } final WorkModel workModel = workModelArrayList.get(i); // removeTextChangedListener(viewHolder);// viewHolder.edtName.setText(workModel.getWorkerName());// addTextChangedListener(viewHolder, i); final ArrayAdapter<String> adapter = new ArrayAdapter<>(context, android.R.layout.simple_list_item_1, workModel.getTypes()); viewHolder.edtName.setAdapter(adapter); viewHolder.edtName.setOnItemSelectedListener(null); viewHolder.edtName.setSelection(workModel.getPosition()); viewHolder.edtName.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) { workModel.setPosition(i); } @Override public void onNothingSelected(AdapterView<?> adapterView) { } }); return view; } private class ViewHolder { private Spinner edtName; ViewHolder(final View view) { edtName = (Spinner) view.findViewById(R.id.row_worker_edit_name); } } }
Model Class :
public class WorkModel { private int workerId; private String workerName; private String[] types = {"Android", "iPhone", "Window"}; private int position; public int getWorkerId() { return workerId; } public void setWorkerId(int workerId) { this.workerId = workerId; } public String getWorkerName() { return workerName; } public void setWorkerName(String workerName) { this.workerName = workerName; } public String[] getTypes() { return types; } public void setTypes(String[] types) { this.types = types; } public int getPosition() { return position; } public void setPosition(int position) { this.position = position; } }
Activity Class:
public class MainActivity extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); final ListView lv = (ListView) findViewById(R.id.activity_main_lv); final ArrayList<WorkModel> workModelList = new ArrayList<>(); for(int i = 0 ; i < 1000; i++){ workModelList.add(new WorkModel()); } final WorkListAdapter workListAdapter = new WorkListAdapter(this, workModelList); lv.setAdapter(workListAdapter); } }