Spinner in fragments pop up delayed - android

I created some fragments in an Activity class as tabs where you can switch through and inside these I have various Spinner. The data I'm getting from a SQL-database with JSONParser and the Spinner gets created in the fragment classes as follow:
spinnerMaterial = (Spinner) view.findViewById(R.id.Spinner_material);
List<String> lables_material = new ArrayList<String>();
for (int i = 0; i < materialList.size(); i++) {
lables_material.add(materialList.get(i).getMaterial());
}
// Creating adapter for spinner
ArrayAdapter<String> spinnerAdapter_material = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_spinner_item, lables_material);
// Drop down layout style - list view with radio button
spinnerAdapter_material
.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinnerMaterial.setAdapter(spinnerAdapter_material);
// spinner item select listener
spinnerMaterial.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view,
int position, long id) {
Object item = adapterView.getItemAtPosition(position);
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
// TODO Auto-generated method stub
}
});
Actually everything is working. I'm getting all the data and the Spinner are poping up. BUT not in a direct way. So I need to switch between the tabs (fragments) and then go back that the Spinner is showing the data. Strange fact is that when I try to debug and go over some breakpoints the Spinner pops up immediately (without switching tabs).
Do you have an idea what the problem could be?

Related

Creating dynamic spinners and getting value of every spinner, Whenever a spinner item changed, get all spinner values

I'm developing a e-com app. I'm trying to create dynamic spinner. Spinner is dependent upon product attributes. I'm able to create spinners also mapped data on them but I want to get all spinners selected item whenever a spinner change its data so I can match to correct variant of product.
Here is my code snippet
final List<Attribute> attributes = product_.getAttributes();
for (i = 0; i < attributes.size(); i++) {
ArrayList<String> spinnerArray = (ArrayList<String>) attributes.get(i).getOptions();
ArrayAdapter<String> spinnerArrayAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, spinnerArray);
Spinner spinner = new Spinner(getActivity());
spinner.setAdapter(spinnerArrayAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int position, long l) {
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
dynamicProductProperties.addView(spinner);
}
First, you need to set same instance of OnItemSelectListener to your all spinners. Let your activity or fragment implement OnItemSelectListener then call spinner.setOnItemSelectListener(this);
In onItemSelected method you can run this code to have selecteds.
ArrayList<String> selectedList = new ArrayList();
for (int i = 0; i < dynamicProductProperties.getChildCount(); i++) {
View view = dynamicProductProperties.getChildAt(i);
if (view instanceof Spinner){
String selected = (String) ((Spinner) view).getSelectedItem();
selectedList.add(TextUtils.isEmpty(selected) ? "" : selected);
}
}
Good luck
Emre

How do you create a dropdown answer in android?

I'm looking for something similar to a drop down menu but when you click on it a block of text drops instead of a list of clickable items. Then you should be able to click to close it again when finished. My app is an informational app so, I want to make a list of several of these within a glossary in my app.
Any ideas??
you should use Spinner in android:
Here is the example :
Spinner mSipnner = (Spinner)findViewById(R.id.spin_beneficiary_targetcode);
List<String> categories2 = new ArrayList<String>();
categories2.add("Choose code");
categories2.add("001");
categories2.add("002");
categories2.add("003");
categories2.add("004");
categories2.add("005");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories2);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
mSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
////Spinner item selected
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String targetCode_item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
//Toast.makeText(parent.getContext(), "Selected: " + category_item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
// attaching data adapter to spinner
mSpinner.setAdapter(dataAdapter);

How to get the value on change of the spinner in Fragment?

