Spinner没有在android中显示项目

时间:2016-04-16 15:38:18

标签: android android-studio

我正在尝试使用fillHymNumber方法在spinner上添加项目,因为它不会更新。日志中没有错误

以下主要活动是这样的:

public class MainLayoutActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {
    private static final String DB_NAME = "yourdbsqlite3.db";
    private SQLiteDatabase database;
    private String TABLE_NAME="ENGLISH",SongTittle, hymn_number_selected = "702";
    private ArrayList HymNumber_Array,HymtitleSearch_Array,songNumber_Array;
    private ScrollView song_scrolview ;
    private TextView song_textview;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_layout);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        //Controls Defination
        song_scrolview = (ScrollView) findViewById(R.id.song_scrollview);
        song_textview = (TextView) findViewById(R.id.song_textview);



        //DB key helper - initialize SQL lite DB
        ExternalDbOpenHelper dbOpenHelper = new ExternalDbOpenHelper(this, DB_NAME);
        database = dbOpenHelper.getReadableDatabase();

        FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
        drawer.setDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
        navigationView.setNavigationItemSelectedListener(this);


    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main_layout, menu);

        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

       private void fillHymNumber(String lang) {
    //Spinner spinner2 = (Spinner) findViewById(R.id.song_number_spinner);
    ArrayAdapter<String> dataAdapter;
    View view = getLayoutInflater().inflate(R.layout.spinnerlayout, null);
    Spinner spinner2 = (Spinner) view.findViewById(R.id.song_number_spinner);

    //Spinner spinner2 = (Spinner) getLayoutInflater().inflate(R.layout.spinnerlayout, null);

    List<String> list = new ArrayList<String>();
    list.add("1");
    list.add("2");
    list.add("3");

    dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,list);
    dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
    spinner2.setAdapter(dataAdapter);
}

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

         if (id == R.id.nav_english) {
        fillHymNumber("english");
    } else if (id == R.id.nav_tsonga) {
        fillHymNumber("tsonga");
    } else if (id == R.id.nav_sotho) {
        fillHymNumber("sotho");
    } else if (id == R.id.nav_venda) {
        fillHymNumber("venda");
    } else if (id == R.id.nav_zulu) {
        fillHymNumber("zulu");
    } else if (id == R.id.nav_pedi) {
        fillHymNumber("pedi");
    } else if (id == R.id.nav_share) {
        fillHymNumber("share");
    } else if (id == R.id.nav_send) {
        fillHymNumber("send");
    }

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

spinnerlayout.xml如下所示:

<?xml version="1.0" encoding="utf-8"?>
<Spinner xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/song_number_spinner"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

</Spinner>

activity_main_layout:

   <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/nav_english"
            android:icon="@drawable/ic_menu_camera"
            android:title="English" />
        <item
            android:id="@+id/nav_tsonga"
            android:icon="@drawable/ic_menu_gallery"
            android:title="Tsonga" />
        <item
            android:id="@+id/nav_sotho"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="Sotho" />
        <item
            android:id="@+id/nav_venda"
            android:icon="@drawable/ic_menu_manage"
            android:title="Venda" />
        <item
            android:id="@+id/nav_zulu"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="Zulu" />
        <item
            android:id="@+id/nav_pedi"
            android:icon="@drawable/ic_menu_manage"
            android:title="Pedi" />
    </group>

    <item android:title="Communicate">
        <menu>
            <item
                android:id="@+id/nav_share"
                android:icon="@drawable/ic_menu_share"
                android:title="Share" />
            <item
                android:id="@+id/nav_send"
                android:icon="@drawable/ic_menu_send"
                android:title="Send" />
        </menu>
    </item>

</menu>

main_layout.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    <item
        android:id="@+id/action_spinner"
        android:orderInCategory="100"
        android:title="728"
        app:showAsAction="always"
        android:actionLayout="@layout/spinnerlayout"/>
    <item
        android:id="@+id/action_search"
        android:orderInCategory="100"
        android:title="Search"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_about"
        android:orderInCategory="100"
        android:title="About"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_settings"
        android:orderInCategory="100"
        android:title="@string/action_settings"
        app:showAsAction="never" />
    <item
        android:id="@+id/action_close"
        android:orderInCategory="100"
        android:title="Close"
        app:showAsAction="never" />
</menu>

1 个答案:

答案 0 :(得分:0)

Terrence,看起来你的代码中没有调用fillHymNumber。