我正在尝试在操作栏中实现微调器。我在布局中使用XML添加了微调器,然后尝试在基本活动类(activity_main_layout.xml)中设置其适配器。
以下是主要活动:
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);
List<String> list = new ArrayList<String>();
list.add("list 1");
list.add("list 2");
list.add("list 3");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item, list);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_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) {
fillHymVerses("english","7");
} else if (id == R.id.nav_tsonga) {
fillHymVerses("tsonga","7");
} else if (id == R.id.nav_sotho) {
fillHymVerses("sotho","7");
} else if (id == R.id.nav_venda) {
fillHymVerses("venda","7");
} else if (id == R.id.nav_zulu) {
fillHymVerses("zulu","7");
} else if (id == R.id.nav_pedi) {
fillHymVerses("pedi","7");
} else if (id == R.id.nav_share) {
fillHymVerses("share","7");
} else if (id == R.id.nav_send) {
fillHymVerses("send","7");
}
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>
答案 0 :(得分:0)
试试这个:
View view = getLayoutInflater().inflate(R.layout.spinnerlayout, null);
Spinner spinner2 = (Spinner) view.findViewById(R.id.song_number_spinner);
spinner2.setAdapter(dataAdapter);
我看到的问题是,spinnerlayout.xml
布局中未包含activity_main
,因此当您在主要活动中执行findViewById()
时,它会搜索微调器 activity_main
布局。
因此,要真正获得Spinner
,您需要先膨胀拥有微调器的layout
,然后继续从布局中检索微调器。