OnItemSelectedListener/OnItemSelected for Spinner not working as desired
I have used a FragmentPagerAdapter to create 4 sections/tabs. The first 2
tabs have various EditText fields with TextChangeListener attached to each
one to monitor and store their contents in different variables. The 3rd
tabs has a single Spinner item declared in XML. In Java what i'm trying to
do is attach a OnItemSelectedListener to it and store the currently
selected value of the spinner as soon as its changed from within the
OnItemSelected() function in a variable. Unfortunately it does not work.
In the 4th tab I take the values from these variables and set them to
various TextViews. The values from the EditViews update dynamically but
not the value from the spinner. AFTER selecting a Spinner item if i edit
any of the EditViews(in the first 2 tabs) only then does the
OnItemSelected() method kick in and the variable and TextView associated
with the Spinner value updates! This is true no matter how many times i
change the selected item of the spinner!
I have attached the code I am using below.
static String name=" ";
static String bldGrp=" ";
static String cell=" ";
static String email=" ";
static String address=" ";
static String country=" ";
static String state=" ";
static String city=" ";
public static class SectionFragment0 extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number_0";
public SectionFragment0() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.reg_name, container, false);
((EditText)view.findViewById(R.id.fn)).addTextChangedListener(new
TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int
i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i,
int i2, int i3) {
}
@Override
public void afterTextChanged(Editable e) {
if (e.toString().length() > 0)
name=remSpace(e.toString())+name.substring(name.lastIndexOf("
"));
}
});
((EditText)view.findViewById(R.id.ln)).addTextChangedListener(new
TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int
i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i,
int i2, int i3) {
}
@Override
public void afterTextChanged(Editable e) {
if (e.toString().length() > 0)
name=name.substring(0,(name.lastIndexOf("
")+1))+remSpace(e.toString());
}
});
return view;
}
}
public static class SectionFragment1 extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number_1";
public SectionFragment1() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.reg_contact, container,
false);
((EditText)view.findViewById(R.id.cell)).addTextChangedListener(new
TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int
i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i,
int i2, int i3) {
}
@Override
public void afterTextChanged(Editable e) {
cell = remSpace(e.toString());
}
});
((EditText)view.findViewById(R.id.email)).addTextChangedListener(new
TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int
i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i,
int i2, int i3) {
}
@Override
public void afterTextChanged(Editable e) {
if (e.toString().length() > 0)
email=remSpace(e.toString());
}
});
((EditText)view.findViewById(R.id.address)).addTextChangedListener(new
TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int
i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i,
int i2, int i3) {
}
@Override
public void afterTextChanged(Editable e) {
if (e.toString().length() > 0)
address=remSpace(e.toString());
}
});
((EditText)view.findViewById(R.id.country)).addTextChangedListener(new
TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int
i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i,
int i2, int i3) {
}
@Override
public void afterTextChanged(Editable e) {
if (e.toString().length() > 0)
country=remSpace(e.toString());
}
});
((EditText)view.findViewById(R.id.state)).addTextChangedListener(new
TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int
i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i,
int i2, int i3) {
}
@Override
public void afterTextChanged(Editable e) {
if (e.toString().length() > 0)
state=remSpace(e.toString());
}
});
((EditText)view.findViewById(R.id.city)).addTextChangedListener(new
TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int
i, int i2, int i3) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i,
int i2, int i3) {
}
@Override
public void afterTextChanged(Editable e) {
if (e.toString().length() > 0)
city=remSpace(e.toString());
}
});
return view;
}
}
public static class SectionFragment2 extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number_2";
public SectionFragment2() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.reg_bloodgrp, container,
false);
((Spinner)view.findViewById(R.id.bloodGrp)).setOnItemSelectedListener(new
AdapterView.OnItemSelectedListener() {
@Override
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
bldGrp = (parent.getItemAtPosition(pos)).toString();
}
@Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
return view;
}
}
public class SectionFragment3 extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number_3";
public SectionFragment3() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.reg_summary, container,
false);
((TextView)view.findViewById(R.id.show_name)).setText("Name -
"+name);
((TextView)view.findViewById(R.id.show_cell)).setText("Mobile -
"+cell);
((TextView)view.findViewById(R.id.show_email)).setText("E-Mail ID
- "+email);
((TextView)view.findViewById(R.id.show_address)).setText("Address
- "+address);
((TextView)view.findViewById(R.id.show_country)).setText("Country
- "+country);
((TextView)view.findViewById(R.id.show_state)).setText("State -
"+state);
((TextView)view.findViewById(R.id.show_city)).setText("City -
"+city);
((TextView)view.findViewById(R.id.show_bldGrp)).setText("Blood
Group - "+bldGrp);
return view;
}
Please Help! Thank You in advance! :)
No comments:
Post a Comment