2017-11-12 5 views
0

こんにちは、私はプログラミングの初心者です。contanct listのようなアンドロイドプロジェクトを作成しようとしています。アダプタクラスのデータを生成しています。私はIndexOutOfBoundsExceptionを取得しています。私はこれがJavaの基本であることを知っていますが、私はなぜこのエラーが出ているのか理解できません。前もって感謝します。ここでアレイアダプタ用のデータの生成

私のコードです:

セクション

public void getListIndexedForPojo(List<Pojo> fruitList) { 

    mMapIndexForPojo = new LinkedHashMap<>(); 

    for (int x = 0; x < fruitList.size(); x++) { 
     String str = fruitList.get(x).getStr(); 
     String ch = str.substring(0, 1); 
     ch = ch.toUpperCase(Locale.US); 

     // HashMap will prevent duplicates 
     mMapIndexForPojo.put(ch, x); 
    } 

    Set<String> sectionLetters = mMapIndexForPojo.keySet(); 

    // create a list from the set to sort 
    ArrayList<String> sectionList = new ArrayList<>(sectionLetters); 

    Collections.sort(sectionList); 

    mSectionsForPojo = new String[sectionList.size()]; 

    sectionList.toArray(mSectionsForPojo); 
} 

//データ生成

用アダプタ

private ArrayList<Pojo> getDataSetForPojo() { 

     List<Pojo> pojoList = genData(); 

     getListIndexedForPojo(pojoList); 

     ArrayList results = new ArrayList<>(); 
     ElementList obj; 
     int section = 0; 
     int normal = 0; 
     String str; 
     String ch; 
     int total = pojoList.size() + mSectionsForPojo.length; 
     for (int index = 0; index < total; index++) { 

      str = pojoList.get(normal).getStr(); //here I am getting error 
      ch = str.substring(0, 1); 

      if (index == 0 || ch.equals(mSectionsForPojo[section])) { 

       obj = new ElementList(ch, true); 
       mMapIndexForPojo.put(ch, index); 
       if (section < mSectionsForPojo.length - 1) { 
        section++; 
       } else { 
        section = 0; 
       } 
      } else { 
       obj = new ElementList(pojoList.get(normal).getStr(), false); 
       normal++; 
      } 

      results.add(index, obj); 
     } 
     return results; 
    } 

//生成リストの一覧を取得する

// How i am setting adapter from activity 

    customAdapter = new CustomAdapter(getActivity(), getDataSetForPojo(), mSectionsForPojo, mMapIndexForPojo); 
     mRecyclerView.setAdapter(customAdapter); 

//

private List<Pojo> genData() { 
     List<Pojo> pojoList = new ArrayList<>(); 

     Pojo pojo; 

     pojo = new Pojo(); 
     pojo.setId(1); 
     pojo.setStr("aback"); 
     pojoList.add(pojo); 

     // adding more objects to list. 

     return pojoList; 
    } 
+1

ThrowされているIndexOutOfBoundsExceptionは、どの行にありますか? –

+0

誰もそのコードをすべて読んで、関連するコードだけをエラーに投稿します。 – m0skit0

+0

私の2番目のコードスニペットに見られるように@ Shn_Android_Dev私は "ここに私はエラーが発生している"と言うコメントを入れました。その行に。 – Thankless

答えて

0

"getDataSetForPojo()"がpojoListのサイズを超えて実行されているという問題があなたのforループであると思います。

private ArrayList<Pojo> getDataSetForPojo() { 
     ... 
     List<Pojo> pojoList = genData(); 
     ... 
     // total can have larger value than pojoList.size() if mSectionsForPojo.length isn't zero. 
     int total = pojoList.size() + mSectionsForPojo.length; 
     for (int index = 0; index < total; index++) { 
      str = pojoList.get(normal).getStr(); //here I am getting error 
     if (index == 0 || ch.equals(mSectionsForPojo[section])) { 

     } else { 
      ... 
      normal++; 
     } 

     } 
     return results; 
    } 

したがって、normalはpojoListサイズを超えて増分できる可能性があります。

関連する問題