我有一个带有自定义ListView项的ListView!
图片-> http://imageshack.us/photo/my-images/155/08pictureslistcopy.png/
要在列表视图中显示正确的图片,我有一个ArrayList,其picture_ID以正确的顺序显示在列表视图中。
我还创建了一个名为AttachmentAdapter的类,该类扩展了BaseAdapter。在getView方法中,我从ArrayList中获取正确的picture_Id并显示该项目。
我已经调试了从ArrayList中获取picture_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:仅在设备上进行过测试(三星Galaxy S2-Android 4.0.4)
似乎您只在convertView为null的情况下更新行的内容。您将标签拉出,但您从不对其进行任何操作。如果传入的视图为null,则创建一个新视图,设置支架,并结束if。您所有的内容分配都应在if-else语句之后。
这样做吧:
ViewHolder holder = null;
if (convertView == null) {
// create new view
// setup holder
}
else {
holder = (ViewHolder) convertView.getTag();
}
try {
// set values on the holder
}
catch (Exception e) { }
return convertView;