Selecting a specific string array - android

I have a question and i cant find a solution myself. I am using for example the following string arrays:
<string-array name="alphabet">
<item>ccc</item>
<item>bbb</item>
<item>aaa</item>
</string-array>
<string-array name="ccc">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<string-array name="bbb">
<item>4</item>
<item>5</item>
<item>6</item>
</string-array>
<string-array name="aaa">
<item>7</item>
<item>8</item>
<item>9</item>
</string-array>
So, The first string array alphabet is placed inside a spinner. (dropdown menu) Lets say i select CCC, Then i want only the items in between the array CCC to be worked with and stored in an array These information in this array should then be randomized and formatted into groups later on. But i only need a selection of the string selected.
So is there a way to select just one array based on the choice made out of the first array?
Kind regards,

Here is the code, the way you needed.
Step 1] Set, setOnItemSelectedListener on first spinner and get the Arrayname for target spinner
spinner_alphabates.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l)
{
String arrayName = spinner_alphabates.getSelectedItem().toString();
int resId = getResources().getIdentifier(arrayName,"array",getPackageName());
setResultArray(resId);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Step 2] set the result array on target spinner as
public void setResultArray(int resID)
{
String [] result_array = getResources().getStringArray(resID);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, result_array);
spinner_result.setAdapter(adapter);
}
No need to put 'if' 'else' conditions to match array names.
Hope this help you.

Try this, I am not sure whether it is going to work, just have try
<string-array name="ccc">
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
<integer-array name="id">
<item> #array/ccc </item>
</integer-array>

You need to get array of string like this. and you will access it with index of string array.
String.xml
<string-array name="my_books">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
<item>Item 4</item>
</string-array>
Get List of string in Java file like :
Resources res = getResources();
String[] myBooks = res.getStringArray(R.array.my_books);

Try this method and put this method into onCreate in activity method..
change this array add one more item..
<string-array name="alphabet">
<item>Select</item>
<item>ccc</item>
<item>bbb</item>
<item>aaa</item>
</string-array>
private void initView() {
spinner = findViewById(R.id.spinner);
final List<String> asList = Arrays.asList(getResources().getStringArray(R.array.alphabet));
final ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, asList);
final ArrayAdapter<String> adapter2 = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (spinner.getSelectedItem().equals("ccc")) {
adapter2.addAll(Arrays.asList(getResources().getStringArray(R.array.ccc)));
spinner.setAdapter(adapter2);
adapter2.notifyDataSetChanged();
} else if (spinner.getSelectedItem().equals("aaa")) {
adapter2.addAll(Arrays.asList(getResources().getStringArray(R.array.aaa)));
spinner.setAdapter(adapter2);
adapter2.notifyDataSetChanged();
} else if (spinner.getSelectedItem().equals("bbb")) {
adapter2.addAll(Arrays.asList(getResources().getStringArray(R.array.bbb)));
spinner.setAdapter(adapter2);
adapter2.notifyDataSetChanged();
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
}

Related

Associate values to spinner

I want to implement in my application, certain mathematical calculation, but, it has a variable that covers age group and data, I will show in practice: 'Adequação da CB = CB obtida/CB percentile 50 * 100' . This is the formula, where the variable "CB percentile 50" has 28 different forms of being expressed, for example, from 1 to 2 years old it has the value of 16, from 2 to 3 has the value of 16.3, from 3 To 4 has the value of 16.8 and so on... How do I make the spinner check the age data and return this value to be placed in the mathematical expression?
My strings file
<string name="fxetaria">Selecione a Faixa Etária</string>
<string-array name="faixaet">
<item>1 a 1.9</item>
<item>2 a 2.9</item>
<item>3 a 3.9</item>
<item>4 a 4.9</item>
<item>5 a 5.9</item>
<item>6 a 6.9</item>
<item>7 a 7.9</item>
<item>8 a 8.9</item>
<item>9 a 9.9</item>
<item>10 a 10.9</item>
<item>11 a 11.9</item>
<item>12 a 12.9</item>
<item>13 a 13.9</item>
<item>14 a 14.9</item>
<item>15 a 15.9</item>
<item>16 a 16.9</item>
<item>17 a 17.9</item>
<item>18 a 24.9</item>
<item>25 a 29.9</item>
<item>30 a 34.9</item>
<item>35 a 39.9</item>
<item>40 a 44.9</item>
<item>45 a 49.9</item>
<item>50 a 54.9</item>
<item>55 a 59.9</item>
<item>60 a 64.9</item>
<item>65 a 69.9</item>
<item>70 a 74.9</item>
</string-array>
And my activity:
public class adeqcbh extends AppCompatActivity {
Spinner spinner;
ArrayAdapter<CharSequence> adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_adeqcbh);
spinner = (Spinner)findViewById(R.id.spinner);
adapter = ArrayAdapter.createFromResource(this, R.array.faixaet, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
I'm thinking the best way would be to have another array with the values (the same amount as your first array).
<string-array name="faixaet_values">
<item>16</item>
<item>16.3</item>
...
<string-array>
Then, in your onItemSelected method, just get the value from the new string array, like so;
String valueString = getResources().getStringArray(R.array.faixaet_values)[position - 1];
float value;
try {
value = Float.valueOf(valueString);
} catch(NumberFormatException e) {
Log.e(TAG, "This is not a number");
}

Spinner1 populate from array, then select second array for spinner2 based on 1st spinner selection

Okay spinner 1 uses Manu_array
spinner 2 uses Manu 1xsd, Manu 2xrsd or 3x4rsd
all I need for now is to be able to populate the second spinner, after selecting the a value from the Manu_array in spinner 1.
<!--for spinner 1-->
<string-array name="Manu_array">
<item>Manu 1xsd</item>
<item>Manu 2xrsd</item>
<item>Manu 3x4rsd</item>
</string-array>
<!--for spinner 2-->
<string-array name="Manu 1xsd">
<item>a1</item>
<item>a2</item>
<item>a3</item>
<item>a4</item>
</string-array>
<string-array name="Manu 2xrsd">
<item>bg 1</item>
<item>bg 2</item>
</string-array>
<string-array name="Manu 3x4rsd">
<item>z1</item>
<item>z2</item>
<item>zd4</item>
<item>xs5</item>
<item>fg34</item>
</string-array>
My java file code:
final Spinner[] spinner1 = {(Spinner) findViewById(R.id.spinner1)};
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.Manu_array, R.layout.textview);
spinner1[0].setAdapter(adapter);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner1[0].setAdapter(adapter);
spinner1[0].setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
int index = arg0.getSelectedItemPosition();
// storing string resources into Array
Manu= getResources().getStringArray(R.array.Manu_array);
Toast.makeText(getBaseContext(), "You have selected : " + Manu[index],
Toast.LENGTH_SHORT).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// do nothing
}
});
I can process code that will allow the spinner to populate with the array data, but I cant get the second spinner to populate based on the selection of the first spinner, surely its simple if equals value use array.
The spinner above shows the values for the Manu_array, the next spinner has to take the value selected and populate with the appropriate array values, but all I have managed to do is generate a second spinner with the array values I have manual typed in, not selected.
if(Manu.equals("Manu 1xsd")){spinner2 languages = getResources().getStringArray(R.array.Manu 1xsd_array)}else......
Any suggestions where I may look for examples please looked at a few on here and confused me something chronic.
Set the second spinner adapter when an item from the first spinner is clicked
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
int arrayId = 0;
switch(position) {//get array id according to selected position
case 0:
arrayId = R.array.Manu_1xsd;
break;
case 1:
arrayId = R.array.Manu_2xrsd;
break;
case 2:
arrayId = R.array.Manu_3x4rsd;
break;
};
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this, arrayId, R.layout.textview);
spinner2.setAdapter(adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
// your code here
}
});

