我在android中自定义创建的CursorAdapter派生类有一个奇怪的问题:
我的 getView() 实现是在许多网站/谷歌谈话中看到的直接教科书。但是,似乎对于不同的位置(调用此方法的位置参数),此方法正在传递相同的convertView实例,即使在我看来,这些应该引用不同的对象实例,因为它应该对应于ListView 中的其他可见项,并且不应在可见列表项的情况下重用相同的对象实例...
我已经删除了更新实际视图的实际部分,因为即使没有它,问题也会重现。
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder viewHolder;
Log.d("dd", "getView()");
if (convertView == null) {
Log.d("d", "convertview is null!");
// create convertView from xml
convertView = this.mInflater.inflate(R.layout.catalog_entry,
parent, false);
// create the viewHolder
viewHolder = new ViewHolder();
viewHolder.name = (TextView) convertView
.findViewById(R.id.gameName2);
viewHolder.image = (ImageView) convertView
.findViewById(R.id.gameImage);
convertView.setTag(viewHolder);
} else {
Log.d("dd", "convertview is not null");
viewHolder = (ViewHolder) convertView.getTag();
}
LinearLayout thisItem = (LinearLayout) convertView;
Log.d("thisItem",
"This Item is Index "
+ position
+ " "
+ thisItem.toString()
+ " "
+ Integer.toHexString(System.identityHashCode(thisItem))
+ "x: " + thisItem.getX() + " y: " + thisItem.getY());
this.cur.moveToPosition((int) (getItemId(position) - 1));
Log.d("dd", "End of getView()");
return convertView;
}
运行此代码会产生以下输出:
D/dd (27725): getView() D/d (27725): convertview 为空!D/thisItem(27725): This Item is Index 0 android.widget.LinearLayout@40fb5f70 40fb5f70x: 0.0 y: 0.0 D/dd
(27725): End of getView() D/dd (27725): getView() D/dd
(27725): convertview 不为空 D/thisItem(27725): 这个项目是索引 1 android.widget.LinearLayout@40fb5f70 40fb5f70x: 0.0 y: 0.0 D/dd
(27725): End of getView() D/dd (27725 ): getView() D/dd
(27725): convertview is not null D/thisItem(27725): This Item is Index 2 android.widget.LinearLayout@40fb5f70 40fb5f70x: 0.0 y: 0.0 D/dd
(27725): End of getView() D/dd (27725):getView() D/dd
(27725): convertview 不为空 D/thisItem(27725): 这个项目是索引 3 android.widget.LinearLayout@40fb5f70 40fb5f70x: 0.0 y: 0.0 D/dd
(27725): 结束 getView() D/dd (27725 ): getView() D/dd
(27725): convertview is not null D/thisItem(27725): This Item is Index 4 android.widget.LinearLayout@40fb5f70 40fb5f70x: 0.0 y: 0.0 D/dd
(27725): End of getView() D/dd (27725): getView() D/dd
(27725): convertview is not null D/thisItem(27725): This Item is Index 0 android.widget.LinearLayout@40fb5f70 40fb5f70x: 0.0 y: 0.0 D /dd
(27725): getView() 结束 D/dd (27725): getView() D/d
(27725): convertview 为空!D/thisItem(27725): This Item is Index 1 android.widget.LinearLayout@40fb89f8 40fb89f8x: 0.0 y: 0.0 D/dd
(27725):getView() D/dd 结束 (27725):getView() D/d
(27725):convertview 为空!D/thisItem(27725): This Item is Index 2 android.widget.LinearLayout@40fb9c48 40fb9c48x: 0.0 y: 0.0 D/dd
(27725): End of getView() D/dd (27725): getView() D/d
(27725):转换视图为空!D/thisItem(27725): This Item is Index 3 android.widget.LinearLayout@40fbae98 40fbae98x: 0.0 y: 0.0 D/dd
(27725): End of getView() D/dd (27725): getView() D/d
(27725):转换视图为空!D/thisItem(27725): This Item is Index 4 android.widget.LinearLayout@40fbc0e8 40fbc0e8x: 0.0 y: 0.0 D/dd
(27725): End of getView()
从一开始可以看出,对于每个位置(0 到 4),都在发送相同的 View 对象哈希......