我在 setViewValue 的代码中更新了 TextSwitcher 的文本。但如果数据库值没有改变,我想避免它的更新。 这是我当前的代码:
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int viewId = view.getId();
switch(viewId) {
case R.id.timetext:
TextSwitcher twTime = (TextSwitcher) view;
// something is wrong in the next line
if (!cursor.getString(columnIndex).equals(getText(viewId)))
{
twTime.setText(cursor.getString(columnIndex));
}
return true;
不知何故,数据仍在更新。为什么?
看起来 getText()
无法与 TextSwitcher
一起正常工作。如何获取它的值?
最佳答案
好的,我找到答案了。
首先要获取 TextSwitcher
的文本,应使用以下代码:
TextView tv = (TextView) twTime.getCurrentView();
if (!cursor.getString(columnIndex).equals(tv.getText().toString())) {
twTime.setText(cursor.getString(columnIndex));
}
但在此之前
twTime.setCurrentText(cursor.getString(columnIndex));
应该使用。
关于android - 如何避免 setViewValue 中 TextView/TextSwitcher 更新?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6089304/