My TextSwitcher
对于每条记录ListView
应显示第一个值(text1),然后是另一个值(text2),然后再次首先值,依此类推。仅当以下情况时才会发生text2不是空的。否则text1应始终显示(没有任何更改和动画)。
我已经创建了Runnable()
,这会改变布尔变量(time2)然后调用items.notifyDataSetChanged()
。它按预期工作并得到结果setViewValue()
for my ListView
叫做。
这是代码:
items.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewId = view.getId();
switch(viewId) {
case R.id.timetext:
TextSwitcher itemTime = (TextSwitcher) view;
if (itemTime.getChildCount() != 2) {
itemTime.removeAllViews();
itemTime.setFactory(new ViewSwitcher.ViewFactory() {
@Override
public View makeView() {
TextView t = new TextView(MyActivity.this);
t.setTextSize(18);
t.setTypeface(null, Typeface.BOLD);
t.setTextColor(Color.WHITE);
return t;
}
});
itemTime.setAnimateFirstView(true);
itemTime.setInAnimation(AnimationUtils.loadAnimation(MyActivity.this,
R.anim.push_up_in));
itemTime.setOutAnimation(AnimationUtils.loadAnimation(MyActivity.this,
R.anim.push_up_out));
}
if (!text2.equals("")) {
if (!time2) {
itemTime.setText(text1);
} else {
itemTime.setText(text2);
}
} else {
itemTime.setCurrentText(text1);
}
return true;
}
return false;
}
} );
它几乎按预期工作。对于一件小事 - 当text2应该显示,它首先将显示值更改为其他值(来自另一条记录!),然后播放动画。的变化text2 to text1正确发生。
我的理解原因如下 - 在显示之前text2, 所有视图itemTime
被删除,因此它被重新创建,这就是为什么会显示一些其他值的原因。但为什么它显示出其他记录的价值呢?
实际上text2 and text1是来自数据库的值,例如。text2 = cursor.getString(cursor.getColumnIndexOrThrow(DbAdapter.KEY_TIME_2))
,可能,这里出了点问题,setViewValue
使用错误的参数调用?
Upd. text1 and text2从数据库中读取setViewValue
。这是完整代码的示例:
itemTime.setText(cursor.getString(cursor.getColumnIndexOrThrow(DbAdapter.KEY_CLOSE_TIME_1)) + " - " + cursor.getString(cursor.getColumnIndexOrThrow(DbAdapter.KEY_OPEN_TIME_1)));