我的listview适配器有点问题。我正在为我的应用程序使用延迟加载列表视图,我从数据库填充。问题是我必须从2个不同的表中获取数据。这是我正在使用的一些代码:
String sqlQuery = "SELECT categoryId, collectionId, objectId, title FROM cards WHERE collectionId="+collId;
Cursor cursor = userDbHelper.executeSQLQuery(sqlQuery);
String sqlQuery2 = "SELECT objectId, title FROM categories";
Cursor cursorCat = userDbHelper.executeSQLQuery(sqlQuery2);
cursorCat.moveToFirst();
if(cursor.getCount()==0 && cursorCat.getCount()==0){
Log.i("No Thumbnails","There are no Cards or Thumbnails.");
cursor.close();
cursorCat.close();
} else if(cursor.getCount()>0 && cursorCat.getCount()>0){
for(cursor.move(0); cursor.moveToNext(); cursor.isAfterLast()){
title = cursor.getString(cursor.getColumnIndex("title"));
Log.v("Title","Title : "+title);
categoryId = Integer.parseInt(cursor.getString(cursor.getColumnIndex("categoryId")));
Log.v("Category Id","Category Id : "+categoryId);
names.add(title);
}
for(cursorCat.move(0); cursorCat.moveToNext(); cursorCat.isAfterLast()){
title = cursorCat.getString(cursorCat.getColumnIndex("title"));
Log.v("Title","Title : "+title);
categoryId = Integer.parseInt(cursorCat.getString(cursorCat.getColumnIndex("objectId")));
Log.v("Category Id","Category Id : "+categoryId);
categories.add(title);
}
}
所以我必须从categoryId
检查cards
并从title
表中获取该类别的categories
。但问题是我有120个不同的名称,当我在列表视图上加载它们时,应用程序崩溃,因为类别的数量是11.所以这是适配器的代码:
public View getView(int position, View convertView, ViewGroup parent) {
View vi=convertView;
ViewHolder holder;
if(convertView==null){
vi = inflater.inflate(R.layout.item, null);
holder=new ViewHolder();
holder.name=(TextView)vi.findViewById(R.id.name);
holder.info=(TextView)vi.findViewById(R.id.info);
holder.image=(ImageView)vi.findViewById(R.id.image);
vi.setTag(holder);
Log.v("Position","Position : "+position);
}
else
holder=(ViewHolder)vi.getTag();
holder.name.setText(name.get(position));
holder.info.setText(info.get(position));
//Here I must do a black magic and get the images if user had 'em.
holder.image.setImageBitmap(b);
//holder.image.setTag(data[position]);
//imageLoader.DisplayImage(data[position], activity, holder.image);
// Black magic over.
return vi;
}
应用程序崩溃在该行:holder.info.setText(info.get(position));
,但有以下异常:
10-14 15:55:42.378: ERROR/AndroidRuntime(16468): java.lang.IndexOutOfBoundsException: Invalid index 11, size is 11
那么如何在适配器中设置类别的大小?有什么建议吗?