change values in 2nd dropdown based on another dropdown android - android

I've 2 drop downs in Android. I want to change contents of 2nd drop down based on values selected in 1st drop down. Here is the code.
<string-array name="categoriesSpinner">
<item>ACCESS</item>
<item>AVAILABILITY - PERFORMANCE</item>
<item>FUNCTIONALITY</item>
<item>INQUIRY</item>
<item>DATA ERROR</item>
<item>ERROR MESSAGE</item>
</string-array>
UI
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/categorySpinner"
android:layout_width="match_parent"
android:layout_height="45sp"
android:hint="Category"
android:layout_marginTop="#dimen/margin_small"
android:background="#drawable/bg_drawable" />
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/subcategorySpinner"
android:layout_width="match_parent"
android:layout_height="45sp"
android:hint="Sub-category"
android:layout_marginTop="#dimen/margin_small"
android:background="#drawable/bg_drawable" />
Java:
public AppCompatSpinner mTextView, getmTextView;
//AppCompatSpinner
mTextView = findViewById(R.id.categorySpinner);
String[] categories = getResources().getStringArray(R.array.categoriesSpinner);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, categories);
arrayAdapter.notifyDataSetChanged();
mTextView.setAdapter(arrayAdapter);
String option = String.valueOf(mTextView.getSelectedItem());
getmTextView = findViewById(R.id.subcategorySpinner);
if (option.contentEquals("ACCESS")) {
List<String> list = new ArrayList<>();
list.add("ACCOUNT LOCKED");
list.add("RESET PASSWORD");
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, list);
arrayAdapter1.notifyDataSetChanged();
getmTextView.setAdapter(arrayAdapter1);
}
if (option.contentEquals("AVAILABILITY - PERFORMANCE")) {
List<String> list = new ArrayList<>();
list.add("LIMITED - DEGRADED");
list.add("UNAVILABLE - DOWN");
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, list);
stringArrayAdapter.notifyDataSetChanged();
getmTextView.setAdapter(stringArrayAdapter);
}
When I run the code in my android device, value of 2nd drop down doesn't change when clicked on 2nd value in first drop down. How can I fix it?

Move this code to onItemSelected of spinner.
mTextView.setOnItemSelectedListener(//the remaining code
String option = String.valueOf(mTextView.getSelectedItem()); //Don't forget to move this here otherwise it won't be updated.
if (option.contentEquals("ACCESS")) {
List<String> list = new ArrayList<>();
list.add("ACCOUNT LOCKED");
list.add("RESET PASSWORD");
ArrayAdapter<String> arrayAdapter1 = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, list);
arrayAdapter1.notifyDataSetChanged();
getmTextView.setAdapter(arrayAdapter1);
}
if (option.contentEquals("AVAILABILITY - PERFORMANCE")) {
List<String> list = new ArrayList<>();
list.add("LIMITED - DEGRADED");
list.add("UNAVILABLE - DOWN");
ArrayAdapter<String> stringArrayAdapter = new ArrayAdapter<>(mContext, R.layout.support_simple_spinner_dropdown_item, list);
stringArrayAdapter.notifyDataSetChanged();
getmTextView.setAdapter(stringArrayAdapter);
);

Related

Searchable spinner doesn't search after space

I'm using a searchable spinner library in my app.
The custom library I using is
com.toptoche.searchablespinner:searchablespinnerlibrary:1.3.1
I'm getting values to spinner from API. The problem I am facing is whenever I search for a keyword after a space is not working. For example I have these data in my spinner
GLASS PIPE
GLASS SHEET
GLASS PLATE
and if i searched "GLASS", The three data will appear. But whenever I search for
"GLASS PIPE", no result will appear.Help me to solve this.
MY CODE BINDNG DATA TO SPINNER
ArrayList<String> employeeNames = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(InsertRequisition.this, R.layout.item_spinner_black, employeeNames);
dataAdapter.setDropDownViewResource(R.layout.item_spinner);
employeeSpinner =(Spinner)findViewById(R.id.employee_name_value);
for (int i=0;i<common.dataArrayList.size();i++){
String[] data = new String[3];
data[0]=common.dataArrayList.get(i)[0];
data[1]=common.dataArrayList.get(i)[1];
data[2]=common.dataArrayList.get(i)[2];
employeeList.add(data);
}
for(int i=0;i<employeeList.size();i++){
employeeNames.add(employeeList.get(i)[1]+" - "+employeeList.get(i)[2]);
}
employeeSpinner.setAdapter(dataAdapter);
dataAdapter.setDropDownViewResource(R.layout.item_spinner);
}
My Spinner code
<com.toptoche.searchablespinnerlibrary.SearchableSpinner
android:id="#+id/company_name_value"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
Check following code for AutoCompleteTextView
autoCompleteTextView=findViewById(R.id.autoCompleteTextViewID);
employeeNames = new ArrayList<String>();
// getdata & store in dataList
dataAdapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, employeeNames );
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
autoCompleteTextView.setThreshold(1);
autoCompleteTextView.setAdapter(dataAdapter);
autoCompleteTextView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String selectedStr = parent.getItemAtPosition(position).toString();
}
});

