ArrayIndexOutOfBounds listview中的异常

时间:2015-09-22 05:30:49

标签: android listview

我有以下arrayadapter:

public class SmsArrayAdapter extends ArrayAdapter<String> {

    List<String> smsBody;
    List<Boolean> Status;
    List<String> time;
    List<String> SmsMessageId;

    Context context;
    private static LayoutInflater inflater = null;
    String fromNumber;

    public SmsArrayAdapter(Context context, int resource, List<String> smsBody,
            List<Boolean> Status, List<String> time, List<String> SmsMessageId,
            String fromNumber) {
        super(context, resource, smsBody);
        this.smsBody = smsBody;
        this.Status = Status;
        this.context = context;
        inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        this.fromNumber = fromNumber;
        this.time = time;
        this.SmsMessageId = SmsMessageId;
    }

    public String getStr(int position) {
        return smsBody.get(position);
    }

    public String getId(int position) {
        return SmsMessageId.get(position);
    }

    public void setRead(int position, String smsMessageId) {
        Status.set(position, true);
        ContentValues values = new ContentValues();
        values.put("read", true);
        context.getContentResolver().update(Uri.parse("content://sms/inbox"),
                values, "_id=" + smsMessageId, null);
    }

    @Override
    public int getCount() {
        // TODO Auto-generated method stub
        return super.getCount();
    }

    @Override
    public String getItem(int position) {
        // TODO Auto-generated method stub
        return smsBody.get(position);
    }

    public static class ViewHolder {
        public TextView textfrom;
        public TextView text_sms;
        public TextView text_time;
    }

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

        ViewHolder holder;
        if (convertView == null) {

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

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

            holder = new ViewHolder();

            holder.textfrom = (TextView) convertView
                    .findViewById(R.id.textView_from);
            holder.text_sms = (TextView) convertView
                    .findViewById(R.id.textView_sms);
            holder.text_time = (TextView) convertView
                    .findViewById(R.id.textView_time);

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

        holder.textfrom.setText(" " + fromNumber);

        String smsTextToDisplay = smsBody.get(position);
        if (smsTextToDisplay.length() > 100)
            smsTextToDisplay = smsTextToDisplay.substring(0, 99) + " ...";

        holder.text_sms.setText(smsTextToDisplay);

        holder.text_time.setText(time.get(position));
        if (Status.get(position) == false) {
            convertView.setBackgroundColor(context.getResources().getColor(
                    R.color.light_blue_overlay));
        }

        return convertView;
    }

}

但是当我使用这个listview时,我得到以下异常。

09-22 11:27:31.161: E/AndroidRuntime(12067): java.lang.IndexOutOfBoundsException: Invalid index 13, size is 13
09-22 11:27:31.161: E/AndroidRuntime(12067):    at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at java.util.ArrayList.get(ArrayList.java:308)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at com.commlink.smscheck.SmsArrayAdapter.getView(SmsArrayAdapter.java:107)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.widget.AbsListView.obtainView(AbsListView.java:2300)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.widget.ListView.makeAndAddView(ListView.java:1816)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.widget.ListView.fillDown(ListView.java:698)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.widget.ListView.fillGap(ListView.java:662)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.widget.AbsListView.trackMotionScroll(AbsListView.java:5529)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.widget.AbsListView.scrollIfNeeded(AbsListView.java:3366)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.widget.AbsListView.onTouchMove(AbsListView.java:3802)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.widget.AbsListView.onTouchEvent(AbsListView.java:3597)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.view.View.dispatchTouchEvent(View.java:7790)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2316)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2013)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2322)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2027)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2322)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2027)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2322)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2027)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.view.ViewGroup.dispatchTransformedTouchEvent(ViewGroup.java:2322)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:2027)
09-22 11:27:31.161: E/AndroidRuntime(12067):    at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:2145)

为什么我收到此错误?我怎样才能避免这个错误?

2 个答案:

答案 0 :(得分:3)

不良做法

List<String> smsBody;
List<Boolean> Status;
List<String> time;
List<String> SmsMessageId;

管理不同的ArrayLists以管理同一对象的数据是最佳做法。在进行添加或删除数据等操作时,您很难管理不同的ArrayLists。

比如说,如果您想要添加数据,则必须在每个列表中添加相同索引的数据。

管理数据ArrayList的最佳实践

最佳实践是管理具有用户定义对象的单个ArrayList,例如, ArrayList<SMSModel>

其中SMSModel是具有与SMS对象相关的所有属性的模型类,如SMSBody,时间,SMS消息等,并且此模型类将包含getter / setter方法以从对象获取和设置项目。 / p>

答案 1 :(得分:0)

我相信你在适配器中拥有的任何项目,如smsBody,status,smsMessageId,time,请仔细阅读这些项目的内容。我认为所有这些内容都没有相同的内容。检查你的内容时间列表。我认为该列表的内容与其他列表的内容不同。