自定义列表视图与延迟加载导致索引outof bound异常

时间:2015-12-28 11:54:44

标签: android indexoutofboundsexception

   public String getAddress(double lat, double lng) {
    Geocoder geocoder = new Geocoder(MapsActivity.this, Locale.getDefault());
    String Address=" ";
    Log.e("ADDRESS","ADDRESS");
    try {
        List<Address> addresses = geocoder.getFromLocation(lat, lng, 1);
        Address obj = addresses.get(0);
        String add = obj.getAddressLine(0);
        Address= obj.getAddressLine(0);

        add = add + "\n" + obj.getCountryName();
        add = add + "\n" + obj.getCountryCode();
        add = add + "\n" + obj.getAdminArea();
        add = add + "\n" + obj.getPostalCode();
        add = add + "\n" + obj.getSubAdminArea();
        add = add + "\n" + obj.getLocality();
        add = add + "\n" + obj.getSubThoroughfare();

        Log.e("IGA", "Address" + add);


    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
    }
    return Address;
}

}

  

at&gt;&gt;&gt; imageLoader.DisplayImage(data [position],image);    错误是

enter image description here

public class MainActivity extends Activity实现OnScrollListener {

public class LazyImageLoadAdapter extends BaseAdapter implements
        OnClickListener {

private int mCount = 10;

// String mPositionString;

private Activity activity;
private String[] data;
private static LayoutInflater inflater = null;
public ImageLoader imageLoader;

// int mTextViewResourceId;

public LazyImageLoadAdapter(Activity a, String[] d) {
    activity = a;
    data = d;
    inflater = (LayoutInflater) activity
            .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

    // mPositionString = activity.getString(d.length) + " ";

    // mTextViewResourceId = textViewResourceId;

    // Create ImageLoader object to download and show image in list
    // Call ImageLoader constructor to initialize FileCache

    imageLoader = new ImageLoader(activity.getApplicationContext());

}

public void addMoreItems(int count) {
    mCount += count;
    notifyDataSetChanged();
}

public int getCount() {

    return mCount;

}

public Object getItem(int position) {

    return position;
}

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

// @Override
// public int getViewTypeCount() {
// return 2;
// }

/********* Create a holder Class to contain inflated xml file elements *********/
public static class ViewHolder {

    public TextView text;
    public TextView text1;
    public TextView textWide;
    public ImageView image;

}

public View getView(int position, View convertView, ViewGroup parent) {

    View vi = convertView;
    ViewHolder holder;

    if (convertView == null) {

        /****** Inflate tabitem.xml file for each row ( Defined below ) *******/
        vi = inflater.inflate(R.layout.listview_row, null);

        /****** View Holder Object to contain tabitem.xml file elements ******/

        holder = new ViewHolder();
        holder.text = (TextView) vi.findViewById(R.id.text);
        holder.text1 = (TextView) vi.findViewById(R.id.text1);
        holder.image = (ImageView) vi.findViewById(R.id.image);

        /************ Set holder with LayoutInflater ************/
        vi.setTag(holder);
    } else
        holder = (ViewHolder) vi.getTag();

    holder.text.setText("Company " + position);
    holder.text1.setText("company description " + position);
    ImageView image = holder.image;

    // DisplayImage function from ImageLoader Class
    imageLoader.DisplayImage(data[position], image);

    /******** Set Item Click Listner for LayoutInflater for each row ***********/
    vi.setOnClickListener(new OnItemClickListener(position));
    return vi;
}

@Override
public void onClick(View arg0) {
    // TODO Auto-generated method stub

}

/********* Called when Item click in ListView ************/
private class OnItemClickListener implements OnClickListener {
    private int mPosition;

    OnItemClickListener(int position) {
        mPosition = position;
    }

    @Override
    public void onClick(View arg0) {
        MainActivity sct = (MainActivity) activity;
        sct.onItemClick(mPosition);
    }
}

}

这是我的MainActivity类,使用OnScrollListener来显示滚动后显示的数据。

这是例外

请帮帮我..

1 个答案:

答案 0 :(得分:0)

我的猜测是你的data数组不包含与mCount变量告诉适配器相同数量的值。尝试通过以下方式解决此问题:

// change your method implementation that returns mCount to this one
public int getCount() {
    return data.length;
}

这应该可以解决你所获得的ArrayIndexOutOfBoundsException。我想,下一步将是addMoreItems方法的实现,以便它可以向您的数组添加项目,而不仅仅是更新计数器。

修改

如果您手动更新mCount,请考虑以下代码:

public void addMoreItems(int count) {
    // update the counter
    mCount += count;
    // if now your counter exceeds the size of the array,
    // set its value to the size
    if (mCount >= data.length) {
        mCount = data.length;
    }
    notifyDataSetChanged();
}

另外,为了确保您应该检查您是否尝试在与其大小相同的索引处访问您的数组:data[mCount]可能是错误的,如果mCount等于数组的大小,在这种情况下,您应该使用data[mCount - 1]