Android Get Id of Value in AutoCompleteTextView - android

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.

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

How to set string array inversely on android spinner

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

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>

Get item value in arrayadapter

I want to get the value of the selected item in the spinner. I am using array adapter.
<string-array name="my_list">
<item value="">---Select the value from the List---</item>
<item value="value1">data1</item>
<item value="value2">data2</item>
<item value="value3">data3</item>
<item value="value4">data4</item>
<item value="value5">data5</item>
</string-array>
If I select the "data1" in my spinner, I want to get the "value1"..not "data1"
Anyone help me. Quick response helps me a lot. thanks in advance.
You have to add a values string-array like this instead of value attribute
<string-array name="my_list">
<item value="">---Select the value from the List---</item>
<item>data1</item>
<item>data2</item>
<item>data3</item>
<item>data4</item>
<item>data5</item>
</string-array>
<string-array name="my_list_values">
<item value="">---Select the value from the List---</item>
<item>value1</item>
<item>value2</item>
<item>value3</item>
<item>value4</item>
<item>value5</item>
</string-array>
To retrieve the values from the my_list_values, You need to write this in onItemSelected function in listener
String selectedValue = getResources().getStringArray(R.array.my_list_values)[parent.getSelectedItemPosition()];
You cann't do something like you have done directly as Android doesn't supports entryValues
I don't think that's possible, tried it myself a while back.
Maybe you just do what I did and create a second array holding the values you want on the same index?

Categories

Resources