我想在导航视图中使用Array for Menu项。我在左侧创建了我的菜单。但是,当我点击项目时,我想显示项目的名称。但这是一个错误。我尝试使用另一个变量。 (例如我确定一个变量而不是“i”喜欢(int counter)“但是它给出了一个像以前一样的错误。”
感谢您的帮助。谢谢。
<div class="carousel slide" id="spinner" data-ride="carousel">
<ol class="carousel-indicators">
<li class="active" data-target="#spinner" data-slide-to="0"></li>
<li data-target="#spinner" data-slide-to="1"></li>
<li data-target="#spinner" data-slide-to="2"></li>
</ol>
<div class="carousel-inner" role="listbox">
<div class="item active"><img src="http://placehold.it/600x300"></div>
<div class="item"><img src="http://placehold.it/600x300"></div>
<div class="item"><img src="http://placehold.it/600x300"></div>
</div>
<a class="left carousel-control" href="#spinner" role="button" data-slide="prev">
<span class="icon-prev" aria-hidden="true">
</span><span class="sr-only">Previous</span>
</a>
<a class="right carousel-control" href="#spinner" role="button" data-slide="next">
<span class="icon-next" aria-hidden="true">
<span class="sr-only">Next</span></span></a>
</div>
这里的数组代码
/AndroidRuntime: FATAL EXCEPTION: main
Process: com.technotalkative.navigationviewdemo, PID: 28374
java.lang.ArrayIndexOutOfBoundsException: length=4; index=4
at com.technotalkative.navigationviewdemo.MainActivity$3.onMenuItemClick(MainActivity.java:290)
at android.support.v7.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:148)
at android.support.v7.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:957)
at android.support.design.internal.NavigationMenuPresenter$1.onClick(NavigationMenuPresenter.java:318)
at android.view.View.performClick(View.java:5198)
at android.view.View$PerformClick.run(View.java:21147)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
private int counter=0;
private void addItemsRunTime(NavigationView navigationView) {
//adding items run time
final Menu menu = navigationView.getMenu();
for (int i = 0; i <= 3; i++) {
counter=i;
menu.add(0,i,0,i + bilgiler[i].toString());
menu.findItem(i).setIcon(R.drawable.coins);
menu.findItem(i).setCheckable(true); //Bu kod seçili kısmı tek bir seçenekli hale getiriyor.
menu.findItem(i).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
@Override
public boolean onMenuItemClick(MenuItem item) {
Toast.makeText(MainActivity.this,"Selam Tıkladığınız Butonun Adı" + bilgiler[counter].toString(),Toast.LENGTH_SHORT).show();
counter++;
return true;
}
});
}
final SubMenu subMenu = menu.addSubMenu("SubMenu Title");
for (int i = 0; i < sayacim; i++) {
subMenu.add(0,i,0,bilgiler[i].toString());
subMenu.findItem(i).setIcon(R.drawable.resmim2);
}
for (int i = 0, count = mNavigationView.getChildCount(); i < count; i++) {
final View child = mNavigationView.getChildAt(i);
if (child != null && child instanceof ListView) {
final ListView menuView = (ListView) child;
final HeaderViewListAdapter adapter = (HeaderViewListAdapter) menuView.getAdapter();
final BaseAdapter wrapped = (BaseAdapter) adapter.getWrappedAdapter();
wrapped.notifyDataSetChanged();
}
}
}
答案 0 :(得分:0)
问题是您正在尝试从内部类访问变量。解决方案:在该方法之前声明您的变量 -
public Integer i;
private void addItemsRunTime(...){
...
for (i = 0; ....){
}
}
编辑:
在你的toast中而不是biligiler [i] .toString()写item.getTitle()。toString()或读取关于MenuItem:developer.android.com/reference/android/view/MenuItem.html
答案 1 :(得分:0)
试试这样:
private int counter = 0;
for (int i = 0; i < x; i++) {
//in interface
....bilgiger[counter]
//at the end
counter++;
}