I am getting error for the following code which implements Spinner with multi-dimensional array.
Spinner sp1 = (Spinner) findViewById(R.id.spinner1);
String dept[][] ={{"cs","101"},{"me","102"}};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line, dept);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(adapter);
Related
I use this code for set padding to spinner ( not for children )
Spinner spnCategory = (Spinner) findViewById(R.id.spnCategory);
List<String> list = new ArrayList<String>();
list.add("UP");
list.add("Down");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(R.layout.dropdown_item);
spnCategory.setPadding(15, 5, 2, 4);
spnCategory.setAdapter(dataAdapter);
But this code not work
Try removing dataAdapter.setDropDownViewResource(R.layout.dropdown_item);, it should work fine
I have Spinner in my app and set String[] programmatic.
speciality = (Spinner) findViewById(R.id.general_specality_s);
specialityadapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, speciality_list);
speciality.setAdapter(specialityadapter);
and get selected string like this speciality.getSelectedItem().toString(); and store in sqlite. Now I want to get that value from sqlite and need to set in Spinner1.
String SpinnerText=c.getString(c.getColumnIndex("speciality"));
How to set this string to speciality / Spinner ? Thanks in advance.
Fetch your data into an array and use an array adapter
String[] spinnerArray = your data goes here;
Spinner spinner = (Spinner)findViewById(R.id.yourspinnerid);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, spinnerArray);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(spinnerAdapter);
How can i populate a Spinner from String array , I know i can do that from array.xml like this code :
ArrayAdapter<CharSequence> gameKindArray = ArrayAdapter.createFromResource(view.getContext(),R.array.game_kind, android.R.layout.simple_spinner_item);
gameKindArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gameKind.setAdapter(gameKindArray);
but when i have
String[] test=new String[]{"test1","test2"};
how i can change String[] to ArrayAdapter ?!
Use ArrayAdapter this way
your string array
String[] test=new String[]{"test1","test2"};
your ArrayAdapter
ArrayAdapter<String> gameKindArray= new ArrayAdapter<String>(MyActivityClass.this,android.R.layout.simple_spinner_item, test);
gameKindArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gameKind.setAdapter(gameKindArray);
Try the below
ArrayAdapter<String> gameKindArray = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, test);
gameKind.setAdapter(adapter);
For more info check the docs.
http://developer.android.com/reference/android/widget/ArrayAdapter.html
http://developer.android.com/reference/android/widget/Spinner.html
you dont need to convert in any form just use your string array like below:
ArrayAdapter<String> gameKindArray= new ArrayAdapter<String>(MyActivityClass.this,android.R.layout.simple_spinner_item, test);
gameKindArray.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
gameKind.setAdapter(gameKindArray);
you can get easily into spinner.
I have two spinners and two arrays. However, one spinner receives both arrays while the other receives no values from either of the two arrays. Note: I do not want to use radio buttons as the data is shortened for review.
final ArrayList<String> serialnums = new ArrayList<String>();
serialnums.add("576798");
serialnums.add("495874");
serialnums.add("345667");
serialnums.add("956345");
final ArrayList<String> carrys = new ArrayList<String>();
serialnums.add("R");
serialnums.add("L");
serialnums.add("F");
serialnums.add("B");
s1 = (Spinner) findViewById(R.id.spinnerSerial);
spinnerCarry = (Spinner) findViewById(R.id.spinnerCarry);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, serialnums );
ArrayAdapter<String> adapterCarrys = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, carrys );
s1.setAdapter(adapter);
spinnerCarry.setAdapter(adapterCarrys);
Note 2: Spinner s1 gets all the data
One spinner has all the data because you are assigning all the data to it. Change this:
final ArrayList<String> carrys = new ArrayList<String>();
serialnums.add("R");
serialnums.add("L");
serialnums.add("F");
serialnums.add("B");
to this:
final ArrayList<String> carrys = new ArrayList<String>();
carrys.add("R");
carrys.add("L");
carrys.add("F");
carrys.add("B");
You are adding "R" "L" "F" "B" to serialnums add it to carrys
I am working on android offline data management. I am creating a text file where all the data of the main server will be store and after that I read it.
How can I fill textfile to a Spinner?
String[] str = new String[totalLine];
BufferedReader in = new BufferedReader(new FileReader("in.txt"));
String line = in.readLine();
int index=0;
while(line!=null)
{
str[index++]=line;
line = in.readLine();
}
spinner = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, str);
spinner.setAdapter(adapter);
Edit:
From your previous comment what i got is your data is in Mumbar.Noida format
Do this
String[] str = response.toString().split(",");
spinner = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item, str);
spinner.setAdapter(adapter);
this will populate spinner like
Mumbai
Noida