我有这种奇怪的行为,我有扩展Dialog的类,并且在onCreate()方法中我加载数据并将其设置为我的微调器,然后调用spinner.setSelection,微调器总是选择第一项。
我试过了
spinner.setSelection(position);
spinner.setSelection(position, true);
spinner.setSelection(position, false);
spinner.post (Runnable)
new Handler().postDelay (Runnable)
所以请任何人告诉我这是什么问题吗?
修改
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>