I have a question and know this has been asked a million times, I am fairly new to Android Programming. So I am kinda boggled.
I have created two parallel arrays - containing phrases in two languages.As the spinner changes it value(from 1st array) - the textview should be updated to reflect the translated phrase (in the second array).
The two arrays are created in string.xml, similar to below and the spinner gets it value from there.
<string-array name="array_name">
<item>phrase1</item>
<item>phrase2</item>
<item>phrase3</item>
<item>phrase4</item>
</string-array>
Im not sure how to go about doing this or if this is a viable way of doing it.... but boggled I remain....
Can someone please help?
You should set OnItemSelectedListener to spinner and change the text
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
textView.setText(getResources().getTextArray(R.array.array_name)[position]);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Related
I have populated spinner values with json data and posted it into a Web Service. Now i have the option to edit all the info. When i click on edit the value that was stored previously in the spinner must to pre-populated as the first item along with other json vales.
nationality_name1.add(jsonObject1.getString("NATIONALITY"));
SprStudentEnrolmentNationality.setAdapter(new ArrayAdapter<String>(ManagementEnhancementProgramStudentEnrolment.this,
android.R.layout.simple_list_item_1, nationality_name1));
SprStudentEnrolmentNationality.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
populateNationality();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
By doing like this the spinner would first be populated by the value that was stored on then displays all items and the first item is no longer in the first position.
For that you have to manually play with the list populating the spinner and bring to first position.
I would rather suggest you to use the below code which would directly point to your selected element.
spinnerObject.setSelection(INDEX_OF_PREVIOUS_VALUE)
I used to dynamically populate a Table Layout with different elements per row, among them a Spinner.
To handle the initialization of spinner values i used what most recommended: a boolean flag variable. The code went something like this
public class spinnerHandling implements OnItemSelectedListener{
public void dynamicallyPopulateTableLayout(int a){
...
//initialize spinner with default values
mySpinner.setSelection(a);
mySpinner.setTag(true);
mySpinner.setOnItemSelectedListener(this);
...
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
if(!(Boolean)parent.getTag()){
//my code
...
}
parent.setTag(false);
}
}
This worked fine.
Later i decided that i should use a List View instead of a Table Layout. For that i have a custom ArrayAdapter that uses a row Layout, in which of course i have a Spinner, to populate the List View. As those of you who are familiar with List Views, understand its complex dynamic behavior, would know that the somehow static turn around of the boolean flag method wont work.
After three days of tremendous headache trying to find a way to resolve this issue, i just came across the spinner's isDirty() method that in contrast with isPressed(), isSelected(), etc. actually changes its boolean values when spinner item is selected by user.
the code is something like this:
public class spinnerHandling implements OnItemSelectedListener{
public void dynamicallyPopulateListView(int a){
...
//initialize spinner with default values
mySpinner.setSelection(a);
mySpinner.setOnItemSelectedListener(this);
...
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
if(!parent.isDirty()){
//my code
...
}
}
}
This seems to work fine!
But i would like to make sure that isDirty() handles properly spinner initialization and user selection.
Can you please confirm or contradict this behavior?
Thank you.
I have checkboxs and spinners just like this:
In the above pictures: first spinner asiaSpinner have different countries and others europeSpnner and africaSpinner.
If I checked Asia and choose japan from spinner and checked Europe and choose Germany from spinner. then click set Details button. set Details both japan and Germany with respective information.
I want to set following field and save on database, what i checked. If i checked only one then one and if i checked all then save all.
My main intension is how to bind check box and spinner.
Each spinner should have an OnItemSelectedListener like this:
Spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
// Get/set your info from the spinner here (depends on how you are populating the spinner
}
});
And then just check the state of each checkbox:
final CheckBox asiaCk = (CheckBox) findViewById(R.id.asia_cb);
if (asiaCk.isChecked()) {
// Set whatever data you need appropriately
}
Just a disclaimer, I'm very new to both android and Java.
The end product that I'm going after in this is a soundboard like application. I want to display a list of sounds that the user can tap on to play the associated sound file.
So far I have setup a couple of string-arrays in my strings.xml file. One array represents the sounds available, and the second array represents the actual R.raw.soundfile resources. Might be easier to just show it.
<string-array name="alex_list">
<item>Sound Title</item>
</string-array>
<string-array name="alex_sounds">
<item>R.raw.soundfile</item>
</string-array>
Here is the class definition I have so far. I really don't want anyone to "do the work for me" as it were, but I'm completely lost as to where to progress from here. I feel like I should pass the string-array position from alex_list (when clicked) to get the right resource from alex_sounds (which should play immediately, doesn't need media controls). But that's where I get stuck.
public class AlexList extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(ArrayAdapter.createFromResource(getApplicationContext(), R.array.alex_list, R.layout.list_item));
getListView().setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
// I have zero clue what to do from here
}
});
}
}
If there is a better way to do any of this, please tell me. The last thing I want to do is develop bad habits at an early stage. I'll continue to work on it and post back if I find a solution.
Thanks in advance for your time!
Change your values array this way:
<string-array name="alex_sounds">
<item>soundfile</item>
</string-array>
Then your listener becomes: (packageName is a string representing the package of your R file: e.g. "com.example.main")
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
String selectedFromList = getItemAtPosition(pos);
int resId = getResources().getIdentifier(selectedFromList, "raw", packageName);
MediaPlayer mp = MediaPlayer.create(AlexList.this, resId);
mp.start();
}
This will play the file which name you selected in your list view, from your res/raw folder.
(Coded quickly in StackOverflow, there may be syntax errors)
I make a spinner put the value in spinner using array,now even I was not select any value from spinner it automatically takes first value from spinner array, I want to set default value when nothing is selected form spinner array by user,means I get default value which I set ,when I was not selected any value, is this possible, and what is the use of onNothingSelected(AdapterView ....below code should run when user select manually any value from spinner but it run always and get first value which is in array,so please tell how to get default value when I nothing selected from spinner, can I use on nothing selected method..?
ArrayAdapter<String> CurrencyAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, Currency);
currency.setAdapter(CurrencyAdapter);
currency.setOnItemSelectedListener( new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
Better set first value of array,the value what you want to set as default. And then you can check whether that value is selected or something else to figure out,is spinner selected or not??
A related question I asked a couple of days ago could help you here, have a look at How to use Android Spinner like a drop-down list and see some of the answers which I found helpful.
And no, onNothingSelected() does not do what you're after... you want to have a look at the methods setSelection() and setSelectionInt() in the question I referred to.