Getting value of choosed option in Spinner - android

I want to get value of choosed option in Spinner.
I know, I can get this from setOnItemSelectedListener, but I don't want to use this.
I have this:
String spinner1odp = spinnerSubject.getSelectedItem().toString();
But result of this code is: android.database.sqlite.SQLiteCursor#40f828e8. I want to get String, not something like that :/

I think you are popping up Spinner from database.
So considering that you will have to get the selected index first and fetch the required data from the Cursor:
Code Snippet :
int position = mySpinner.getSelectedItemPosition();
Cursor cursor = (Cursor) myAdapter.getItem(position);
String myText = cursor.getString(cursor.getColumnIndex(KEY_NAME));
Further Reference : Android Spinner Selected Item

By using this code you can get...
String value= (String)spinnerSubject.getItemAtPosition(spinnerSubject.getSelectedItemPosition());

Related

how to remove a string from arraylist of strings, ANDROID

I created an arraylist of strings
List<String> textArray = new ArrayList<String>();
and then I added the strings(which I am getting from edittext) to textArray as follows
String text = editText1.getText().toString();
textArray.add(text);
Now I created a button and need to remove the string from the array when the button is clicked.But i dont know what to do.
I know for arrays of bitmaps we clear a bitmap from array using recycle but Please suggest me how to remove or clear the string from arraylist.
You can call one of:
to remove all entries
textArray.clear();
to remove from a specific position (e.g. first item)
textArray.remove(0);
to remove a specific string (that equals yours)
textArray.remove("myString");
Try this..
Getting position from the textArray array list
int pos = textArray.indexOf(text);
and then remove from the string position
textArray.remove(pos);
because we cannot directly remove the string. we can remove the string contains position.
You can do it with more then one way as per your need.
textArray.clear();
It will clear whole ArrayList.
If you want to remove only some specifis data from index then,
textArray.remove(INDEX TO REMOVE);
Try This
String text = editText1.getText().toString();
textArray.add(text);
to remove
textArray.remove(text);

Adapter for spinner does not return correct position of the string in android

I have an arraylist of type string which is assigned to spinner. When I use following code to set spinner's selection, it returns -1 as position although string is present in the list.
int spinnerPosition = myAdap.getPosition(myString);
////here it returns -1
//set the default according to value
spinner.setSelection(spinnerPosition);
It returns correct position for some adapters but not for others. What can be possibly wrong?? String I passed in "myString" variable is 100% correct and exists in the adapter list.
I'm not positive, so I apologize if this is wrong. Don't have source code to look at right now.
but I think the getPosition() method does not perform a .equals() comparison, so you can't just pass in an equivalent string. So don't do
myString = "asdf"; //myString is getting assigned a reference to a brand new string
spinnerPosition = adapter.getPosition(myString);
because mySpring is not pointing at the same object as the string in your array, even if myString.equals(originalString) may return true;
This should work:
//assign the reference to point at the exact same object that is in the ArrayList
myString = originalString;
spinnerPosition = adapter.getPosition(myString);

how to set single value and arrays on same edittext in android

I'm getting contacts from the "+" button and retrieving email from MULTIPLE_CHOICE_LIST and getting in array form and setting on edittext, but when i manually write the value in edittext and put "," and then add '+' button to retrive more emails than it replaces the value i have written manually.Please, tell me how to keep the value.
for eg-
abc#gmail.com, and when i retrive from contacts it replaces with an array like [xyz#sdsd.com,qwe#wer.com] and i want it like abc#gmail.com,xyz#sdsd.com,qwe#wer.com
Thanks in advance...
try this way
String[] emails = {"emai1#abc.com","emai2#abc.com","emai3#abc.com"};
String finalStr="";
for (String string : emails) {
finalStr+=string+",";
}
edtEmail.setText(finalStr.substring(0, finalStr.length()-1));

update My Spinner from SQlite Database

