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.
Related
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);
As I am new to android development i struck up with a problem where i am unable to set data to spinner adapter..
Here i am getting data from database is like
String times = [09:30,10:30,12:15,04:45,10:50]
I am getting array in this pattern. When i am trying to set this array to spinner adapter it's getting error...
dateadp=new ArrayAdapter<CharSequence>(this, android.R.layout.simple_spinner_dropdown_item,times);
datespn.setAdapter(dateadp);
so how to convert the following array to string array and how i can append that data to spinner. Can anyone help me with this....
If
["09:30","10:30","12:15","04:45","10:50"] represents String abc.
Then you can take following approach.
String processingString = abc.substring(abc.indexOf("[") + 1,
abc.indexOf("]"));
String[] arr = processingString.split(",");
ArrayAdapter < String > adapter = new ArrayAdapter < String > (this,
android.R.layout.simple_list_item_1, arr);
I think you struck with converting arraylist to String array. if you are getting data into the arraylist then the code is here.
ArrayAdapter<String> dateadp;
timesArray= times.toArray(new String[times.size()]);
dateadp=new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item,timesarray);
datespn.setAdapter(dateadp);
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 currently using a simpleCursorAdaptor and a cursor to load a spinner with data, however I would prefer to convert the cursor to a simple array and use this instead (as the list is short and static).
What's the simplest way of doing this?
My code currently is:
private void loadEmployeeList(){
LoginDataHandler dataHandler = new LoginDataHandler(getContentResolver());
Cursor data = dataHandler.activeEmployeeList();
if (null!=data){
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item,
data,
new String[]{MyobiliseData.Columns_employees.NAME},
new int[] { android.R.id.text1 }
);
// Attach the data to the spinner using an adaptor
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
ArrayList<String> mArrayList = new ArrayList<String>();
for(data.moveToFirst(); !data.isAfterLast(); data.moveToNext()) {
mArrayList.add(data.getString(data.getColumnIndex(MyobiliseData.Columns_employees.NAME)));
}
now you can proceed as you have an array mArrayList of cursor's data,
If it is short and static, you might consider putting it in your strings.xml as an array and accessing it that way rather than incurring somewhat greater overhead of reading from a DB and translating to an array.
data.xml
<resources>
<string-array name="States">
<item>AL</item>
<item>AK</item>
<item>AR</item>
</string-array>
</resources>
Then to use it for your spinner:
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
getActivity(), R.array.States, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(R.layout.listlayout_black);
final Spinner states = (Spinner) v.findViewById(R.id.mbr_state_spinner);
states.setAdapter(adapter);
Then you have to write a logic for iterating over cursor and fetch data. Create array with these data you got from cursor.
Here you go
ArrayList<String> list = new ArrayList<String>();
Cursor data = dataHandler.activeEmployeeList();
if (data.moveToFirst())
{
do {
list.add(data.getString(data.getColumnIndex(MyobiliseData.Columns_employees.NAME)));
} while (data.moveToNext());
}
private ArrayAdapter spn_1;
ArrayList<My Class> result;
spn_1 = new ArrayAdapter(this, android.R.layout.simple_spinner_item, result);
spn_1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
m_spn_gender.setAdapter(spn_1);
I try like this but it wrong
what should I do?
You need to put objects in the ArrayList.