How to set string array inversely on android spinner - android

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)));

Related

How to set StringArray value on click on array item in android?

I'm working with an android spinner. Here I have two arrays in my XML like below and I'm showing car array in mine spinner.
<string-array name="car">
<item>128i Coupe</item>
<item>M3 Coupe</item>
<item>M5 Sedan</item>
</string-array>
<string-array name="value">
<item>1</item>
<item>0</item>
<item>2</item>
</string-array>
Now I can call the any of them in my Java file like this
String[] BMW_Model = MainActivity.this.getResources().getStringArray(R.array.car);
I can get/print the value of this item on click by this way
String td = spinnerManufacture.getSelectedItem().toString();
Now what I need is when I click on an item of my car array I should display the value of my second value array according to the position. As an example.
If I click on M5 Sedan from my spinner item it should show 2 in a Toast message from my second array.
Any kind of suggestion will be highly appreciated.
Since you have completely different arrays, the solution to your problem could be to get the position of the pressed spinner element and get the value of the second array at this index.
int position = spinner.getSelectedItemPosition()
String[] valueArray = context.getResources().getStringArray(R.array.value);
Toast.makeText(context, valueArray[position],Toast. LENGTH_SHORT).show();
If you need to display toast on click, then you need to set clickListener
spinner.setOnItemSelectedListener(new

ListView and R string directory

I have a project (meaning an android app) created and used a custom ListView with three TextView UI for each column. But the app is also designed to support multi-language (at least three) using strings.xml file to fetch the selected language when user selects. Thus, the case here is, I have to write for the ListView String [] example {"a", "b", "c"} in java. I tried to do something like this String [] example {R.string.a, R.string. b, R.string. c} but the compiler says Error " illegal array ".
Now my question: Is it possible to reference the String [] { } of the ListView to strings.XML files.
Thank in advance and any example code or link I'm appreciated.
If your elements of the array are constant, you can make string array in strings.xml and access that array in code.
Put the following in strings.xml
<string-array name="example">
<item>a</item>
<item>b</item>
<item>c</item>
</string-array>
Access this array in code as:
String examples[] = getResources().getStringArray(R.array.example);
you can not use the direct reference to the String array, you have to get the value of String resource via getString()
like this
String[] example = {
getString(R.string.a), getString(R.string.b), getString(R.string.c)
};

Android XML Parser - Creating Spinner

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);

Can add ListView items without using Adapter? too cumbersome for static array display

I'd like to implement ListView and each item have multiple tags like StackOverflow list (a kind of master-detail style view).
Each tag in items is enumerated by tags string array.
The tags are no need to be changed/filtered when it first showed so that I think it does NOT need to use an adapter (an adapter is for binding between data model and view, right?). Moreover, I think using adapter in each item may cause performance issue in order to process additional bindings.
Is there any workaround to add ListView items without using Adapter?
For reference, in C#, listView.Items.Add("item1"); can display items simply.
As #Android-Developer noted, it is impossible to add arrays to ListView without Adapter.
listViewTopics.setAdapter(new ArrayAdapter<Topic>(CurrentActivity.this, R.layout.item_tag, topics));
above single-line code is simplist way to show array items(topics in this example) to ListView.
There is no option to create list with adapter. but yes you can use the default Array Adapter for list view.
List<String> values = new ArrayList<>();
values.add("Lesson 1.");
values.add("Lesson 2.");
values.add("Lesson 3.");
values.add("Lesson 4.");
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, values);
listView.setAdapter(arrayAdapter);
Try this..
In string.xml do this first..
<string-array name="Entries">
<item>Item 1</item>
<item>Item 2</item>
<item>Item 3</item>
</string-array>
Then do this in your ListView in xml..
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/Entries"></ListView>

Combine Multiple string-array Resources

I have multiple different categories and in each category I use one string resource to populate a ListView like so:
<string-array name="list">
<item>item1</item>
<item>item2</item>
<item>item3</item>
</string-array>
I call them like so:
String[] list = getResources().getStringArray(R.array.list);
How can I combine these into one String[] to use as an "All" list.
you can combine two Array List into a single one Like this:
ArrayList<String> first;
ArrayList<String> second;
second.addAll(first);
Since you use Simple Array there a multiple way to change arrays to arrayList, you can fetch for it on the net. But why you are not storing them on a single array in xml file that you call it global_content for exemple. You are waisting time and memory for this!!

Categories

Resources