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
Related
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);
);
How to get value to dynamically add multiple spinner where spinner id is same.i used 'String spin = parent.getSelectedItem().toString();' but i get all time last spinner value.Plz help me?
you can create array list of spinner
ArrayList<Spinner> listSpinner = new ArrayList<>();
listSpinner.add(spinner1);
listSpinner.add(spinner2);
listSpinner.add(spinner3);
for(int i=0;i<listSpinner.size();i++){
String p1 = listSpinner.get(i).getSelectedItem();
}
get different value for all spinner out side for loop and store it in arraylist.
ArrayList<Spinner> listSpinner = new ArrayList<>();
ArrayList<ArrayList<String>> status_list = new ArrayList<>();
status_data.add("" + snapshot.getValue());// getting data
status_list.add(status_data);
for (int i = 0; i < 10; i++) {
View v = LayoutInflater.from(AssetCodingDetails.this).inflate(R.layout.custom_asset_coding_label, null);
Spinner spinner_status = (Spinner) v.findViewById(R.id.spinner_status);
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), R.layout.spinner_item, status_list.get(i));
spinner_status.setAdapter(adapter);
listSpinner.add(spinner_status);
}
for(int i=0;i<listSpinner.size();i++){
String p1 = listSpinner.get(i).getSelectedItem().toString();
}
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?
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
I have a little problem with getting all data from SQLite database and showing them in ListView. The code I'm using is showing only the last item. Here is the code which I'm using :
ListView lv1 = (ListView) findViewById(R.id.saved_codes_listview);
SystemDatabaseHelper systemDbHelper = new SystemDatabaseHelper(this, null, 1);
systemDbHelper.initialize(this);
String sqlQuery = "SELECT code_id, code_string FROM saved_codes";
Cursor cursor = systemDbHelper.executeSQLQuery(sqlQuery);
if(cursor.getCount()==0){
Log.i("No Saved Codes","There is no Saved Codes.");
} else if(cursor.getCount()>0){
for(cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()){
String codeString = cursor.getString(cursor.getColumnIndex("code_string"));
ArrayList<String> codes = new ArrayList<String>();
codes.add(codeString);
Log.i("Code String","Code String : "+codeString);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, codes);
lv1.setAdapter(adapter);
}
}
I have 3 entries in my database as an example : shosho, bosho, gosho and as a result I have only gosho in my listview. Any idea how to fix that?
Thanks in advance!
Change your code so that you initialize array list before for loop, and also set ArrayAdapter AFTER the loop.
ArrayList<String> codes = new ArrayList<String>();
for(cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()){
String codeString = cursor.getString(cursor.getColumnIndex("code_string"));
codes.add(codeString);
Log.i("Code String","Code String : "+codeString);
}
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, codes);
lv1.setAdapter(adapter);
Like this:
ArrayList<String> codes = new ArrayList<String>();
for(cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()){
String codeString = cursor.getString(cursor.getColumnIndex("code_string"));
codes.add(codeString);
Log.i("Code String","Code String : "+codeString);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, codes);
lv1.setAdapter(adapter);
}
You re-create a new ArrayList<String> codes in every loop so the last loop the list containing only one element, which is the last.
Change for(cursor.move(0); cursor.moveToNext(); cursor.isAfterLast())
to for(cursor.move(0);; cursor.isAfterLast(); cursor.moveToNext())
or use
while(cursor.isAfterLast())
cursor.moveToNext();