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.
String[] title = context.getResources().getStringArray(R.array.title); String[] subtitle = context.getResources().getStringArray(R.array.subtitle);? when you havethis.mTitle = title; this.mSubTitle = subtitle;and using a model class will be bettergetView(). 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.