使用>;创建列表视图后的java.lang.ArrayIndexOutOfBoundsException;100项
创建包含100多个项目(确切地说是111个)的列表视图并部署应用程序后,它会按预期运行,但当我滚动列表时,应用程序崩溃,并返回使用>;创建列表视图后的java.lang.ArrayIndexOutOfBoundsException;100项,java,android,arrays,indexoutofboundsexception,Java,Android,Arrays,Indexoutofboundsexception,创建包含100多个项目(确切地说是111个)的列表视图并部署应用程序后,它会按预期运行,但当我滚动列表时,应用程序崩溃,并返回ArrayIndexOutOfBoundsException。我真的不明白为什么长度和索引返回'19'。有人知道为什么会显示19吗?需要做些什么来解决这个问题 java.lang.ArrayIndexOutOfBoundsException:长度=19;指数=19 在com.helloapps.helloworldapp.adapters.OrangeListAdapte
ArrayIndexOutOfBoundsException
。我真的不明白为什么长度和索引返回'19'。有人知道为什么会显示19吗?需要做些什么来解决这个问题
java.lang.ArrayIndexOutOfBoundsException:长度=19;指数=19
在com.helloapps.helloworldapp.adapters.OrangeListAdapter.getPositionForSection(OrangeListAdapter.java:160)上
XML
<ListView
android:id="@android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:fastScrollEnabled="true"
android:scrollbarStyle="outsideInset"/>
Java
public class OrangeListAdapter extends BaseAdapter implements Filterable, SectionIndexer {
private List<Orange> mData;
private List<Orange> mFilteredData;
private LayoutInflater mInflater;
private ItemFilter mFilter;
private Object[] mSections;
private int[] mSectionsIndexedByPosition;
private int[] mPositionsIndexedBySection;
public OrangeListAdapter (List<Orange> data, Context context) {
mData = data;
mFilteredData = new ArrayList(mData);
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
setupSections();
}
@Override
public int getCount() {
return mFilteredData.size();
}
@Override
public Orange getItem(int position) {
return mFilteredData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = mInflater.inflate(R.layout.list_item_dualline, parent, false);
holder = new ViewHolder();
holder.title = (TextView) convertView.findViewById(R.id.item_name);
holder.description = (TextView) convertView.findViewById(R.id.item_description);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
Orange orange = getItem(position);
holder.title.setText(orange.getName());
holder.description.setText(orange.getDescrption());
if (orange.isSelected()) {
convertView.setBackgroundColor(Color.parseColor("#FF6600"));
holder.title.setTextColor(Color.parseColor("#FFFFFF"));
holder.description.setTextColor(Color.parseColor("#FFFFFF"));
} else {
convertView.setBackgroundColor(Color.TRANSPARENT);
holder.title.setTextColor(Color.parseColor("#FFFFFF"));
holder.description.setTextColor(Color.parseColor("#B5B5B5"));
}
holder.title.setText(mFilteredData.get(position).getStation());
holder.description.setText(mFilteredData.get(position).getZone());
return convertView;
}
@Override
public Filter getFilter() {
if (mFilter == null) {
mFilter = new ItemFilter();
}
return mFilter;
}
/**
* View holder
*/
static class ViewHolder {
private TextView title;
private TextView description;
}
private class ItemFilter extends Filter {
@Override
protected FilterResults performFiltering(CharSequence constraint) {
FilterResults results = new FilterResults();
if (TextUtils.isEmpty(constraint)) {
results.count = mData.size();
results.values = new ArrayList(mData);
} else {
//Create a new list to filter on
List<Orange> resultList = new ArrayList<Orange>();
for (Orange str : mData) {
if (str.getStation().toLowerCase().contains(constraint.toString().toLowerCase())) {
resultList.add(str);
}
}
results.count = resultList.size();
results.values = resultList;
}
return results;
}
/**
* Runs on ui thread
* @param constraint the constraint used for the result
* @param results the results to display
*/
@SuppressWarnings("unchecked")
@Override
protected void publishResults(CharSequence constraint, FilterResults results) {
if (results.count == 0) {
mFilteredData.clear();
notifyDataSetInvalidated();
} else {
mFilteredData = (ArrayList<Orange>)results.values;
notifyDataSetChanged();
}
setupSections();
}
}
@Override
public int getPositionForSection(int section) {
return mPositionsIndexedBySection[section];
}
@Override
public int getSectionForPosition(int position) {
return mSectionsIndexedByPosition[position];
}
@Override
public Object[] getSections() {
return mSections;
}
private void setupSections() {
String initial = "\0";
List<String> sections = new ArrayList<String>();
mSectionsIndexedByPosition = new int[mFilteredData.size()];
mPositionsIndexedBySection = new int[mFilteredData.size()];
int section = 0;
for (int pos = 0; pos < mFilteredData.size(); pos++) {
Orange orange = mFilteredData.get(pos);
if (initial.charAt(0) != orange.getName().charAt(0)) {
initial = orange.getName().substring(0, 1);
section = sections.size();
sections.add(initial);
mPositionsIndexedBySection[section] = pos;
mSectionsIndexedByPosition[pos] = section;
} else {
mSectionsIndexedByPosition[pos] = section;
}
}
mSections = sections.toArray();
mPositionsIndexedBySection = Arrays.copyOf(mPositionsIndexedBySection, mSections.length);
}
}
公共类OrangeListAdapter扩展BaseAdapter实现可过滤的节索引器{
私有列表数据;
私人名单;
私人停车场;
私有项目过滤器;
私有对象[]个操作;
私有int[]mSectionsIndexedByPosition;
私有int[]mpositionIndexedBySection;
公共OrangeListAdapter(列表数据、上下文){
mData=数据;
mFilteredData=新阵列列表(mData);
mInflater=(LayoutInflater)context.getSystemService(context.LAYOUT\u充气机\u服务);
设置节();
}
@凌驾
public int getCount(){
返回mFilteredData.size();
}
@凌驾
公共橙色getItem(内部位置){
返回mFilteredData.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图){
视窗座;
if(convertView==null){
convertView=mInflater.充气(R.layout.list\u item\u dualline,父项,false);
holder=新的ViewHolder();
holder.title=(TextView)convertView.findViewById(R.id.item_name);
holder.description=(TextView)convertView.findViewById(R.id.item_description);
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
橙色=获取项目(位置);
holder.title.setText(orange.getName());
holder.description.setText(orange.getDescrption());
if(orange.isSelected()){
convertView.setBackgroundColor(Color.parseColor(“#FF6600”));
holder.title.setTextColor(Color.parseColor(“#FFFFFF”));
holder.description.setTextColor(Color.parseColor(“#FFFFFF”));
}否则{
convertView.setBackgroundColor(Color.TRANSPARENT);
holder.title.setTextColor(Color.parseColor(“#FFFFFF”));
holder.description.setTextColor(Color.parseColor(“#b5”));
}
holder.title.setText(mfilteredata.get(position.getStation());
holder.description.setText(mFilteredData.get(position.getZone());
返回视图;
}
@凌驾
公共过滤器getFilter(){
if(mFilter==null){
mFilter=newitemfilter();
}
返回过滤器;
}
/**
*视图持有者
*/
静态类视窗夹{
私有文本视图标题;
私有文本视图描述;
}
私有类ItemFilter扩展了筛选器{
@凌驾
受保护的筛选器结果性能筛选(CharSequence约束){
FilterResults results=新的FilterResults();
if(TextUtils.isEmpty(约束)){
results.count=mData.size();
results.values=新的ArrayList(mData);
}否则{
//创建要筛选的新列表
List resultList=new ArrayList();
用于(橙色str:mData){
if(str.getStation().toLowerCase().contains(constraint.toString().toLowerCase())){
结果列表添加(str);
}
}
results.count=resultList.size();
results.values=结果列表;
}
返回结果;
}
/**
*在ui线程上运行
*@param constraint用于结果的约束
*@param results要显示的结果
*/
@抑制警告(“未选中”)
@凌驾
受保护的void publishResults(CharSequence约束、FilterResults结果){
如果(results.count==0){
mFilteredData.clear();
notifyDataSetionValidated();
}否则{
mFilteredData=(ArrayList)results.values;
notifyDataSetChanged();
}
设置节();
}
}
@凌驾
公共int getPositionForSection(int段){
返回MPositionIndexedBySection[节];
}
@凌驾
公共int getSectionForPosition(int位置){
返回mSectionsIndexedByPosition[位置];
}
@凌驾
公共对象[]getSections(){
返回mseconts;
}
私人文件(第()节){
字符串initial=“\0”;
List sections=new ArrayList();
mSectionsIndexedByPosition=newint[mFilteredData.size()];
mpositionIndexedBySection=new int[mfilteredata.size()];
int段=0;
对于(int pos=0;posmPositionsIndexedBySection = Arrays.copyOf(mPositionsIndexedBySection, mSections.length);