BaseAdapter在getView()方法调用上返回错误的位置(BaseAdapter returns wrong position on getView() method call)

编程入门 行业动态 更新时间:2024-10-28 02:33:58
BaseAdapter在getView()方法调用上返回错误的位置(BaseAdapter returns wrong position on getView() method call)

奇怪的是,我的CustomBaseAdapter为需要膨胀的项返回了错误的位置,因此适配器会在行上显示错误的数据类型!

虽然我正在使用ViewHolder模式,但我的ListView layout_height设置为match_parent并且我发现的每种可能的方式,为了确保ListView项目的稳定性,已经实现, CustomBaseAdapter似乎没有响应它。

getView()方法

@Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; mItemView = convertView; if (convertView == null) { mItemView = mInflater.inflate(R.layout.layout_listview_row, parent, false); holder = new ViewHolder(); //setting up the Views mItemView.setTag(holder); } else { holder = (ViewHolder) mItemView.getTag(); } //Getting the item final MyItem item = getItem(position); //Doing some checks on my item and then display the appropriate data. //By saying checks i mean something like: if(item.getSomething().equals("blabla")){ //Load some pic }else{ //Load another pic } //Now when i have scrolled the list once and return back to top, //Suddenly in Logcat i am seeing that the first row is getting matched to //the object in the 4th position, but it doesnt display its data. It displays //the text from the first item as it was supposed to do. But the relation between //the first row and the item's position is like 0->4. }

其他方法

@Override public int getCount() { return this.mObjects.size(); } @Override public MyItem getItem(int position) { return this.mObjects.get(position); } @Override public long getItemId(int position) { return position; }

我在Google上搜索并尝试了一切! 似乎什么都没有给我一个解决方案。

任何帮助,将不胜感激! 如果您需要更多代码,请告诉我们。

Strangely enough my CustomBaseAdapter is returning wrong position for the item that needs to be inflated and so on the adapter takes the wrong kind of data to display on the row!

Although i am using the ViewHolder pattern, my ListView layout_height is set to match_parent and every possible way i have found, to ensure ListView items stability, is already implemented, the CustomBaseAdapter seems not to be responding at it.

getView() method

@Override public View getView(final int position, View convertView, ViewGroup parent) { final ViewHolder holder; mItemView = convertView; if (convertView == null) { mItemView = mInflater.inflate(R.layout.layout_listview_row, parent, false); holder = new ViewHolder(); //setting up the Views mItemView.setTag(holder); } else { holder = (ViewHolder) mItemView.getTag(); } //Getting the item final MyItem item = getItem(position); //Doing some checks on my item and then display the appropriate data. //By saying checks i mean something like: if(item.getSomething().equals("blabla")){ //Load some pic }else{ //Load another pic } //Now when i have scrolled the list once and return back to top, //Suddenly in Logcat i am seeing that the first row is getting matched to //the object in the 4th position, but it doesnt display its data. It displays //the text from the first item as it was supposed to do. But the relation between //the first row and the item's position is like 0->4. }

Other methods

@Override public int getCount() { return this.mObjects.size(); } @Override public MyItem getItem(int position) { return this.mObjects.get(position); } @Override public long getItemId(int position) { return position; }

I have literally searched and tried everything on Google! Nothing seems to have given me a solution.

Any help would be appreciated! Let me know if you need any more part of code.

最满意答案

我怀疑mItemView是罪魁祸首。 根据名称判断,这是一个实例字段,因此如果多个线程调用mItemView getView() ,则mItemView可能会更改它指向您的鼻子下方的哪个回收视图。 另外,我假设getView()以return mItemView结束,对吧?

无论如何,我建议尝试删除mItemView ,只需编写如下函数:

@Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = mInflater.inflate(R.layout.layout_listview_row, parent, false); ViewHolder holder = new ViewHolder(); //setting up the Views convertView.setTag(holder); } ViewHolder holder = (ViewHolder) convertView.getTag(); // ... return convertView; }

I suspect that mItemView is the culprit here. Judging by the name, this is an instance field, so if more than one thread calls getView() in your CustomBaseAdapter, then mItemView may change which recycled view it is pointing at right under your nose. Also, I assume that getView() ends with a return mItemView, right?

Anyway, I would advise to try eliminating mItemView and just write the function like this:

@Override public View getView(int position, View convertView, ViewGroup parent) { if (convertView == null) { convertView = mInflater.inflate(R.layout.layout_listview_row, parent, false); ViewHolder holder = new ViewHolder(); //setting up the Views convertView.setTag(holder); } ViewHolder holder = (ViewHolder) convertView.getTag(); // ... return convertView; }

更多推荐

本文发布于:2023-08-01 00:49:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1350494.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:错误   位置   方法   getView   BaseAdapter

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!