Android ListView基本适配器的位置错误
我有一个自定义ListView项目的ListView 图片--> 为了在列表视图中显示正确的图片,我有一个带有图片ID的ArrayList,它在列表视图中的顺序正确 我还创建了一个名为AttachmentAdapter的类,它扩展了BaseAdapter。 在getView方法中,我从ArrayList中获取正确的图片Id并显示该项 我已经调试了从ArrayList获取图片ID的过程,这是正确的:-) 我的问题是,在listview中有6个或更多条目之后,getView方法开始给我错误的条目位置。因此,我在列表中的错误位置显示了错误的图片 有人能告诉我为什么BaseAdapters getView方法会传递错误的位置吗 下面是我的BaseAdapter代码:Android ListView基本适配器的位置错误,android,listview,adapter,Android,Listview,Adapter,我有一个自定义ListView项目的ListView 图片--> 为了在列表视图中显示正确的图片,我有一个带有图片ID的ArrayList,它在列表视图中的顺序正确 我还创建了一个名为AttachmentAdapter的类,它扩展了BaseAdapter。 在getView方法中,我从ArrayList中获取正确的图片Id并显示该项 我已经调试了从ArrayList获取图片ID的过程,这是正确的:-) 我的问题是,在listview中有6个或更多条目之后,getView方法开始给我错误的条目位置
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;
}
}
私有类AttachmentAdapter扩展BaseAdapter{
私人停车场;
private ArrayList mData=new ArrayList();
公共AttachmentAdapter(上下文片段){
//super(片段、textViewResourceId、条目);
mInflater=LayoutInflater.from(片段);
}
公共void setData(ArrayList数据)
{
mData=数据;
}
私有数据\附件getAttachmentfromPosition(int pos)
{
返回DataResources.getAttachment(position_list.get(pos));
}
@凌驾
公共视图getView(int位置、视图转换视图、视图组父视图)
{
数据\附件数据=getAttachmentfromPosition(位置);
ViewHolder=null;
Log.e(“TEST”、“getView”+position+”,附件:#“+data.getID()+”-->“+data.getBriefDescr());
if(convertView==null){
convertView=mInflater.充气(R.layout.listpictures\u项,空);
holder=新的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\u预览);
试一试{
位图缩略图=data.getThumbnail(getApplicationContext());
如果(缩略图!=null)
{
holder.icon.setImageBitmap(缩略图);
}
其他的
{
setImageDrawable(getApplicationContext().getResources().getDrawable(R.drawable.ic_notavailable));
}
}
捕获(例外e)
{
Log.e(“TEST”,“PicturesIncident_Fragment.AttachmentData.getView”,显示图片时出错:“+e.toString());
setImageDrawable(getApplicationContext().getResources().getDrawable(R.drawable.ic_notavailable));
}
字符串名称=data.getBriefDescr();
if(name.length()==0)
{
holder.name.setVisibility(View.GONE);
支架。上传器。setTextSize(14);
holder.uploader.setTypeface(Typeface.DEFAULT_BOLD);
}
其他的
{
//Kürzen nach 40弦
if(name.length()>=\u设置.MAX\u附件\u名称\u大小)
{
char[]buf=新字符[\u设置。最大附件名称\u大小];
name.getChars(0,_Settings.MAX_ATTACHMENT_name_SIZE-1,buf,0);
name=String.valueOf(buf)+“…”;
}
holder.name.setText(名称);
}
//设置上载程序和日期
Data\u Worker Data\u Worker=DataResources.getWorkerData\u fromID(Data.getuploaderID());
String uploader=data_worker.getName();
字符串日期=data.getModified_str();
holder.uploader.setText(上传器);
//TODO更改以查看日期
//holder.date.setText(日期);
holder.date.setText(“pos:+position+”,#“+data.getID());
convertView.setTag(支架);
}否则{
holder=(ViewHolder)convertView.getTag();
}
返回视图;
}
类视图持有者{
文本视图名称;
文本视图上传器;
文本查看日期;
图像视图图标;
}
@凌驾
public int getCount(){
返回mData.size();
}
@凌驾
公共数据\u附件getItem(内部位置){
返回mData.get(位置);
}
@凌驾
公共长getItemId(int位置){
返回位置;
}
}
出于测试目的,我显示了这个项目在listview中的当前位置,正如您在下面的代码和屏幕截图中看到的
图片-->
在这里,您可以看到位置0不在列表的顶部
此外,在我上下滚动一点后,位置会发生变化,我无法理解位置变化背后的任何逻辑
附言:
仅在设备上测试(三星Galaxy S2-安卓4.0.4)如果convertView为空,您似乎只在更新行的内容。你把标签拉出来,但你从来没有用它做过任何事。如果传入的视图为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;
非常感谢。就这样!有时候你忘了看电视