2014-02-09 2 views
1

SectionIndexer으로 구현했습니다. 그것은 시간의 99 %를 작동하지만 오류가 발생하는 한 가지 상황을 발견했습니다.LIstView SectionIndexer에서 오류가 발생했습니다. ArrayIndexOutOfBoundsException

@Override 
    public int getPositionForSection(int section) { 
     return indexer.get(sections[section]); 
    } 

디버깅 나는 위의 방법, 입력을 위해 나는이 말에 제공하는 것으로 나타났습니다 :이 코드에서 무슨 일이 일어나고

java.lang.ArrayIndexOutOfBoundsException: length=1; index=1 

그리고있다 :

오류는 텍스트가 두 번 호출됩니다. 두 번째로 section 값이 1으로 가정하면 오류가 발생합니다.

내 어댑터는이 방법으로 구현됩니다

public class SearchAdapter extends ArrayAdapter<String[]> implements SectionIndexer { 
    private final Context context; 
    private ArrayList<String[]> foodData; 
    private HashMap<String, Integer> indexer; 
    private String[] sections; 

    public SearchAdapter(Context context, ArrayList<String[]> allFoodData) { 
     super(context, R.layout.rowlist, allFoodData); 
     this.context = context; 
     this.foodData = allFoodData; 


     indexer = new HashMap<String, Integer>(); 
     int size = foodData.size(); 
     for (int x = 0; x < size; x++) { 
      String[] s = foodData.get(x); 
      System.out.println("x = " + x + ": foodata[0]=" + s[0] + ": foodata[1]=" + s[1] + ": foodata[2]=" + s[2] + ": foodata[3]=" + s[3]); 

      String ch = s[1].substring(0,1); 
      ch = ch.toUpperCase(); 
      if (!indexer.containsKey(ch)) 
       indexer.put(ch, x); 
     } 

     Set<String> sectionLetters = indexer.keySet(); 
     // create a list from the set to sort 
     ArrayList<String> sectionList = new ArrayList<String>(sectionLetters); 
     Collections.sort(sectionList); 
     sections = new String[sectionList.size()]; 
     sections = sectionList.toArray(sections); 
    } 

    @Override 
    public View getView(int position, View convertView, ViewGroup parent) { 
    \\\\\\\ stuff 
    } 

    // ##################### LISTVIEW INDEX ##################### 
    @Override 
    public Object[] getSections() { 
     return sections; 
    } 

    @Override 
    public int getPositionForSection(int section) { 
     return indexer.get(sections[section]); 
    } 

    @Override 
    public int getSectionForPosition(int position) { 
     return 0; 
    } 
} 

입력은 다음과 같습니다

x = 0: foodata[0]=81: foodata[1]=Iogurte Açucarado batido gordo com cereais e fruta: foodata[2]=PT INSA: foodata[3]=1 
x = 1: foodata[0]=80: foodata[1]=Iogurte Açucarado batido gordo com fruta: foodata[2]=PT INSA: foodata[3]=1 
x = 2: foodata[0]=70: foodata[1]=Iogurte Açucarado batido meio gordo: foodata[2]=PT INSA: foodata[3]=1 
x = 3: foodata[0]=75: foodata[1]=Iogurte Açucarado batido meio gordo com fruta: foodata[2]=PT INSA: foodata[3]=1 
x = 4: foodata[0]=69: foodata[1]=Iogurte Açucarado com edulcorante de síntese, batido magro com cereais: foodata[2]=PT INSA: foodata[3]=1 
x = 5: foodata[0]=68: foodata[1]=Iogurte Açucarado com edulcorante de síntese, sólido magro: foodata[2]=PT INSA: foodata[3]=1 
x = 6: foodata[0]=71: foodata[1]=Iogurte Açucarado líquido meio gordo: foodata[2]=PT INSA: foodata[3]=1 
x = 7: foodata[0]=82: foodata[1]=Iogurte Aromatizado açucarado batido gordo: foodata[2]=PT INSA: foodata[3]=1 
x = 8: foodata[0]=72: foodata[1]=Iogurte Aromatizado açucarado batido meio gordo: foodata[2]=PT INSA: foodata[3]=1 
x = 9: foodata[0]=77: foodata[1]=Iogurte Aromatizado açucarado líquido magro: foodata[2]=PT INSA: foodata[3]=1 
x = 10: foodata[0]=73: foodata[1]=Iogurte Aromatizado açucarado líquido meio gordo: foodata[2]=PT INSA: foodata[3]=1 
x = 11: foodata[0]=78: foodata[1]=Iogurte Aromatizado açucarado sólido magro: foodata[2]=PT INSA: foodata[3]=1 
x = 12: foodata[0]=74: foodata[1]=Iogurte Aromatizado açucarado sólido meio gordo: foodata[2]=PT INSA: foodata[3]=1 
x = 13: foodata[0]=79: foodata[1]=Iogurte Natural sólido magro: foodata[2]=PT INSA: foodata[3]=1 
x = 14: foodata[0]=76: foodata[1]=Iogurte Natural sólido meio gordo: foodata[2]=PT INSA: foodata[3]=1 

당신이 볼 수 있듯이, 인덱스,이 경우 "I"에, 단 하나 개의 문자이다. 나는 하나의 문자 색인만을 생성하는 다른 입력을 시도해 왔으며 이것이 오류를주는 유일한 상황이다.

왜 그런가?

+0

이 같은 문제가 발생, 아무도 이것에 대답을했다 ??? – astuter

답변

3

이 문제를 해결할 수 :

public int getPositionForSection(int section) { 

     if (section > sections.length - 1) { 
      return 0; 
     } else { 

      return alphaIndexer.get(sections[section]); 
     } 
    } 
+0

고마워요. 그러나 다음과 같이 해결했습니다 :'if (section> sections.length - 1) { returns sections.length - 1; } else { return indexer.get (섹션 [섹션]); }' – Favolas

+0

니스, 아직도 논리는 .. .. :) – astuter

관련 문제