Unable to get the json data on Spinner in android - 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);
}

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

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?

How to load a Spinner with Simple Json Array using json class in Android

I have a Json Array and i want it to load it in Android Spinner. I tried many things like using Json objects and array list, but it is not giving me correct result. I am using a web service which is returning me in the form of JSON array. I used Json Array class but it returns whole object.
Please see Json array example.
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://microsoft.com/webservices/">
<string>1</string>
<string>2</string>
<string>3</string>
<string>4</string>
</ArrayOfString>
Although your JSON looks like xml, i will provide code for getting items of JSON array into spinner items.
public static final String JSON = "[\"1\", \"2\", \"3\", \"4\", \"444\"]";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ArrayList<String> itemsList = new ArrayList<String>();
try {
JSONArray array = new JSONArray(JSON);
for (int i=0; i < array.length(); i++) {
String s = (String) array.get(i);
itemsList.add(s);
}
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<String> adapter =
new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, itemsList);
spinner.setAdapter(adapter);
} catch (JSONException e) {
e.printStackTrace();
}
}
For parsing XML you can use XmlPullParser http://developer.android.com/reference/org/xmlpull/v1/XmlPullParser.html

The constructor ArrayAdapter<String>(AddTravauxInterne.sdloadingTask, int, List<String>) is undefined

I am trying to populate a spinner widget with dynamic data. Firstly, i receive a jsonarray that i break and saved to my model as follows
list = new ArrayList<String>();
for (int nombre=0;nombre<nombre_tache;nombre++){
JSONObject tache= (JSONObject) response.get(nombre);
tid= tache.getString("tid");
task= tache.getString("task");
travauxInterne.add(new TravauxInterne(tid,task));
list.add(travauxInterne.get(nombre).task.toString());
}
where travauxInterne is a global arraylist
ArrayList<TravauxInterne> travauxInterne = new ArrayList<TravauxInterne>();
When i set the spinner adapter as follows the error occurs.
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
and i get the following error:
The constructor ArrayAdapter<String>(AddTravauxInterne.sdloadingTask, int, List<String>) is undefined
It looks like you are doing this within your inner AsyncTask so this refers to that, which is causing the error.
Change it to:
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(NameOfYourActivity.this, android.R.layout.simple_spinner_item,list);

Categories

Resources