得到例外:索引-1,大小为3

时间:2015-09-16 08:50:29

标签: android listview

我通过以下代码填充列表。填充列表后,我将其设置为listView。

public void refreshSmsInbox() {
        ContentResolver contentResolver = getContentResolver();
        // Cursor smsInboxCursor = contentResolver.query(
        // Uri.parse("content://sms/inbox"), null, null, null, null);

        Cursor smsInboxCursor = getContentResolver().query(
                Uri.parse("content://sms/inbox"), new String[] {},
                "read = 1 and address='" + pre_address + "'", null, null);
        int indexBody = smsInboxCursor.getColumnIndex("body");
        int indexAddress = smsInboxCursor.getColumnIndex("address");
        if (indexBody < 0 || !smsInboxCursor.moveToFirst())
            return;
        arrayAdapter.clear();
        smsBody = new ArrayList<String>();
        String fromNumber = "";
        do {
            if (pre_address.equals(smsInboxCursor.getString(indexAddress))) {
                String str = "SMS From: "
                        + smsInboxCursor.getString(indexAddress) + "\n"
                        + smsInboxCursor.getString(indexBody) + "\n";
                fromNumber = smsInboxCursor.getString(indexAddress);
                smsBody.add(smsInboxCursor.getString(indexBody));
                // arrayAdapter.add(str);
            }
        } while (smsInboxCursor.moveToNext());
        try {
            Cursor smsInboxCursor1 = getContentResolver().query(
                    Uri.parse("content://sms/inbox"), new String[] {},
                    "read = 0 and address='" + pre_address + "'", null, null);
            indexBody = smsInboxCursor1.getColumnIndex("body");
            indexAddress = smsInboxCursor1.getColumnIndex("address");
            do {
                if (pre_address.equals(smsInboxCursor1.getString(indexAddress))) {
                    String str = "SMS From: "
                            + smsInboxCursor1.getString(indexAddress) + "\n"
                            + smsInboxCursor1.getString(indexBody) + " \n";
                    fromNumber = smsInboxCursor1.getString(indexAddress);
                    smsBody.add(smsInboxCursor1.getString(indexBody));
                    // arrayAdapter.add(str);
                }
            } while (smsInboxCursor1.moveToNext());

        } catch (Exception e) {
            Toast.makeText(this, " message is "+e.getMessage(), Toast.LENGTH_LONG).show();
        }
        arrayAdapter = new SmsArrayAdapter(this, R.layout.row_item, smsBody,
                fromNumber);
        smsListView.setAdapter(arrayAdapter);
        Toast.makeText(this, "The list size is " + smsBody.size(),
                Toast.LENGTH_LONG).show();
    }

但我有一个例外,&#34;索引-1请求大小为3&#34; 。为什么我会得到例外?我怎么解决这个问题?

2 个答案:

答案 0 :(得分:2)

int getColumnIndex (String columnName)

返回给定列名的从零开始的索引,,如果该列不存在,则返回-1。如果您希望该列存在,请使用getColumnIndexOrThrow(String),这将使错误更加清晰。

即使它不是那个函数, getIndexOfSomething()类型的函数通常在没有找到任何内容时返回-1(因为0是有效索引)。

答案 1 :(得分:1)

你有检查

if (indexBody < 0 || !smsInboxCursor.moveToFirst())
            return;

表示read = 1短信,但没有检查read = 0短信息。

如果您的目的是获取包含所有已读消息和所有未读消息的列表,而不是上述检查后的return,则应跳至第二部分以获取read = 0消息,并对smsInboxCursor1上的空结果执行相同的检查。