android - 如何避免 setViewValue 中 TextView/TextSwitcher 更新?

标签 android android-listview

我在 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/

相关文章:

android - Gradle 没有将 Dagger 2 生成的类添加到 aar

android - 如何在 Android 的标题栏中隐藏应用程序名称?

java - 如何在没有 Activity 的情况下使用 LocalBroadcastManager

java - 在安装了 facebook Lite 的手机上使用 facebook 按钮登录时出现错误

c# - 访问路径被拒绝 (Xamarin/Android)

android - 在 android 中快速滚动时避免滑动刷新

android - 如何从多选 ListView 中获取选定项目

Android:requestLayout() 调用不当

android - 更新 ListView 页脚

Android:通过 SimpleAdapter 在 imageview 中显示图像