我的列表数组有点问题,它最初是教程中的 4 个项目,然后我尝试使用它并通过输入更多标题和图像来扩大它的大小,但我
在 logcat 中收到以下错误:
java.lang.ArrayIndexOutOfBoundsException = length=4; index=4
这是代码:
package org.iimed.www;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
import org.iimed.www.R;
import org.iimed.www.CustomListViewAdapter;
import org.iimed.www.RowItem;
public class ImageTextListViewActivity extends Activity implements
OnItemClickListener {
public static final String[] titles = new String[] { "Penicillins",
"Cephalosporins", "Monobactams", "Carbapenems","Macrolides","Tetracyclines",};
public static final String[] descriptions = new String[] {
"It is an aggregate accessory fruit",
"It is the largest herbaceous flowering plant", "Citrus Fruit",
"Mixed Fruits" };
public static final Integer[] images = { R.drawable.antibiotic_buton,
R.drawable.antibiotic_buton, R.drawable.antibiotic_buton, R.drawable.antibiotic_buton, R.drawable.antibiotic_buton,R.drawable.antibiotic_buton, };
ListView listView;
List<RowItem> rowItems;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.screj);
rowItems = new ArrayList<RowItem>();
for (int i = 0; i <= titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
rowItems.add(item);
}
listView = (ListView) findViewById(R.id.list);
CustomListViewAdapter adapter = new CustomListViewAdapter(this,
R.layout.list_item, rowItems);
listView.setAdapter(adapter);
listView.setOnItemClickListener(this);
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast toast = Toast.makeText(getApplicationContext(),
"Item " + (position + 1) + ": " + rowItems.get(position),
Toast.LENGTH_SHORT);
toast.setGravity(Gravity.BOTTOM|Gravity.CENTER_HORIZONTAL, 0, 0);
toast.show();
}
}
看到<=titles.length,“titles”的增加不应该增加长度吗?还是布局本身的列表视图太小?感谢您的时间。