0

My string array has around 40 items, so how I can set the length of string[]? My code:

Context context;
String[] mTitle;
String[] mSubTitle;
LayoutInflater inflater;

public MenuListAdapter(Context context, String[] title, String[] subtitle)
        {
    this.context = context;
    this.mTitle = title;
    this.mSubTitle = subtitle;
}

@Override
public int getCount() {
    return mTitle.length;
}

@Override
public Object getItem(int position) {
    return mTitle[position];
}

@Override
public long getItemId(int position) {
    return position;
}

@SuppressLint("ViewHolder") public View getView(int position, View convertView, ViewGroup parent) {
    // Declare Variables
    TextView txtTitle;
    TextView txtSubTitle;

    inflater = (LayoutInflater) context
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    View itemView = inflater.inflate(R.layout.drawer_list_item, parent,
            false);

    // Locate the TextViews in drawer_list_item.xml
    txtTitle = (TextView) itemView.findViewById(R.id.title);
    txtSubTitle = (TextView) itemView.findViewById(R.id.subtitle);

    // Set the results into TextViews
    String[] title = context.getResources().getStringArray(R.array.title);
    String[] subtitle = context.getResources().getStringArray(R.array.subtitle);
    txtTitle.setText(title[position]);
    txtSubTitle.setText(subtitle[position]);

    return itemView;
}

}

And my string array:

<string-array name="title">
<item>10</item>
<item>11</item>
<item>11A</item>
<item>11G</item>
<item>12</item>
<item>12A</item>
<item>12Y</item>
<item>13</item>
<item>13A</item>
<item>13G</item>
<item>13Y</item>
<item>14</item>
<item>14G</item>
<item>15</item>
<item>15A</item>
<item>15Y</item>
<item>16</item>
<item>16A</item>
<item>17</item>
<item>20</item>
<item>22</item>
<item>23</item>
<item>23E</item>
<item>24</item>
<item>25</item>
<item>26</item>
<item>26A</item>
<item>26G</item>
<item>27</item>
<item>29</item>
<item>31</item>
<item>31E</item>
<item>32</item>
<item>33</item>
<item>34</item>
<item>35</item>
<item>36</item>
<item>37</item>
<item>38</item>
<item>42</item>
<item>43</item>
<item>44</item>
<item>M26</item>
</string-array>

Where should I declare the length of array? The exact error is:

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

I do not want to use string list, as I don't know how. Please help me.

Thanks in advance.

3
  • you forgot the closing tag of String-array </string-array> after the last <item>M26</item> Commented Feb 25, 2015 at 17:07
  • I don't understand why you need this String[] title = context.getResources().getStringArray(R.array.title); String[] subtitle = context.getResources().getStringArray(R.array.subtitle);? when you have this.mTitle = title; this.mSubTitle = subtitle; and using a model class will be better Commented Feb 25, 2015 at 17:13
  • Like @Raghunandan said, you shouldn't be fetching your array again in getView(). You should be using mTitle and mSubtitle. Check the length of both these arrays. The error means that one of these arrays (probably subtitles) only contains 4 items. Commented Feb 25, 2015 at 17:22

1 Answer 1

1

Your arrays title and subtitle are not the same length. Check your data out.

You can't set an array length, but you can get it from a property:

int number_of_items = title.length;
Sign up to request clarification or add additional context in comments.

1 Comment

Well, actually it was a pretty stupid mistake, I just had to make the two arrays the same length. Thanks.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.