In my application I have a method returns a list of clients. How do I make a Spinner popular with only the customer names contained in the List?
In my application I have a method returns a list of clients. How do I make a Spinner popular with only the customer names contained in the List?
Create an adapter
public class ExampleSpinAdapter extends ArrayAdapter<Estado>{
// Your sent context
private Context context;
// Your custom values for the spinner (User)
private ArrayList<Cliente> values;
public ExampleSpinAdapter(Context context, int textViewResourceId,
ArrayList<Cliente> values) {
super(context, textViewResourceId, values);
this.context = context;
this.values = values;
}
public int getCount(){
return values.size();
}
public Estado getItem(int position){
return values.get(position);
}
public long getItemId(int position){
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView label = new TextView(context);
label.setTextColor(Color.BLACK);
label.setText(values.get(position).getNome());
return label;
}
@Override
public View getDropDownView(int position, View convertView,
ViewGroup parent) {
TextView label = new TextView(context);
label.setTextColor(Color.BLACK);
label.setText(values.get(position).getNome());
label.setPadding(10,10,10,10);
return label;
}
}
Instantiate the adapter by passing your list of clients
ExampleSpinAdapter exampleSpinAdapter = new ExampleSpinAdapter(getActivity(),
android.R.layout.simple_spinner_dropdown_item,
clientes/*Sua lista de clientes*/);
spinner.setAdapter(exampleSpinAdapter);
If you prefer, you can create ArrayList and popular as the names of the clients and then pass them by parameter in the ExampleSpinAdapter, and make small changes in the Adapter