I FOUND A SOLUTION FOR RESTORING MY VALUES, SEE MY NEW VERSION of populateFields()
Okay so I have been reading through all the Spinner and SQlite posts on here and cannot seem to find a good answer for what I am looking for so I am posting this scenario.
My app has two screens and uses the sqlite database on my device saving a name and weight fields from editTexts as strings like so
String eName = name.getText().toString();
String eWeight = weight.getText().toString();// where name and weight are EditTexts
and I have two spinners as follows
String eReps = spinReps.getSelectedItem().toString();
String eSets = spinSets.getSelectedItem().toString();
Then I call this to add to the database
long id = mDbHelper.createExercise(eName, eWeight, eReps, eSets);
Here is where my issue is, upon someone selecting to create a new exercise my app crashes because it is trying to populate a spinner incorrectly. Here is what I have currently.
private void populateFields(){
if(mRowId != null){ // where mRowId is the selected row from the list
Cursor exercise = mDbHelper.fetchExercise();
name.setText(exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_NAME)));
weight.setText(exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_WEIGHT)));
// this is the part that i need help with, I do not know how to restore
// the current items spinner value for reps and sets from the database.
spinReps.setSelection(exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS)));
spinSets.setSelection(exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_SETS)));
}
I assume I need to use some sort of adapter to restore my list items along with the current value from the database, I am just not sure how.
Can someone please help me with this???
** BELOW IS MY SOLUTION**
I had to move my ArrayAdapters repsAdapter, spinAdapter out of my onCreate()
and then implement this new populateFields()
private void populateFields(){
if(mRowId != null){
Cursor exercise = mDbHelper.fetchExercise(mRowId);
// same as before
name.setText(exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_NAME)));
// get the string for sReps and sSets from the database
String sReps = exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS));
String sSets = exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_SETS));
// use the strings to get their position in my adapters
int r = repsAdapter.getPosition(sReps);
int s = setsAdapter.getPosition(sSets);
// set their returned values to the selected spinner Items
spinReps.setSelection(r);
spinSets.setSelection(s);
// same as before
weight.setText(exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_WEIGHT)));
}
}
The way to set a Spinner to a value, not a position, depends on what adapter you are using.
ArrayAdapter, this one is easy:
int position = adapter.getPosition(exercise.getString(exercise.
getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS)));
spinReps.setSelection(position);
SimpleCursorAdapter, this one is a little harder:
String match = exercise.getString(exercise.getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS));
Cursor cursor = adapter.getCursor();
cursor.moveToPosition(-1);
int columnIndex = cursor.getColumnIndexOrThrow(ExerciseDbAdapter.KEY_REPS);
while(cursor.moveToNext()) {
if(cursor.getString(columnIndex).equals(match))
break;
}
spinReps.setSelection(cursor.getPosition());
If you are using a different type of adapter and can't modify the code above to fit it, let me know. Hope that helps!

Calling a Resource by a string?

Here's the setup. I have a spinner, and each item in the spinner is associated with their own StringArray. I want to streamline the process of loading the StringArray when an item is selected in the spinner without using a bunch of if statements for each item.
The StringArray has the same name as the spinner item's text
Drawn out it would look like this:
String cat = parent.getItemAtPosition(pos).toString(); //Selected Spinner item (Category)
...
String catStringArray = "R.array." + cat;
listdata = getResources().getStringArray(catArray); //Get the StringArray
is there a way to do this correctly?
--Edit--
#EboMike
Your answer sent me on a hunt and ran into this which I'm now using:
Class res = R.array.class;
Field field = res.getField(selectedCategory);
int saId = field.getInt(null);
String[] myList = getResources().getStringArray(saId);
That's not a great approach. It's slow. It'd be better to have an internal integer array with all the R.string IDs or something similar.
If you really insist on using a string-based approach, use Resources.getIdentifier(). It's technically not a big deal if you only do it once.

Categories

Resources