如何将Spinner添加到Fragment并使用选项填充Spinner

时间:2017-06-11 12:09:43

标签: java android android-fragments android-spinner

在片段中创建微调器并使用存储在string.xml中的数组中的值填充微调器的方法是什么?这是我的代码,但微调器不起作用

MainActivity.java

package com.mycompany.scrolltab;

import android.app.*; import android.os.*; import android.support.v4.app.FragmentActivity; import android.support.v4.view.*; import android.support.v4.app.FragmentManager; import android.support.v4.app.*; import android.support.v4.app.Fragment; import android.view.*; import android.content.*; import android.net.*; import android.widget.*; import android.widget.Spinner.*; import android.widget.SpinnerAdapter.*;

public class MainActivity extends Activity  {

    Spinner mSpinner1,mSpinner2;





    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      ActionBar ab=getActionBar();        ab.hide();      mSpinner1=(Spinner)findViewById(R.id.spinner1);         mSpinner2=(Spinner)findViewById(R.id.spinner2);




        ArrayAdapter<CharSequence>adapter=      ArrayAdapter.createFromResource(this,R.array.chethan,android.R.layout.simple_spinner_item);

        adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);       mSpinner1.setAdapter(adapter);      mSpinner2.setAdapter(adapter);







    }



        public void onButtonClick(View l)

    {       if(l.getId() ==R.id.button)

        {

            String sp1Val = mSpinner1.getSelectedItem().toString();             String sp2Val = mSpinner2.getSelectedItem().toString();

            // Then compare both spinner values:            if(sp1Val.equalsIgnoreCase("kalpane") && sp2Val.equalsIgnoreCase("B.C.Road")){
                Intent i = new Intent(this,mallur.class);
                startActivity(i);           }

        }                   }

}

public class MainActivity extends FragmentActivity {



        ViewPager viewpager=null;   //@Override
       protected void onCreate(Bundle savedInstanceState)    {
        super.onCreate(savedInstanceState);
       setContentView(R.layout.main);




                ActionBar ab=getActionBar();        ab.hide();
                viewpager=(ViewPager)findViewById(R.id.pager);      FragmentManager fragmentmanager=getSupportFragmentManager();        viewpager.setAdapter(new MyAdapter(fragmentmanager));
    }   }








class MyAdapter extends FragmentPagerAdapter

{

        public MyAdapter(FragmentManager fm)        {       super(fm);

                }


            @Override   public android.support.v4.app.Fragment getItem(int i)   {       // TODO: Implement this method      Fragment fragment=null;
                if(i==0)


                        {

                fragment=new FragmentA();
                            }



                        if(i==1)

             {
                 fragment=new FragmentB();
                            }           if(i==2)            {
                fragment=new FragmentC();


                            }       return fragment;    }

    @Override   public int getCount()   {       // TODO: Implement this method      return 3;   }

    @Override   public CharSequence getPageTitle(int position)  {

                if(position==0)
                        {
                return "MAINSCREEN";
                            }
                if(position==1)

        {           return "GOOGLE MAPS";

        }       if(position==2)

        {           return "EXTRAS";

        }
                // TODO: Implement this method      return super.getPageTitle(position);    }


         }

FragmantA.java

package com.mycompany.scrolltab;

import android.support.v4.app.Fragment; import android.os.*; import android.view.*;

public class FragmentA extends Fragment {

    @Override   public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)   {       // TODO: Implement this method      //return super.onCreateView(inflater, container, savedInstanceState);       return inflater.inflate(R.layout.fragment_a,container,false);   }



         }

main.xml中

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
<android.support.v4.view.PagerTitleStrip

    android:id="@+id/title"
    android:layout_width="match_parent"
    android:layout_height="50dp" 
    android:background="#DD1212"

    android:layout_gravity="top"
    android:gravity="center"

    android:titleTextColor="#EEEEEE"
    >
    <TextView
        android:id="@+id/test"
        android:layout_width="match_parent"
        android:layout_height="50dp" 

        >
    </TextView>

</android.support.v4.view.ViewPager>

0 个答案:

没有答案