why doesn't fragment support spinners in it? I have the following fragment class and added spinners to it.
public class SettingFragment extends Fragment {
Spinner spinner1;
public List<String> genderitems, titleitems;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View rootview = inflater.inflate(R.layout.settinglayout, container,
false);
spinner1 = (Spinner) rootview.findViewById(R.id.usertitle);
titleitems = new ArrayList<String>();
setTitleSpinnerContent(rootview);
titleitems.add("Beginner");
titleitems.add("Novice");
return rootview;
}
private void setTitleSpinnerContent(View view) {
ArrayAdapter<String> adp = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_dropdown_item_1line, titleitems);
adp.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adp);
}}
How to get the spinner change value upon selection and add it a textview on the spinner.
What is that i am missing here? Thanks in advance.
I tried even adding onitemselectedlistener() but too the result is nil.
There are no differences in adding spinners in activity and fragment. If u want to get the spinner item changed listener implement like this..
spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
Toast.makeText(parent.getContext(),
"On Item Select : \n" + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
I'm a bit confused as to what your specific problem is, but if the problem is that you aren't seeing your spinner populated with the values 'Beginner' and 'Novice', it looks like have set your adapter on the empty list, and then added items to the list after the adapter is set. But you never called notifyDataSetChanged() on your adapter, so it doesn't know about your new values. Have you tried the simple solution of adding the items to your list before you set the adapter?
titleitems.add("Beginner");
titleitems.add("Novice");
setTitleSpinnerContent(rootview);

Order of OnItemSelectedListener Calls After OnCreate

I have two spinners in an Activity where the second Spinner's selection set is based on what the user picked for the first Spinner. I use a private class variable in the Activity which is set in the top Spinner's OnItemSelectedListener and then referenced in the bottom Spinner's OnItemSelectedListener to obtain the correct selection set.
This almost always works, but sometimes (mainly when app was run, not exited, and then started again by a user click some long time later) I get a null pointer exception in the second Spinner's OnItemSelectedListener due to this local variable not being set. This indicates to me that after the OnCreate that the second Spinner's OnItemSelectedListener was called before the first Spinner's.
Is there any method to force a certain order in the listeners being fired or is there a better design approach to handle this second Spinner's dependency on the first Spinner?
Example code:
package com.crashtestdummylimited.navydecoder;
public class Test extends Activity {
// Variable that at times is still null
private ReferenceData referenceData;
private void setupSpinnerFromArray (int spinnerId, String stringArray[], OnItemSelectedListener listener) {
Spinner spinner = (Spinner) findViewById(spinnerId);
ArrayAdapter <CharSequence> adapter = new ArrayAdapter <CharSequence>(
this, android.R.layout.simple_spinner_item, stringArray);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(listener);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_screen);
// Setup Top (main) Spinner
Spinner spinner1 = (Spinner) findViewById(R.id.mainDecodeSpinner);
ArrayAdapter<CharSequence> adapter1 = ArrayAdapter.createFromResource(
this, R.array.level0_list_array, android.R.layout.simple_spinner_item);
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner1.setAdapter(adapter1);
spinner1.setOnItemSelectedListener(new MainDecoderItemSelectedListener());
// Setup Bottom (dependent) Spinner
setupSpinnerFromArray(R.id.secondaryDecodeSpinner, R.array.level1_list_array, new SecondaryDecoderItemSelectedListener());
}
public class MainDecoderItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String selectedString = parent.getItemAtPosition(pos).toString();
if (selectedString.equals("AAA")){
// Problem variable is set
referenceData = new RatingCodes();
setupSpinnerFromArray(R.id.secondaryDecodeSpinner, referenceData.getKeys(), new SecondaryDecoderItemSelectedListener());
}
else if (selectedString.equals("BBB")){
// Problem variable is set
referenceData = new IMSCodes();
setupSpinnerFromArray(R.id.secondaryDecodeSpinner, referenceData.getKeys(), new SecondaryDecoderItemSelectedListener());
}
// TODO: Improve what occurs if no match which should not occur
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
public class SecondaryDecoderItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
String key = parent.getItemAtPosition(pos).toString();
// **** referenceData being null at this point has caused crashed ****
String value = referenceData.getValue(key);
// ... Update text on activity screen ...
}
public void onNothingSelected(AdapterView<?> parent) {
// Do nothing.
}
}
}
public void onItemSelected(AdapterView<?> parent,View view, int pos, long id)
{
if(pos == 0)
{ //when it loads on onCreate() then pos is always 0
//donothing
}
else
{ //If user manually select item
//do what you need to do on manual user selection
}
}

Display Data in Spinner,Android

I wanted to display my data into android spinner from database.
I used two spinner..
2nd spinner should reflect once 1st spinner item is selected, every thing is working fine..data is loaded into the second spinner, but not displaying into 2nd spinner when 2nd spinner item is selected.
Spinner1 = (Spinner)findViewById(R.id.createProfileCitySpinnerId);
Spinner2 = (Spinner)findViewById(R.id.createProfileStateSpinnerId);
//for 1st spinner.....(working)
final List<String> list1 = new ArrayList<String>();
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item,list1);
Spinner1.setAdapter(adapter1);
//for second spinner...
final List<String> list2 = new ArrayList<String>();
Spinner1.setOnItemSelectedListener(new OnItemSelectedListener(){
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(//some condition using id)
{
list2.add(stateCursor.getString(1));
}
}
#Override
public void onNothingSelected(AdapterView<?> parent)
{
}
});
//everything is working data is loading, but not display once item is selected on 2nd spinner
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item,list2);
stateSpinner.setAdapter(adapter2);
You need to add the element directly to the adapter (not to list2), the ArrayAdapter keeps its own internal data. Try this code on your onItemSelected:
adapter2.add(stateCursor.getString(1));
adapter2.notifyDataSetChanged();
Call adapter2.notifyDataSetChanged();
such as
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos, long id)
{
if(//some condition using id)
{
list2.add(stateCursor.getString(1));
adapter2.notifyDataSetChanged();
}
}
see http://developer.android.com/reference/android/widget/ArrayAdapter.html for more information.
public void notifyDataSetChanged () Since: API Level 1
Notifies the attached observers that the underlying data has been
changed and any View reflecting the data set should refresh itself.

Categories

Resources