How to add a default selected value in android dropdown

I have a dropdown which takes its values from a database, I then want to add a default value to the existing drop down values. In this case the default value would be the selected value.
StringBuilder strbuilder = new StringBuilder();
ArrayList<String> arr_list = new ArrayList<String>();
List<String> m = new ArrayList<String>();
//This get the data values from database
m = db.getData();
int ms = m.size();
String str = strbuilder.toString();
String[] str1 = str.split(",");
try {
if (ms > 1) {
//This is my default value
m.add("Default Value");
for (int i = 0; i < ms; i++) {
m.get(0);
}
} else {
m.get(0);
}
} catch (Exception e) {
e.printStackTrace();
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_dropdown_item, m);
dropdown.setAdapter(adapter);
The codes above is able to add the values from the database to the dropdown and the Default Value is added but it doesn't appear to be the selected value.
For instance if the values from the database are "Apple, Mango,Pineapple" and the default value is "ALL FRUITS".
EXPECTED DROPDOWN
ALL FRUITS
Apple
Mango
Pineapple
Please how can I do make the default value the selected value? Thanks in advance.
I think you need below code.
List<String> categories = new ArrayList<String>();
categories = db.getData();
categories.add(0,"your_default_value");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setSelection(0);
Remember when you want to add any value to the very first of Arraylist you just do
arraylist.add(0,"value");
This will add the value to the very first index of arraylist and push all other element down.
In case you need any other element to be selected not first one
Spinner spinner = (Spinner) findViewById(R.id.spinner);
List<String> categories = new ArrayList<String>();
categories.add("Automobile");
categories.add("Business Services");
categories.add("Computers");
categories.add("Education");
categories.add("Personal");
categories.add("Travel");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
int i=categories.indexOf("Education");
spinner.setSelection(i);
If the default value is always going to be at the 0 index, just get a reference to your dropdown/spinner, then set:
spinner.setSelection(0);
And here is a previously answered question detailing how to set it by value, if you'd rather do that.
https://stackoverflow.com/a/4228121/3299157

Android: Spinner setPadding not work

I use this code for set padding to spinner ( not for children )
Spinner spnCategory = (Spinner) findViewById(R.id.spnCategory);
List<String> list = new ArrayList<String>();
list.add("UP");
list.add("Down");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(R.layout.dropdown_item);
spnCategory.setPadding(15, 5, 2, 4);
spnCategory.setAdapter(dataAdapter);
But this code not work
Try removing dataAdapter.setDropDownViewResource(R.layout.dropdown_item);, it should work fine

Spinner and HTML select

I have spinner for displaying categories. ((String)categorySpinner.getSelectedItem())) gives the category name.
The spinner is initialized as given below:
List<String> list = new ArrayList<String>();
for (int i = 0; i < category.length; i++) {
list.add(category[i].getName());
}
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(
android.R.layout.simple_spinner_dropdown_item
);
shoppingItemCategorySpnr.setAdapter(dataAdapter);
Is there any idea to make spinner like HTML select-tag so that I can display category name and get category id?

Using Multiple Spinners, But Only One Spinner Has All The Data

I have two spinners and two arrays. However, one spinner receives both arrays while the other receives no values from either of the two arrays. Note: I do not want to use radio buttons as the data is shortened for review.
final ArrayList<String> serialnums = new ArrayList<String>();
serialnums.add("576798");
serialnums.add("495874");
serialnums.add("345667");
serialnums.add("956345");
final ArrayList<String> carrys = new ArrayList<String>();
serialnums.add("R");
serialnums.add("L");
serialnums.add("F");
serialnums.add("B");
s1 = (Spinner) findViewById(R.id.spinnerSerial);
spinnerCarry = (Spinner) findViewById(R.id.spinnerCarry);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, serialnums );
ArrayAdapter<String> adapterCarrys = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, carrys );
s1.setAdapter(adapter);
spinnerCarry.setAdapter(adapterCarrys);
Note 2: Spinner s1 gets all the data
One spinner has all the data because you are assigning all the data to it. Change this:
final ArrayList<String> carrys = new ArrayList<String>();
serialnums.add("R");
serialnums.add("L");
serialnums.add("F");
serialnums.add("B");
to this:
final ArrayList<String> carrys = new ArrayList<String>();
carrys.add("R");
carrys.add("L");
carrys.add("F");
carrys.add("B");
You are adding "R" "L" "F" "B" to serialnums add it to carrys

Categories

Resources