当ListView不应出现时,它出现在ListView的第一个条目中

时间:2013-03-05 01:38:03

标签: android android-listview android-cursoradapter

这是我正在处理的应用程序中ListView的右侧。

There are two stars here. There should only be one.

如您所见,显示星形的ImageView在9个条目的列表中显示两次。

这是ListView的{​​{1}}使用的BindView方法:

CursorAdapter

如您所见,它检查数据库中的列是否为1,然后将该位布尔数学存储在变量中。使用该变量,它标记@Override public void bindView(View v, Context context, Cursor c) { AQuery aq = new AQuery(v); MyDatabaseHelper helper = new MyDatabaseHelper(context); //Used for various bits of text-setting. Is largely unrelated to the question. boolean isDefault = (c.getShort(c.getColumnIndexOrThrow(MyDatabaseHelper.DEFAULT))==1); Log.d("Binding account to view", "Entry "+currentID+"isDefault: "+isDefault); v.setTag(isDefault); if (isDefault) { aq.id(R.id.favStar).visible(); //favStar defaults to GONE. } helper.close(); } ,报告该变量的状态,然后当且仅当变量为真时才使星形可见。

这是在添加第9个条目之前填充的列表的日志输出:

View

(添加第9个条目只会在日志输出中添加“Entry 9isDefault:false”。)

任何人都可以告诉我为什么我的列表中出现了一个星形,其中调试日志表明一个不应该出现?此02-28 10:50:23.381: D/Binding account to view(20759): Entry 1isDefault: false 02-28 10:50:23.397: D/Binding account to view(20759): Entry 2isDefault: false 02-28 10:50:23.413: D/Binding account to view(20759): Entry 3isDefault: false 02-28 10:50:23.420: D/Binding account to view(20759): Entry 4isDefault: false 02-28 10:50:23.436: D/Binding account to view(20759): Entry 5isDefault: false 02-28 10:50:23.444: D/Binding account to view(20759): Entry 6isDefault: false 02-28 10:50:23.459: D/Binding account to view(20759): Entry 7isDefault: false 02-28 10:50:23.475: D/Binding account to view(20759): Entry 8isDefault: true 02-28 10:50:23.498: D/Binding account to view(20759): Entry 1isDefault: false 02-28 10:50:23.506: D/Binding account to view(20759): Entry 2isDefault: false 02-28 10:50:23.530: D/Binding account to view(20759): Entry 3isDefault: false 02-28 10:50:23.553: D/dalvikvm(20759): GC_CONCURRENT freed 122K, 2% free 11069K/11271K, paused 3ms+15ms, total 43ms 02-28 10:50:23.553: D/Binding account to view(20759): Entry 4isDefault: false 02-28 10:50:23.577: D/Binding account to view(20759): Entry 5isDefault: false 02-28 10:50:23.592: D/Binding account to view(20759): Entry 6isDefault: false 02-28 10:50:23.600: D/Binding account to view(20759): Entry 7isDefault: false 02-28 10:50:23.616: D/Binding account to view(20759): Entry 8isDefault: true 出现的ListFragmentListView所包含的活动都不会在任何地方对R.id.favStar进行任何引用。

进一步详细说明:我也在不使用AQuery(使用ListFragmentFindViewById())的情况下尝试了此操作,结果未更改为调试日志中显示的预期结果。 有问题的星星总是出现在第一个位置,只有在列表中的另一个星星出现时才会出现。(DatabaseHelper类在设置之前将数据库中的所有项目设置为ISDEFAULT = 0 ISDEFAULT = 1。)奇怪的是,使用模板ADT为您提供主/细节流程,此错误在我的Nexus 7上无法复制,但可以在我的Galaxy Nexus和模拟器上找到。

1 个答案:

答案 0 :(得分:1)

如果isDefault为假,您永远不会隐藏您的favStar视图。如果您的元素设置为true并且您显示视图,则该星形将一直显示,直到您再次隐藏它。当视图被回收时,即使不是假设,明星仍将显示。你没有在你的nexus 7上看到它,因为你可能没有足够的元素来发生这种情况,因为它的屏幕高于手机的屏幕。无论如何,将代码更改为类似应该修复它:

if (isDefault) {
  aq.id(R.id.favStar).visible(); //favStar defaults to GONE.
} else {
  //set favStar to View.GONE here
}