Popular Spinner

Asked

Viewed 726 times

0

In my application I have a method returns a list of customers. How do I popular a Spinner only with the names of customers contained in the List?

  • Here’s an example : http://androidexample.com/Spinner_Basics_-_Android_example/index.php? view=article_discription&aid=82&aaid=105 . You think it’s enough to understand?

1 answer

1

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;
}
}

instancie o Adapter passando sua lista de clientes

 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 customer names and then pass by parameter in Examplespinadapter, and make small changes in Adapter

Browser other questions tagged

You are not signed in. Login or sign up in order to post.