I have this strange behavior, I have class that extend Dialog, and in onCreate() Method I load data and set it to my spinner and then call spinner.setSelection , the spinner is always select the first item.
I have tried
spinner.setSelection(position);
spinner.setSelection(position, true);
spinner.setSelection(position, false);
spinner.post (Runnable)
new Handler().postDelay (Runnable)
so please can any one tell me what is the problem ??
EDIT
public class MyDialog extends Dialog{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
int layourResId = mContext.getResources().getIdentifier("dialog_layout_default", "layout", mContext.getPackageName());
setContentView(layourResId);
initializeUIComponent();
initializeUIComponentData();
initializeUIComponentAction();
}
private void initializeUIComponent () {
mCountriesSpinner = (Spinner) findViewById(countriesResId);
mOperatorsSpinner = (Spinner) findViewById(operatorsResId);
}
private void initializeUIComponentData () {
mCountriesSpinner.setAdapter(getCountriesAdapter());
mOperatorsSpinner.setAdapter(getOperatorsAdapter(getCountriesAdapter().getItem(mCurrentSelectedCountry)));
mMCC = PhoneUtils.getDeviceMCC(mContext);
mMNC = PhoneUtils.getDeviceMNC(mContext);
if (mMCC != null && mMNC != null) {
String plmn = mMCC + mMNC;
plmn = "41503";
getDefaultSelection(plmn);
}
}
private ArrayList<String> getCountries() {
// logic here
return countriesArray;
}
private ArrayList<String> getOperators(String countryName) {
// logic here
return operatorsArray;
}
private ArrayAdapter<String> getCountriesAdapter() {
// logic here
return countriesArrayAdapter;
}
private ArrayAdapter<String> getOperatorsAdapter(String countryName) {
// logic here
return operatorsArrayAdapter;
}
private void getDefaultSelection(String plmn) {
for (int i = 0; i < array.size(); i++) {
if (array.get(i).getPLMN().equals(plmn)) {
object defaultSelection = array.get(i);
if (defaultSelection != null) {
mCountriesSpinner.setSelection(getCountriesAdapter().getPosition(defaultSelection.getCountryName()));
ArrayAdapter<String> adapter = getOperatorsAdapter(defaultSelection.getCountryName());
mOperatorsSpinner.setAdapter(adapter);
adapter.notifyDataSetChanged();
int position = adapter.getPosition(defaultChannel.getOperatorName());
Toast.makeText(mContext, "position to be selected in operator " + position, Toast.LENGTH_SHORT).show();
mOperatorsSpinner.setSelection(position, true);
}
}
}
}
}
EDIT2
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/default_container_bg"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@string/please_choose_country"
android:textColor="@android:color/black" />
<Spinner
android:id="@+id/countries_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@string/please_choose_operator"
android:textColor="@android:color/black" />
<Spinner
android:id="@+id/operators_spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp" />
<Button
android:id="@+id/payment_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:text="@string/payment" />
</LinearLayout>
</RelativeLayout>