我有一个包含自定义 ListView 项目的 ListView!
图片 --> http://imageshack.us/photo/my-images/155/08pictureslistcopy.png/
为了在 Listview 中显示正确的图片,我有一个 ArrayList,其中 picture_ID 以正确的顺序用于列表视图。
我还创建了一个名为 AttachmentAdapter 的类,它扩展了 BaseAdapter。在 getView 方法中,我从 ArrayList 中获取正确的 picture_Id 并显示该项目。
我已经调试了从 ArrayList 获取图片 ID 的过程,这工作正常:-)
我的问题是在我的列表视图中有 6 个或更多条目后,getView 方法开始给我错误的项目位置。所以我在列表中的错误位置显示了错误的图片。
谁能告诉我为什么 BaseAdapters getView 方法会通过错误的位置?
这是我的 BaseAdapter 代码:
private class AttachmentAdapter extends BaseAdapter {
private LayoutInflater mInflater;
private ArrayList<Data_Attachment> mData = new ArrayList<Data_Attachment>();
public AttachmentAdapter(Context fragment) {
// super(fragment, textViewResourceId, items);
mInflater = LayoutInflater.from(fragment);
}
public void setData(ArrayList<Data_Attachment> data)
{
mData = data;
}
private Data_Attachment getAttachmentfromPosition(int pos)
{
return DataResources.getAttachment(position_list.get(pos));
}
@Override
public View getView(int position, View convertView, ViewGroup parent)
{
Data_Attachment data = getAttachmentfromPosition(position);
ViewHolder holder = null;
Log.e("TEST","getView " + position + ", Attachment: #" + data.getID() + " --> " + data.getBriefDescr());
if (convertView == null) {
convertView = mInflater.inflate(R.layout.listpictures_item, null);
holder = new ViewHolder();
holder.name= (TextView) convertView.findViewById(R.id.idPictureName);
holder.date= (TextView) convertView.findViewById(R.id.idPictureDate);
holder.uploader= (TextView) convertView.findViewById(R.id.idPictureUploader);
holder.icon = (ImageView) convertView.findViewById(R.id.idListPicture_Preview);
try{
Bitmap thumbnail = data.getThumbnail(getApplicationContext());
if(thumbnail != null)
{
holder.icon.setImageBitmap(thumbnail);
}
else
{
holder.icon.setImageDrawable(getApplicationContext().getResources().getDrawable(R.drawable.ic_notavailable));
}
}
catch(Exception e)
{
Log.e("TEST","PicturesIncident_Fragment.AttachmentData.getView, Error while showing picture: " + e.toString());
holder.icon.setImageDrawable(getApplicationContext().getResources().getDrawable(R.drawable.ic_notavailable));
}
String name = data.getBriefDescr();
if(name.length() == 0)
{
holder.name.setVisibility(View.GONE);
holder.uploader.setTextSize(14);
holder.uploader.setTypeface(Typeface.DEFAULT_BOLD);
}
else
{
//Kürzen nach 40 Strings
if(name .length() >= _Settings.MAX_ATTACHMENT_NAME_SIZE)
{
char[] buf = new char[_Settings.MAX_ATTACHMENT_NAME_SIZE];
name.getChars(0, _Settings.MAX_ATTACHMENT_NAME_SIZE-1, buf, 0);
name = String.valueOf(buf) +"...";
}
holder.name.setText(name);
}
//Set uploader and Date
Data_Worker data_worker = DataResources.getWorkerData_fromID(data.getuploaderID());
String uploader = data_worker.getName();
String date = data.getModified_str();
holder.uploader.setText(uploader);
//TODO change to view the date
// holder.date.setText(date);
holder.date.setText("pos: "+ position + " , #" + data.getID());
convertView.setTag(holder);
} else {
holder = (ViewHolder)convertView.getTag();
}
return convertView;
}
class ViewHolder {
TextView name;
TextView uploader;
TextView date;
ImageView icon;
}
@Override
public int getCount() {
return mData.size();
}
@Override
public Data_Attachment getItem(int position) {
return mData.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
}
出于测试目的,如您在代码和下面的屏幕截图中所见,我显示了此项目在列表视图中的当前位置。
图片 --> http://imageshack.us/photo/my-images/805/device20121010155051.png/
在这里你可以看到位置 0 不在列表的顶部。
此外,在我上下滚动一点后,位置会发生变化,我无法弄清楚位置变化背后的任何逻辑。
PS:仅在设备上测试过(Samsung Galaxy S2 - Android 4.0.4)