Why is my dynamic spinner not populating

I have created a spinner dynamically. It shows up but is empty. It is meant to contain the following string array.
<string-array name="task_array">
<item>task 1</item>
<item>task 2</item>
<item>task 3</item>
<item>task 4</item>
</string-array>
The following is my code from a class that extends fragmentactivity
public void addNewView() {
final TableLayout table = (TableLayout) findViewById(R.id.spLayout);
Spinner sp = new Spinner(this);
ArrayAdapter<CharSequence> adapter_tasks = ArrayAdapter.
createFromResource(this, R.array.task_array,
android.R.layout.simple_spinner_item);
table.addView(sp);
for(int i = 0; i < 10; i++) {
sp.setId(i+10);
}}
Any ideas? ta
You forgot to add the adapter to the spinner.
Use sp.setAdapter(adapter_tasks);

how to read particular key value in string.xml on android?

string.xml contains lot of keys with value. I want to get particular value based on key.
For example, string.xml contains key1, key2 up to key100. I displaying these keys into ListView. If user select the key50 means I want to displaying the corresponding value in TextView. How to achieve this?
Sample string.xml file,
<?xml version="1.0" encoding="utf-8" standalone="no"?>
<resources>
<string name="key1">value 1</string>
<string name="key2"> value 2 </string>
...
...
<string name="key100"> value 100 </string>
</resources>
You should save your key as a string array resource like this
<string-array name="my_keys">
<item>Key 1</item>
<item>Key 2</item>
<item>Key 3</item>
<item>Key 4</item>
<item>Key 5</item>
<item>Key 6</item>
</string-array>
Populate the listView with the following code.
String[] myKeys = getResources().getStringArray(R.array.my_keys);
ListView mListView = (ListView)findViewById(R.id.yourListView);
mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, myKeys));
Retrieve the clicked item and put the value into the TextView
TextView myTextView = (TextView)findViewById(R.id.yourTextView);
mListView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
myTextView = ((TextView) view).getText();
}
});
If you have a <string name="key">string value</string> resources then the only way is to access them by key:
String value = context.getString(R.string.key);
Btw, if you need to store some arbitrary data into your Views (the ones inside your ListView), use view.setTag(object) and view.getTag().
I will suggest to use XPath to get your value for keys.

spinner adding string array on item selection how can get item related value in android

i am developing one spinner this spinner i am string array
spinner = (Spinner)this.findViewById(R.id.mlg);
final CharSequence[] itemArray =getResources().getTextArray(R.array.RectBeam);
final List<CharSequence> itemList =new ArrayList<CharSequence>(Arrays.asList(itemArray));
adapter = new ArrayAdapter(this,android.R.layout.simple_spinner_item,itemList);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
Toast.makeText(parent.getContext(), "The planet is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
............................
<string-array name="RectBeam">
<item value="3000000" > Steel</item></string-array>
this is the spinner related string array i am get the spinner item i am using parent.getItemAtPosition(pos).toString(),done my problem is particular item value how can get
example : steel----------->3000000
I am not sure either Spinner allow that attribute value in XML String or not but your problem can be solved like this.
Create two arrays in your array.xml file like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="items">
<item>Nokia 1200</item>
<item>Nokia 1600</item>
<item>Nokia 5130</item>
</string-array>
<string-array name="values">
<item>1000</item>
<item>2000</item>
<item>5000</item>
</string-array>
</resources>
Now load first array in your adapter and store the second one in other Array to hold values of items as:
String items [] = getResources().getStringArray(R.array.items);
String values [] = getResources().getStringArray(R.array.values);
And you can simply get the respective item name and value in your onItemSelected() method like this:
String item = parent.getItemAtPosition(pos).toString();
String value = values [pos];

Categories

Resources