Hi i am parsing xml file to insert buttons,textview and for creating them i am calling a function for each so that it may create many, depending on the xml. Now, i like to create a spinner in that xml. But i want to insert its items from an array.
<item>
<id>1</id>
<text>text1</text>
</item>
</items>
i dont want to enter the text in this way. i want to call an array.So what do i need to do?
how i am going to record array name into xml and call it? Can somebody please help me ?
Did you try to set Adapter on that spinner ? Like this :
Spinner spinner = ...;
final String[] choices = {"1","2"};
ArrayAdapter<String> adapter =new ArrayAdapter<String>(ObservationSubmit.this,android.R.layout.simple_spinner_item, choices);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(a);
Related
i dont understand what is meant by R.layout or R.array in the following code, who describes
how to fill a spinner with drop-down menue with a array who has different countries as its elements:
adapter = ArrayAdapter.createFromResource(this, R.array.countries, androidx.appcompat.R.layout.support_simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinnerCountry.setAdapter(adapter);
The string Array with the name "countries" is loacated in the strings.xml file, in folder res -> values. Why is it necessary to use R.array to locate the country array and why can i not use findViewbyID?
And second question is why is in R.layout the spinner called support_simple_spinner_dropdown_item?
If i choose spinner on the Design tab via drag and drop theres only one spinner to choose.
Would be nice to have some help. Thank you.
I looked on the Developersite and searched the forum.
First, the values under R are generated at compile time. They're simply a list of int values with names likes R.array.<name> or R.id.<name> that can be used as identifiers for actual arrays, views, or whatever.
I think the official docs explain what is happening quite well, but I'll explain in my own words anyway.
Why is it necessary to use R.array to locate the country array and why can i not use findViewbyID?
A string array is not a View, so it wouldn't make sense to use findViewById. findViewById is a method that returns the View referenced by an int value defined in the R.id constants.
R.array.countries will just be an int value that references a string array. To get the String[] for that id, you would instead use Resources::getStringArray(...). So, in the context of an Activity, you'd do final String[] array = getResources().getStringArray(R.array.countries);.
why is in R.layout the spinner called support_simple_spinner_dropdown_item
support_simple_spinner_dropdown_item is the id for a layout. You should understand some of the different components of a spinner. There is:
The View itself. There is only one of these, and it's what you see in the drag-and-drop option in the design/laout editor.
The layout id for each row in the spinner when it is open. This is defined in the layout xml file referenced by R.layout.simple_spinner_item.
The data used to populate each row. This is defined in the array referenced by R.array.countries.
I have a simple list view defined in my xml:
<ListView
android:id="#+id/mode_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:entries="#array/mode_array_generic" />
I have three different string arrays defined in my string.xml (one being "mode_array_generic") and I'd like to choose which string array is loaded when my fragment starts based on some passed parameter.
Is there a clean simple way to redirect the "entries" field to a different string resource during "onCreateView"?
MikeM.'s comment gave me the hints I needed. Here's my solution for future reference.
Get the string array based on the parameter using a switch or if-else block:
String[] modeList = getActivity().getResources().getStringArray(R.array.mode_array_generic);
Then create an ArrayAdapter with it:
ArrayAdapter<String> adapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, modeList);
(the "android.R.layout.simple_list_item_1" as the resource ID for a layout whas where I was stuck)
Than just set the adapter on the ListView:
ListView listView = (ListView) rootView.findViewById(R.id.mode_list);
listView.setAdapter(adapter);
I have an string array like this:`
<string-array name="converterlist">
<item>Angle</item>
<item>Area</item>
<item>Bits and Bites</item>
<item>Density</item>
<item>Electric Current</item>
<item>Energy</item>
<item>Force</item>
<item>Length</item>
<item>Mass</item>
<item>Power</item>
<item>Pressure</item>
<item>Speed</item>
<item>Temperature</item>
<item>Time</item>
<item>Volume</item>
</string-array>
When I set it on an android spinner, it is showing as it is on the array. Now, I have another spinner where I want to set the list reverse or from second item. How can I do it?
In your onCreate method for the activity that has the spinner, load the string array from resources getResources().getStringArray(...), reverse that array, and set it as the data source to the reverse spinner using an ArrayAdapter.
try this.
String[] arr=getResources.getStringArray(R.id.converterlist);
//reversed list
List<String> convertList=Collections.reverse(new ArrayList<String>(Arrays.asList(arr)));
I'm using xml array with value and its Id.I want to get Id of value after user selects it.Kindly tell me how can I get that?
<string-array name="medicine">
<item id="21058">ALPHA LIPOIC ACID</item>
<item id="19699">B 50 VITAMIN</item>
<item id="19470">B100 ULTRA 50S</item>
</string-array>
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.select_dialog_item, getResources().getStringArray(R.array.medicine));
AutoCompleteTextView actv= (AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1);
actv.setOnClickListener(this);
actv.setThreshold(1);
actv.setAdapter(adapter);
I have found its answer myself. I made a model for ArrayList, I assign that ArrayList to the AutoCompleteTextView Adapter and then, the OnClick event of the AutoCompleteTextView gives me the exact id and value.
private ArrayAdapter<String> arr_adapt ;
str_eye_cat = "Select^"+str_eye_cat;
str_arr = fun.split(str_eye_cat.trim(), "^");
arr_adapt = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, str_arr);
arr_adapt.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spn_eye_cat.setAdapter(arr_adapt);
I have used above code....
But I want to increase the size of the list in the spinner so that user can see what he is selecting.
Well...what I think you want is to make the spinner either wider or more narrow so that the things in it display correctly? If that's what you want, than the code you need is in the XML file, where make the spinner. The android:layout_width is what you'd need.
If you need to increase the size of the adapter that makes the list, then that's a different story. What you can do is make a string array in the strings.xml file in the values directory, like this:
<string-array name="EU_types">
<item>Units</item>
<item>%</item>
<item>BBLS</item>
<item>BPD</item>
<item>CFM</item>
<item>DegC</item>
</string-array>
This array can pretty much hold as much as you want it to, but the values have to be all preset like this. If you want to pull the values dynamically from a database, then that's a whole other story. Anyways, you can reference this array like this:
ArrayAdapter<CharSequence> adapter2 = ArrayAdapter.createFromResource(this, R.array.EU_types, android.R.layout.simple_spinner_item);
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Then you simply use that to fill your spinner like this:
Spinner yourspinner = (Spinner) findViewById(R.id.YOUR_SPINNER);
yourspinner.setAdapter(adapter2);
Hopefully one of those will be useful to you.