2

I want to add local pin codes in spinner.

For example : 600000 to 600113

How to add these in spinner?

ShreeshaDas
  • 2,042
  • 2
  • 17
  • 36
Gomathi
  • 29
  • 2
  • have a try with this http://www.java-samples.com/showtutorial.php?tutorialid=1517.. before posting a question over here please search in google about your requirement. – itsrajesh4uguys Feb 28 '13 at 12:11
  • Just use a loop to generate a string array with the numbers, and add it to the spinner. – Raghav Sood Feb 28 '13 at 12:13

4 Answers4

0

You should create an adapter and then add items to the adapter.

You are beginner, I post full code

public class MainActivity extends Activity {

Spinner addnum_spinner;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    addnum_spinner=(Spinner) findViewById(R.id.spinner1);
    String[] myarray=new String[113];// create an array
    long a=600000;
    String v;
    for(int i=0;i<=113;i++)
    {
    v=String.valueOf(a);
    myarray[i]=v;
    a=a+1;
    }
    ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
    android.R.layout.simple_spinner_dropdown_item,myarray);
    addnum_spinner.setAdapter(adapter);
    }
}
Linga
  • 10,379
  • 10
  • 52
  • 104
0

There are lot of questions already on SO addressing this type of issues. Please have a look at them.

You can check this previous SO question : How can I add items to a spinner in Android?

Community
  • 1
  • 1
Divya Motiwala
  • 1,659
  • 1
  • 16
  • 24
  • ArrayAdapter.createFromResource we can't sent it argument in String – Gomathi Feb 28 '13 at 13:21
  • I didn't get you. Did you mean you can't pass String to ArrayAdapter.createFromResource ? `ArrayAdapter.createFromResource(this, R.array.str2, android.R.layout.simple_list_item_1);` Here str2 is string array. – Divya Motiwala Mar 01 '13 at 06:19
0

Make an array of values which you want to populate in the spinner like this:

      String []Pin = {"600000","600113"}; 

Inatialize, then bind it to the spinner like this:

      Spinner spinner = (Spinner) findViewById(R.id.YourSpinner); //From XML
      ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(this,     android.R.layout.simple_spinner_item, Pin);
      spinnerArrayAdapter.setDropDownViewResource(android.R.layout.simple_list_item_1); // The drop down vieww
      YourSpinner.setAdapter(spinnerArrayAdapter);
      YourSpinner.setOnItemSelectedListener(this);
Skynet
  • 7,820
  • 5
  • 44
  • 80
0

it's simpler:

long int start=600000;
String myarray[]=new String[113];
for(int i=0;i<=113;i++)
{
myarray[i++]=a+"";
a++;
}

and then

Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
myarray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
user982115
  • 23
  • 3
  • Change type myarray to int error show it.What to do? – Gomathi Feb 28 '13 at 12:33
  • but only one issues in arrayadapter myarray error show it.In that error myarray convert to integer. – Gomathi Feb 28 '13 at 12:40
  • No it is not work.Change type myarray to int error show it? – Gomathi Feb 28 '13 at 12:55
  • Your code only i have added .just error show it createFromResource we can't added String.but we are using myarray in string format. – Gomathi Feb 28 '13 at 13:01
  • just check it ArrayAdapter.createFromResource – Gomathi Feb 28 '13 at 13:03
  • again error show it. The method createFromResource(Context, int, int) in the type ArrayAdapter is not applicable for the arguments (MainActivity, String[], int) – Gomathi Feb 28 '13 at 13:17
  • Please see second line,ArrayAdapter adapter = ArrayAdapter.createFromResource(this, myarray, android.R.layout.simple_spinner_item); – Gomathi Feb 28 '13 at 13:20
  • No this is your ans right? Spinner spinner = (Spinner) findViewById(R.id.spinner); ArrayAdapter adapter = ArrayAdapter.createFromResource(this, myarray, android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); spinner.setAdapter(adapter); – Gomathi Feb 28 '13 at 13:22
  • Please see second line – Gomathi Feb 28 '13 at 13:23
  • Can you sent it your code again – Gomathi Feb 28 '13 at 13:28