Can't select Spinner item when receive server data - android

i can't select spinner item that i received from server.
i have some Person Class objects come from server in a list of array. then i collect the names of objects from PersonList to PersonNames
personList = response.body();
for(Person p : personList){
personNames.add(p.getPersonFullName());
}
im using retrofit 2 to receive data from server.
Fortunately spinner show names (in a first look in spinner no item selected but when i press the dropdown menu the names shown all!).
But i can't select.But when i add some item manualy like PersonNames.add("Hi"); Spinner works fine and select item in offline work. But in getting data from server and turn them to another list , i can't use spinner and select item. what's going wrong?
final ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, personNames);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerPersons.setAdapter(dataAdapter);
spinnerPersons.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
ePersonUserName.setText(personNames.get(i));
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});

Related

How to populate 2 spinner based on selection of 1 spinner where data comes from server in android using volley?

I am new to android and i want to make 3 spinner, 1 is to work, 2 is stain and 3rd is price. The data should come from server through JSON. If i select Metier example plumber then by selection of plumber i should get the list of tasks related to the plumber. . Price should get the price. The spinner should change according to 1st spinner.
All you have to do that give a network call on first spinner selection
for example:
if plumber is selected in first spinner post this string to server and get the data regarding this string "plumber"
after you get the data populate the data with second and third spinner
Hope its help :)
final Spinner spinner = (Spinner)findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String items = spinner.getSelectedItem().toString();
//Here give a network call
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});

Dynamic array in spinner issue

Scenario : I have 2 spinners with one adapter and global array. When any one select an item from one spinner then that item will delete from global array.Problem is when an item suppose select with 0 index again after deleting that item from global array and another item at acquire position with 0 index . If I will select that 0 index changed item then it will not select other than all items will select.
So can any one explain or suggest what i want to do in this scenario.
List<String> spinnerArray = new ArrayList<String>();
HashMap<String, String> index_val = new HashMap<String, String>();
ArrayAdapter<String> adapter_left;
spinnerArray.add("None");
spinnerArray.add("Task");
spinnerArray.add("Priority");
spinnerArray.add("Deadline");
spinnerArray.add("Assigned to");
spinnerArray.add("Created by");
spinnerArray.add("Closed by");
spinnerArray.add("Category");
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, R.layout.spinner_selected_item, noneArray);
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
adapter_left = new ArrayAdapter<String>(activity, R.layout.spinner_selected_item_left, spinnerArray);
adapter_left.setDropDownViewResource(R.layout.spinner_dropdown_item);
task_spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if(!index_val.get("1").equalsIgnoreCase("None")){
spinnerArray.add(index_val.get("1"));
}
current_selected_val = adapterView.getItemAtPosition(i).toString();
index_val.put("1", current_selected_val);
if(!current_selected_val.equalsIgnoreCase("none")) {
spinnerArray.remove(spinnerArray.indexOf(current_selected_val));
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
Your Array Adapter should be notify every time, when ever you modify an array list like adding, deleting or updating.
So notify your adapter.
I hope this might help you.
It's not possible, because spinner maintains their items on behalf of index instead of actual items.

How to send value to server based on user clicked in spinner item in android?

I have a spinner in my activity which contain Operators name ,what I want when user selected item from spinner I want to send another value to server,like
if user select "Airtel" from spinner I have to send "AR" to server same like other items .how can I do that.
code:-\
<string-array name="operators">
<item>Select Operator</item>
<item>Aircel</item>
<item>Airtel</item>
<item>BSNL</item>
<item>Idea</item>
<item>Vodafone</item>
<item>MTNL Delhi</item>
<item>MTNL Mumbai</item>
</string-array>
here when user select item from above list I have to send value from below list according to above items.
<string-array name="Operators_Code">
<item>AC</item>
<item>AT</item>
<item>BS</item>
<item>ID</item>
<item>VD</item>
<item>MT</item>
</string-array>
A Spinner, similar to ListView, RecyclerView, etc., is an "adapter backed" View. That is, it gets the items to be displayed from an Adapter.
When you set the entries to be shown using android:entries="#array/operators, Spinner internally creates an Adapter with the supplied array items. This simple solution, however, doesn't support complex objects.
For your use case, you'll have to create a custom Adapter for your Spinner.
public class Operator {
String name;
String code;
#Override
public String toString() {
return name;
}
}
final List<Operator> operators = new ArrayList(7);
operators.add(new Operator("Select operator", null));
operators.add(new Operator("Aircel", "AC"));
operators.add(new Operator("Airtel", "AC"));
....
Next, set the adapter on your spinner:
final ArrayAdapter<String> operatorsAdapter = new ArrayAdapter<>(context, android.R.layout.simple_dropdown_item_1line, operators);
spinner.setAdapter(operatorsAdapter);
That's it. Now if you want to listen to user's selections, add a listener to your Spinner by:
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
final Operator selectedOperator = operatorsAdapter.getItem(position);
final String selectedOperatorCode = selectedOperator.code;
// TODO: Send the selected operator to the server.
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Android: How to connect Spinner selection with AutoCompleteTextView

I'm trying to connect actions in Spinner with AutoCompleteTextView. My goal is when user selects some item in spinner, AutoCompleteTextView suggestion list needs to be changed.
Example:
Spinner items: Cats, Dogs, Horses
String1: cat1, cat2, cat3, ...
String2: dog1, dog2, dog3, ...
String3: horse1, horse2, horse3 ...
So when user chooses 'Dogs' in spinner and after that clicks on AutoCompleteTextView, he will get following suggestions after he begins to write: dog1, dog2, dog3. Similar for Cats and Horses.
I can't find a solution for this problem.
I tried to put onClickListener to AutoCompleteTextView but it seems that doesn't work.
autoCompleteTextView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
//some actions
});
And there should be some better solution because after the screen opens that AutoCompleteTextView is already selected.
Check out the API for AutoCompleteTextView - http://developer.android.com/reference/android/widget/AutoCompleteTextView.html
The suggestions are populated from a data adapter, so you could modify the data adapter for your autoCOmpleteTextView after the user makes their spinner selection:
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id)
{
String[] choices;
// set the adapter for the autoCOmpleteTextView here based on what was selected
if(DOGS) {
choices = new String[] {"dog1", "dog2", "dog3"};
} else if (CATS) {
choices = new String[] {"cat1", "cat2", "cat3"};
}
// etc...
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_dropdown_item_1line, choices);
myTextView.setAdapter(adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parentView)
{
// nothing
}
});

