2 dependent spinner - android

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.

Related

Can't select Spinner item when receive server data

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

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.

Spinner that trigger other spinner

I want to create an application with two Spinners. When you select one of the items inside the first spinner, the second spinner will get only a few items (depends on which item selected).
For example: in the first spinner I select "Mazda", and then on the second I will be able to see only Mazda's models, not BMW, Ford etc. Can I do something like this?
I tried to create a spinner without items, and set the entries of the spinner on the XML when item selected, but there is no method to do this.
I don't create Lists. I want to create string-array resources in my strings.xml, and give that array to the second spinner.
Of course it is doable and pretty straightforward. Spinners works with the model provider, which in Android it is called Adapter pattern. So what you can do is put in your first spinner an adapter that holds all the brands of your cars, and listen on the first spinner for changes using setOnItemSelectedListener.
When the item changes, you can create a new adapter instance for the second spinner with the only values that are valid in this case.
Try following code. I've organized the sample data in a HashMap but you can do it in your own way.
// hashmap object containing data of spinner1 as 'keys' with relevant
// data of spinner2 in List<String> object as 'values'
final Map<String, List<String>> data = new HashMap<>();
data.put("A", Arrays.asList("1","2","3","4"));
data.put("B", Arrays.asList("4", "5"));
data.put("C", Arrays.asList("6", "7", "8", "9"));
data.put("D", Arrays.asList("10", "11", "12"));
data.put("E", Arrays.asList("13", "14"));
// obtaining a string array containing keys(data of spinner1) of above hashmap
final String[] dataSpinner1 = new String[data.keySet().size()];
data.keySet().toArray(dataSpinner1);
// initializing an string type, ArrayAdapter for spinner1
// you will need to pass activity context, layout for the spinner item and
// spinner content(as string array) as arguments to create an array adapter
final ArrayAdapter<String> spinner1Adapter = new ArrayAdapter<String>(context, R.layout.spinner_layout, dataSpinner1);
spinner1.setAdapter(spinner1Adapter);
// setting listner for spinner1 to trigger when an spinner item is being
// clicked by the user
spinner1.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// obtaining relevant data for spinner2
List<String> dataSpinner2 = data.get(dataSpinner1[position]);
// crating an setting array adapter for spinner2
ArrayAdapter<String> spinner2Adapter = new ArrayAdapter<String>(context, R.layout.spinner_layout, dataSpinner2);
spinner2.setAdapter(spinner2Adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});

Android updating a spinner based on "Parent" spinner's selection - .notifyDataSetChange() not firing?

So, I have two spinners, let's call the first spinner Parent (account_type_spinner) and the second spinner Child (account_name_spinner). Please notice in the following code the ArrayAdapter initialization for the Child (account_name_spinner), I am feeding it a string array of account names I previously queried before the following lines of code (account_name_array):
//---define spinner objects as variables, assign adapters and listeners---
account_type_spinner = (Spinner) findViewById(R.id.account_type_spinner);
account_type_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, account_type_array);
account_type_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
account_type_spinner.setAdapter(account_type_adapter);
account_type_spinner.setOnItemSelectedListener(new SpinnerSelectionListener());
account_name_spinner = (Spinner) findViewById(R.id.account_name_spinner);
account_name_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, account_name_array);
account_name_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
account_name_spinner.setAdapter(account_name_adapter);
Selection in the Parent spinner triggers my "SpinnerSelectionListener" which is simply my class implementing OnItemSelectedListener. This class fires obviously each time a selection is made in the Parent spinner and the code looks like the following:
public class SpinnerSelectionListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String spinner_selection = parent.getItemAtPosition(pos).toString();
if(spinner_selection.contentEquals(INCOME)) {
//---grab Income type accounts from db and build array---
db.open();
account_name_array = db.getAccounts(INCOME);
account_name_adapter.notifyDataSetChanged();
dr_amount_textview.setVisibility(View.GONE);
dr_amount.setVisibility(View.GONE);
cr_amount_textview.setVisibility(View.VISIBLE);
cr_amount.setVisibility(View.VISIBLE);
db.close();
} else {
//---grab Expense type accounts from db and build array---
db.open();
account_name_array = db.getAccounts(EXPENSE);
account_name_adapter.notifyDataSetChanged();
cr_amount_textview.setVisibility(View.GONE);
cr_amount.setVisibility(View.GONE);
dr_amount_textview.setVisibility(View.VISIBLE);
dr_amount.setVisibility(View.VISIBLE);
db.close();
}
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
The code above should update the Child (account_name_spinner) with INCOME or EXPENSE accounts whenever the Parent's selection changes from INCOME to EXPENSE or vice versa (this is an accounting app). The updating of the Child's spinner list should be facilitated by the "account_name_adapter.notifyDataSetChanged();" however nothing is happening.
I looked into this issue further on StackOverflow and found that I must .clear() or .remove() items from my Child's ArrayAdapter (account_name_adapter) before the list will update, however, when I try "account_name_adapter.clear();" before notifying the ArrayAdapter I get an error that the operation is illegal. Any ideas what I am doing wrong?
You can just reassign the adapter after you got the updated array (account_name_array)
account_name_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, account_name_array);
account_name_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
account_name_spinner.setAdapter(account_name_adapter);
EDIT: To the adapter you don't pass a reference, so when you update the array the adapter still have the same data. Calling .notifyDataSetChanged() do not update the adapter with the new array.

Combobox in Android

I need something like a combobox in access in android, i want to choose the customer per name, but in the background the id should be chosen. how to do?
In android comboboxes are called spinner. Nevertheless, gnugu has posted in his blog his own implementation of a combobox. http://www.gnugu.com/node/57
A simple example of an spinner would be the following.
First, edit your XML code with something like this
Spinner android:id="#+id/Spinner01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
Your java code should include something like this, the options are very intuitive. If you are using eclipse it will suggest you some options
public class SpinnerExample extends Activity {
private String array_spinner[];
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Here come all the options that you wish to show depending on the
// size of the array.
array_spinner=new String[5];
array_spinner[0]="option 1";
array_spinner[1]="option 2";
array_spinner[2]="option 3";
array_spinner[3]="option 4";
array_spinner[4]="option 5";
Spinner s = (Spinner) findViewById(R.id.Spinner01);
ArrayAdapter adapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array_spinner);
s.setAdapter(adapter);
}
}
An alternate solution to the need to link Customer ID to the selected Item.
To have a simple selector with text you cause make use of the array resources
Setup the Spinner in XML
<Spinner android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/colors"/>
If you need more data linked with the spinner you can use Objects to populate the spinner.
The default functionality of an ArrayAdapter is to call toString() on any object and pass that to the view.
if (item instanceof CharSequence) {
text.setText((CharSequence)item);
} else {
text.setText(item.toString());
}
You can implement toString() in your object and it will display correctly in the spinner. Then to get the data back from the array you can add a handler onto ItemSelected and get the object back from the seed array or the ArrayAdapter.
ArrayAdapter adapter = new ArrayAdapter(activity, android.R.layout.simple_spinner_item, arrayOfObjects);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
{
Log.d(arrayOfObjects[position]._id);
}
});

Categories

Resources