我正在尝试创建包含图片和文字的自定义列表。当我运行应用程序时,它可以正常工作,但是当我向上和向下滚动它时会出现以下错误:
11-19 17:48:09.530 10113-10113/com.example.hadi.do2get E/AndroidRuntime: java.lang.ArrayIndexOutOfBoundsException: length=7; index=7
11-19 17:48:09.530 10113-10113/com.example.hadi.do2get E/AndroidRuntime: at com.example.hadi.do2get.CustomListAdapter.getView(CustomListAdapter.java:39)
Heres是customlistadapter类:
public class CustomListAdapter extends ArrayAdapter<String> {
private final Activity context;
private final String[] itemname;
private final Integer[] imgid;
LayoutInflater inflater;
public CustomListAdapter(Activity context, String[] itemname, Integer[] imgid) {
super(context, R.layout.mylist, itemname);
// TODO Auto-generated constructor stub
this.context=context;
this.itemname=itemname;
this.imgid=imgid;
}
public View getView(int position,View view,ViewGroup parent) {
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylist, null);
TextView txtTitle = (TextView) view.findViewById(R.id.item);
ImageView imageView = (ImageView) view.findViewById(R.id.icon);
TextView extratxt = (TextView) view.findViewById(R.id.textView1);
txtTitle.setText(itemname[position]);
imageView.setImageResource(imgid[position]); //this is line 39
extratxt.setText("Description "+itemname[position]);
return view;
};
}
这是列表所在的类:
public class Forside extends ActionBarActivity implements View.OnClickListener{
CustomListAdapter listAdapter;
EditText filterText;
ImageButton sog;
EditText sogefelt;
static String [] annoncer = {"Hjælp til valutaprogram", "Renovering af kontor", "Fix af computer",
"Tegner til byggeprojekt", "Statsborgerskab", "Sushi til 100 mennesker", "Bil råd", "Finansiel rådgivning"};
static Integer[] imageId = {
R.drawable.it,
R.drawable.renovering,
R.drawable.it,
R.drawable.renovering,
R.drawable.statsborgerskab,
R.drawable.sushi,
R.drawable.bil
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_forside);
filterText = (EditText)findViewById(R.id.sogefelt);
ListView itemList = (ListView)findViewById(R.id.listView);
listAdapter = new CustomListAdapter (this, annoncer, imageId);
sog = (ImageButton) findViewById(R.id.sog);
sog.setOnClickListener(this);
sogefelt = (EditText) findViewById(R.id.sogefelt);
itemList.setAdapter(listAdapter);
itemList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), "Position " + position, Toast.LENGTH_LONG).show();
}
});
}
public void onClick (View v){
if (v == sog){
String søgeord = sogefelt.getText().toString();
ArrayList<String> resultater = new ArrayList<String>();
for(String i: annoncer) {
if (i.contains(søgeord))
resultater.add(i);
}
Intent intent = new Intent(this, Sogeresultater.class);
intent.putExtra("søg", resultater.toArray(new String[resultater.size()]));
startActivity(intent);
}
}
}
所有图片均为PNG
PS:我已经在谷歌和Stackoverflow上搜索了类似的问题/问题,但没有找到解决方案