[英]Spinner won't load
我正在嘗試使用包含選項列表的微調器在android中創建彈出窗口(模式框)。 該微調器處於片段活動中。 我將以下代碼放入onCreateDialog
方法中:
@Override
protected Dialog onCreateDialog(int id) {
AlertDialog dialogDetails = null;
switch (id)
{
case DIALOG_LOGIN:
LayoutInflater inflater = LayoutInflater.from(this);
View dialogview = inflater.inflate(R.layout.dialog_layout, null);
((ItemListFragment) getSupportFragmentManager().findFragmentById(R.id.item_list))
.setActivateOnItemClick(true);
Spinner spinner = (Spinner) findViewById(R.id.planets_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
break;
}
return dialogDetails;
}
我的XML布局文件如下:
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android'
android:layout_width='fill_parent'
android:layout_height='fill_parent'
android:orientation='vertical'
android:padding='10sp' >
<EditText
android:id='@+id/txt_name'
android:layout_width='fill_parent'
android:layout_height='wrap_content'
android:hint='"Username'
android:singleLine='true' >
<requestFocus />
</EditText>
<EditText
android:id='@+id/password'
android:layout_width='match_parent'
android:layout_height='wrap_content'
android:ems='10'
android:inputType='textPassword' >
</EditText>
<RelativeLayout
android:layout_width='match_parent'
android:layout_height='wrap_content' >
<Button
android:id='@+id/btn_login'
android:layout_width='120dp'
android:layout_height='wrap_content'
android:text='"Submit' />
<Button
android:id='@+id/btn_cancel'
android:layout_width='120dp'
android:layout_height='wrap_content'
android:layout_alignParentTop='true'
android:layout_marginLeft='10dp'
android:layout_toRightOf='@+id/btn_login'
android:text='"Cancel' />
</RelativeLayout>
<Spinner
android:id="@+id/planets_spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
當我在Android平板電腦上打開應用程序時,它只是關閉了該應用程序。 當我創建一個自定義對話框並將代碼添加到onCreateDialog
方法中時,就很好了。 然后我添加了微調器代碼,但失敗了。 有人可以告訴我我做錯了什么以及如何讓微調器出現。
謝謝!
問題是您沒有獲得微調器參考。 試試下面的代碼:
case DIALOG_LOGIN:
LayoutInflater inflater = LayoutInflater.from(this);
View dialogview = inflater.inflate(R.layout.dialog_layout, null);
((ItemListFragment) getSupportFragmentManager().findFragmentById(R.id.item_list))
.setActivateOnItemClick(true);
Spinner spinner = (Spinner) dialogview.findViewById(R.id.planets_spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
break;
}
要么
Spinner spinner = (Spinner) dialogview.findViewById(R.id.planets_spinner);
您必須從參考視圖中獲取價值。
這是我嘗試過的方法,似乎可以完美地滿足我的需要。 我將微調器添加到片段中:
protected void onCreateDialog()
{
final Dialog settingdialog = new Dialog(ItemListActivity.this);
settingdialog.setContentView(R.layout.dialog_layout);
settingdialog.setTitle("Settings Menu");
settingdialog.show();
spinner = (Spinner)settingdialog.findViewById(R.id.planets_spinner);
//Populate the dropdown list
String[] state = {"Cupcake", "Donut", "Eclair", "Froyo",
"Gingerbread", "HoneyComb", "IceCream Sandwich", "Jellybean",
"KitKat"};
ArrayAdapter<String>adapter = new ArrayAdapter<String>(ItemListActivity.this,
android.R.layout.simple_spinner_item,state);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
{
/**
* Called when a new item is selected (in the Spinner)
*/
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id)
{
value = spinner.getItemAtPosition(pos).toString();
String s = value;
if (mTwoPane)
{
settingdialog.cancel();
Bundle arguments = new Bundle();
arguments.putInt("num", 1);
arguments.putString("value", s);
ItemListFragment fragment = new ItemListFragment();
fragment.setArguments(arguments);
getSupportFragmentManager().beginTransaction().replace(R.id.item_list, fragment).commit();
}
else
{
Intent detailIntent = new Intent(ItemListActivity.this, ItemListFragment.class);
detailIntent.putExtra("num", 1);
detailIntent.putExtra("value", s);
startActivity(detailIntent);
}
}
public void onNothingSelected(AdapterView<?> parent)
{
}
});
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.