2 dependent spinner

if i have 2 spinner the second depend in the first
i will retriever the info of the second spinner from MySQL database depend in the choose of first spinner i succuflly get the id of the first spinner but i do not
how to send it to the other cause it not work with me
i have class MainActivity that have:
new LoadAllCourses().execute(); //first spinner generation
new LoadAllSection().execute(); //second spinner
in class LoadAllCourses extends AsyncTask<String, String, String>
adapter1 = new MyCustomAdapter(MainActivity.this,
android.R.layout.simple_spinner_item,
coursesList);
spinner1.setAdapter(adapter1); // Set the custom adapter to the spinner
spinner1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long id) {
HashMap<String, String> map12 = coursesList.get(position);
String id3 = map12.get("CourseID");
// Do something
Log.d("All coursesdddddddddddddddddddddddddddddddddddd: ", id3);
// Do something
}
#Override
public void onNothingSelected(AdapterView<?> adapter) {
}
});
now i want to get the id3 and send it to
class LoadAllSection extends AsyncTask<String, String, String>
but it is not work
how can i solve it if i have as i said
If I have understood your question, you require the selection on the first spinner to for the basis of a search from a database to populate the second spinner.
If in adapter 1 (adapter1 = new MyCustomAdapter(MainActivity.this,
android.R.layout.simple_spinner_item,
coursesList);) courseList is an array, one can get the selected item from the spinner in the setOnItemSelectedListeneras follows courseList [position].
Having obtained the selected item, you then require a function to perform the query in the database, say load_all_selection(courseList [position]) which should return an array of results from your database.
Since it returns an array you can define adapter 2 as follows
adapter2 = new MyCustomAdapter(MainActivity.this,
android.R.layout.simple_spinner_item,
load_all_selection(courseList [position]));
and assign it to the second spinner spinner2.setAdapter(adapter2);
All this can be done from within the setOnItemSelectedListener part, except maybe for the definition of the load_all_selection(...) function.
I hope I understood you question and this helps. If it doesn't you can still look around for a different solution.

Categories

Resources