这是一个非常基本的java问题,我有一个字符串数组
String[] navTitles = new String[] {Home, HR, company, joiner, next};
这些标题显示在ListView中。单击第一个项目时,我必须显示某个列表,否则如果单击任何其他标题,我必须显示不同的列表。第一个(默认)列表应该是单击第一个项目时出现的列表。问题是,如果我点击标题,则会显示与下一个标题相关的列表。当我点击最后一个标题时出现错误: -
05-02 04:47:38.787: E/AndroidRuntime(1490): java.lang.ArrayIndexOutOfBoundsException: length=5; index=5
05-02 04:47:38.787: E/AndroidRuntime(1490): at com.abc.attini.Home.displayView(Home.java:600)
这是我的代码: -
for (int j = 0; j < navMenuTitles.length && j < iconColors.length; j++) {
navDrawerItems.add(new NavDrawerItem(navMenuTitles[j], iconColors[j]));
}
adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
}
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// display view for selected nav drawer item
displayView(position);
}
}
private void displayView(int position) {
Fragment fragment = null;
if (position == 0) {
fragment = new HomeFragment(SPHostUrl, encodedAccountName,
deviceAuthKey, usersname, avatarUrl, fullName,
getApplicationContext(), myFinalNewsList);
} else {
String myPosition = null;
myPosition = navDrawerItems.get(position).getTitle(); //Line 600
companyNewsList = Lists.newArrayList(Collections2.filter(myFinalNewsList,
new ArticleFilter(myPosition)));
fragment = new HomeFragment(SPHostUrl, encodedAccountName, deviceAuthKey,
usersname, avatarUrl, fullName, getApplicationContext(), companyNewsList);
}
答案 0 :(得分:0)
尝试以下代码: -
发生错误,因为索引值达到索引以上。
private void displayView(int position)
{
Fragment fragment = null;
position = position % length; //length of your array
if(position==0)
{
fragment = new HomeFragment(SPHostUrl,encodedAccountName,deviceAuthKey,usersname,avatarUrl, fullName,getApplicationContext(),myFinalNewsList);
}
else
{
String myPosition = null;
myPosition = navDrawerItems.get(position).getTitle(); //Line 600
companyNewsList = Lists.newArrayList(Collections2.filter(myFinalNewsList, new ArticleFilter(myPosition)));
fragment = new HomeFragment(SPHostUrl,encodedAccountName,deviceAuthKey,usersname,avatarUrl, fullName,getApplicationContext(),companyNewsList);
}
}
上面的代码在我的最后工作正常,因为它在数组长度下建立位置。