Java android中的索引越界

Java android中的索引越界,java,android,exception,indexoutofboundsexception,Java,Android,Exception,Indexoutofboundsexception,我试图从api中读取数据,在DB中填充数据并在列表视图中显示。我不断地得到索引范围之外的数据,但我不知道为什么。Book是一个简单的Book类,具有sting phoneNo和字符串名称。我能够检索数据。当我打印我获取的数据时,我得到了正确的输出,但当我试图将数据插入数据库并获取数据时,我不断遇到问题 List<Book> list = db.getAllBooks(); for(int i = 0; i<4; i++) {

我试图从api中读取数据,在DB中填充数据并在列表视图中显示。我不断地得到索引范围之外的数据,但我不知道为什么。Book是一个简单的Book类,具有sting phoneNo和字符串名称。我能够检索数据。当我打印我获取的数据时,我得到了正确的输出,但当我试图将数据插入数据库并获取数据时,我不断遇到问题

            List<Book> list = db.getAllBooks();
            for(int i = 0; i<4; i++) {
                thatName[i] = list.get(i).getName();
                thatMobile[i] = list.get(i).getPhoneNo();
                Log.d("List 1", thatName[i]);
                Log.d("List 2", thatMobile[i]);
            }

            customListViewAdapter = new CustomListViewAdapter(getApplication(),thatName,thatMobile);

            listView.setAdapter(customListViewAdapter);
    };
List List=db.getAllBooks();

对于(int i=0;i您指定将迭代四项:

for(int i = 0; i<4; i++) {
    thatName[i] = list.get(i).getName();
    thatMobile[i] = list.get(i).getPhoneNo();
    Log.d("List 1", thatName[i]);
    Log.d("List 2", thatMobile[i]);
}
或者只需迭代列表本身:

for(Book book: list)

请将您的logcat发送给(int i=0;i
for(int i = 0; i < list.size(); i++)
for(Book book: list)