listadapter中的列表为空

时间:2015-09-21 06:50:41

标签: android listview arraylist

我在android中有一个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 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.textfrom.setText(" " + fromNumber);
            holder.text_sms = (TextView) convertView
                    .findViewById(R.id.textView_sms);
            String smsTextToDisplay = smsBody.get(position);
            if (smsTextToDisplay.length() > 100)
                smsTextToDisplay = smsTextToDisplay.substring(0, 99) + " ...";

            holder.text_sms.setText(smsTextToDisplay);

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

            }

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

        return convertView;
    }

}

我已按如下方式初始化此arrayadapter:

arrayAdapter = new SmsArrayAdapter(this, R.layout.row_item, smsBody,
                status, time, new ArrayList<String>(), fromNumber);
        smsListView.setAdapter(arrayAdapter);

我想按如下方式访问列表项目:

public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {


try {
        String smsMessageStr = (String) arrayAdapter.getItem(pos);
        String smsMessageId = ((SmsArrayAdapter) arrayAdapter).getId(pos);
        ((SmsArrayAdapter) arrayAdapter).setRead(pos,smsMessageId);
        Intent intent = new Intent(SmsActivity.this,
                ShowIndividualSMS.class);
        intent.putExtra("SMS", smsMessageStr);
        startActivity(intent);

    } catch (Exception e) {
        e.printStackTrace();
        Toast.makeText(this,"exception is "+e.getMessage(), Toast.LENGTH_LONG).show();

    }
}

但是我得到了例外:&#34;索引1无效,大小为0&#34; 。对于此异常,我已了解SmsMessageId列表为空。为什么?我该如何解决这个错误?

3 个答案:

答案 0 :(得分:1)

List<String> SmsMessageId在带有空列表new ArrayList<String>()

的构造函数[adapter]中初始化

答案 1 :(得分:1)

初始化适配器时,将其new ArrayList<String>()作为其SmsMessageID。此ArrayList为空。在初始化它和尝试访问SmsMessageID的第一个元素之间,您必须使用某些元素填充该数组。

您还可以,如果您想稍后填充该数组,请修改getID,以便在它尝试获取的元素不存在时返回一些falsey值。这将是一个很好的做法。

我不太了解该计划正在尝试做什么,没有评论,所以我很抱歉,我无法提供更多帮助。

答案 2 :(得分:1)

    #scrollUp:hover {
       background-image: url("https://cdn0.iconfinder.com/data/icons/sweets/128/lollypop_blue_candy.png");
    }