gpt4 book ai didi

java - 在 CursorAdapter 中为可见 View 传递相同的 convertView 实例

转载 作者:行者123 更新时间:2023-11-29 07:59:38 25 4
gpt4 key购买 nike

我在 android 中自定义创建的 CursorAdapter 派生类有一个奇怪的问题:

我的 getView() 实现是在许多网站/Google 演讲中看到的直接教科书。然而,似乎对于不同的positions(调用此方法的位置参数),此方法正在传递 convertView 的相同实例,即使正如我所见,这些应该引用不同的对象实例,因为它应该对应于 ListView 中的其他可见项,并且在可见列表项的情况下不应重用相同的对象实例...

我删除了更新实际 View 的实际部分,因为即使没有它,问题也会重现。

@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 is null! 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 is not null D/thisItem(27725): This Item is Index 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 is not null D/thisItem(27725): This Item is Index 3 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 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): End of getView() D/dd (27725): getView() D/d
(27725): convertview is null! D/thisItem(27725): This Item is Index 1 android.widget.LinearLayout@40fb89f8 40fb89f8x: 0.0 y: 0.0 D/dd
(27725): End of getView() D/dd (27725): getView() D/d
(27725): convertview is null! 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): convertview is null! 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): convertview is null! 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 对象哈希...

最佳答案

简而言之,将您的 ListView 的高度设置为 match_parent 或其他固定高度。

ListView 以您看到的“空运行”方式调用 getView() 的原因有很多,最常见的是因为您使用了 wrap_content 作为 ListView 的高度。 Android 必须膨胀一堆行来计算 wrap_content 的高度,但它不能使用真实数据,因为它尚不可用。因此,适配器会抛出这些最佳猜测。稍后使用实际数据(重新)创建布局,这就是为什么您会看到每行创建两次。

另外一个 CursorAdapter 应该维护适当的行本身,你不需要这一行:

this.cur.moveToPosition((int)(getItemId(position) - 1));

关于java - 在 CursorAdapter 中为可见 View 传递相同的 convertView 实例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15232895/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com