从数组中显示的下一项而不是预期值 [英] Next item displayed from array instead of the expected value

查看:32
本文介绍了从数组中显示的下一项而不是预期值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是一个非常基本的java问题,我有一个字符串数组

This is a very basic java question, I have an array of strings

String[] navTitles = new String[] {Home, HR, company, joiner, next};

这些标题显示在 ListView 中.单击第一项时,我必须显示某个列表,否则如果单击任何其他标题,我必须显示不同的列表.第一个(默认)列表应该是单击第一项时出现的列表.问题是,如果我单击一个标题,则会显示与下一个标题相关的列表.当我点击最后一个标题时,出现错误:-

These titles are shown in a ListView. When the first item is clicked, I have to display a certain list, else if any other title is clicked I have to show a different list. The first (default) list should be the one which appears on clicking the first item. The issue is,if I click on a title, the list related to the next title is displayed. When I click the last title i get an error:-

    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);

    }

推荐答案

试试下面的代码:-

由于索引值超过索引值而发生错误.

Error occur because index value reach more than the index.

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);

    }
}

上面的代码在我的最后工作正常,因为它在数组长度下定位.

above code works fine at my end because it makes position under array length .

这篇关于从数组中显示的下一项而不是预期值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