检查ListView适配器是否正确绑定所有数据。
以上回复有部分引自免费微信小程序:皆我百晓生。 如问题尚未解决,还请稍待,如有疑问请回复进一步沟通。
根据您提供的代码片段,我注意到在getView()
方法中的问题。当列表项被重用时(即convertView不为null),你应该从Cursor中获取当前位置的数据并更新视图。然而,在您的代码中,您始终尝试将数据移动到position,这可能导致在某些情况下不会正确显示所有数据。
以下是修改后的getView()
方法,修复了这个问题:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewUser viewUser;
if (convertView == null){
viewUser = new ViewUser();
convertView = LayoutInflater.from(context).inflate(R.layout.listview2, null);
viewUser.name = convertView.findViewById(R.id.name);
viewUser.age = convertView.findViewById(R.id.age);
viewUser.weight = convertView.findViewById(R.id.weight);
convertView.setTag(viewUser);
} else {
viewUser = (ViewUser) convertView.getTag();
}
if (cursor != null && cursor.moveToPosition(position)) { // Check if cursor is not null before moving to position
viewUser.name.setText(DataBase.slh(context).getName(cursor));
viewUser.age.setText(DataBase.slh(context).getAge(cursor));
viewUser.weight.setText(DataBase.slh(context).getWeight(cursor));
}
return convertView;
}
另外,关于 "nodebuggable process detected" 的警告,这通常意味着你的应用可能没有正确的调试信息,导致无法进行调试。这不会影响应用的功能,但会使得调试变得困难。要解决这个问题,你需要确保在构建应用时启用了调试选项。对于Android Studio,你可以在项目设置的build.gradle
文件中检查android{}
部分下的debuggable true
是否被注释掉或者配置错误。如果没有问题,那么可能是你的设备设置问题,确保在设备开发者选项中未禁用USB调试。
如果你仍然遇到问题,建议检查一下以下几个方面:
DataBase.slh(context)
返回的是正确的实例,并且它能正确地从Cursor中获取数据。listview2.xml
布局文件,确认视图ID(name、age、weight)是否与Java代码中匹配。如果你需要更深入的帮助,比如代码审查或者具体步骤指导,请随时告诉我。