Spinner and HTML select - android

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?

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

get value from Dynamically add multiple spinner in android?

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>> s‌​tatus_list = new ArrayList<>();
status_data.add("" + snapshot.getValue());// getting data
s‌​tatus_list.add(status_data);
for (int i = 0; i < 10; i++) {
View v = LayoutInflater.from(AssetCodingDetails.this).inflate(R.layou‌​t.custom_asset_codin‌​g_label, null);
Spinner spinner_status = (Spinner) v.findViewById(R.id.spinner_status);
ArrayAdapter adapter = new ArrayAdapter(getApplicationContext(), R.layout.spinner_item, s‌​tatus_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();
}

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

Unable to get the json data on Spinner in android

I'm new to android and having trouble with the JSON part...
I'm developing a app where I'm retrieving the data from the database.
Although response is successful but the data isn't displaying on the spinner.
I can see the data is retrieved and can be seen in log.
Please help I can't see where am I wrong.
12959-13040/com.xlint.atish.nycx E/Response:﹕ > get_word_file_names.php
{"word":[{"id":"1","word":"Sachin.docx"},{"id":"2","word":"Disable right click java script--.docx"}]}
//Populating spinner
private void populateSpinner() {
List<String> lables = new ArrayList<String>();
for (int i = 0; i < files_name.size(); i++) {
lables.add(files_name.get(i).getName());
}
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, lables);
spinnerAdapter
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
dropList.setAdapter(spinnerAdapter);
}

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