How to avoid TextView/TextSwitcher update in setViewValue?

僤鯓⒐⒋嵵緔 提交于 2020-01-05 02:39:57

问题


I update the text of my TextSwitcher in the code of setViewValue. But in case database value is not changed, I would like to avoid its update. Here is my current code:

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;

Somehow, the data is still updated. Why?

Looks like getText() doesn't work properly with TextSwitcher. How to get its value?


回答1:


Ok, I found the answer. First of all to get the text of TextSwitcher the following code should be used:

TextView tv = (TextView) twTime.getCurrentView();
if (!cursor.getString(columnIndex).equals(tv.getText().toString())) {
    twTime.setText(cursor.getString(columnIndex));
}

but prior to that

twTime.setCurrentText(cursor.getString(columnIndex));

should be used.



来源:https://stackoverflow.com/questions/6089304/how-to-avoid-textview-textswitcher-update-in-setviewvalue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!