Pav*_*los 5 android listview android-listview android-adapter
奇怪的是,我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.
}
Run Code Online (Sandbox Code Playgroud)
其他方法
@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;
}
Run Code Online (Sandbox Code Playgroud)
我在Google上搜索并尝试了一切!似乎什么都没有给我一个解决方案.
任何帮助,将不胜感激!如果您需要更多代码,请告诉我们.
我怀疑这mItemView就是这里的罪魁祸首。从名称来看,这是一个实例字段,因此如果多个线程getView()在您的 中调用CustomBaseAdapter,则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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
5312 次 |
| 最近记录: |