简体   繁体   English

微调框不会加载

[英]Spinner won't load

I'm trying to create popup (modal box) in android with a spinner that contains a list of options. 我正在尝试使用包含选项列表的微调器在android中创建弹出窗口(模式框)。 This spinner is within a fragment activity. 该微调器处于片段活动中。 I put the following code in my onCreateDialog method: 我将以下代码放入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;
 }

My XML layout file is as follow: 我的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" />

When I open the application on my android tablet, it just closes the application. 当我在Android平板电脑上打开应用程序时,它只是关闭了该应用程序。 When I created a custom dialog and added the code in the onCreateDialog method, it was fine. 当我创建一个自定义对话框并将代码添加到onCreateDialog方法中时,就很好了。 Then I added the spinner code and it fails. 然后我添加了微调器代码,但失败了。 Can someone tell me what I'm doing wrong and how I could get a spinner to show up. 有人可以告诉我我做错了什么以及如何让微调器出现。

Thanks! 谢谢!

The problem is your not getting spinner reference. 问题是您没有获得微调器参考。 Try below code:- 试试下面的代码:

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;
  }

or 要么

   Spinner spinner = (Spinner) dialogview.findViewById(R.id.planets_spinner);

You must have to get value from reference view. 您必须从参考视图中获取价值。

This is what I tried and it seems to work perfectly for what I need to do. 这是我尝试过的方法,似乎可以完美地满足我的需要。 I'm adding the spinner to a fragment: 我将微调器添加到片段中:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM