观察viewmodel中的实时数据以通过属性绑定更新UI

时间:2019-04-06 12:51:10

标签: android repository-pattern android-databinding android-livedata android-viewmodel

我想观察; Name: testirv.asm ; Assemble: tasm testirv ; Link: tlink /3 testirv ; Run: testirv Take whatever you need INCLUDE IRVINE16.inc INCLUDELIB Irvine16.lib INCLUDE beeftasm.inc .CODE main PROC mov ax, @data mov ds, ax call WhereXY mov WORD PTR [buf], dx call Clrscr call WhereXY mov WORD PTR [buf+2], dx lea dx, tClrscr call WriteString movzx eax, WORD PTR [buf+2] call WriteDec mov al, ' ' call WriteChar movzx eax, WORD PTR [buf] call WriteDec call Crlf lea dx, tDumpMem call WriteString lea si, hello mov cx, 16 mov bx, 1 mov dx, OFFSET hello call DumpMem lea dx, tGetCommandtail call WriteString lea dx, buf call GetCommandtail call WriteString call Crlf lea dx, tWriteString call WriteString mov dx, OFFSET hello call WriteString call Crlf lea dx, tWriteChar call WriteString mov al, [hello] call WriteChar mov al, [hello+6] call WriteChar call Crlf lea dx, tWriteInt call WriteString mov eax, 1234567890 call WriteInt mov al, ' ' call WriteChar call WriteChar call WriteChar lea dx, tWriteDec call WriteString mov eax, 1234567890 call WriteDec mov al, ' ' call WriteChar call WriteChar call WriteChar lea dx, tWriteHex call WriteString mov eax, 1234567890 call WriteHex call Crlf lea dx, tWriteHexB call WriteString mov eax, 1234567890 mov ebx, 1 call WriteHexB call Crlf lea dx, tWriteBin call WriteString mov eax, 1234567890 call WriteBin call Crlf lea dx, tWriteBinB call WriteString mov eax, 1234567890 mov ebx, 1 call WriteBinB call Crlf lea dx, tGetMaxXY call WriteString call GetMaxXY movzx eax, dl call WriteDec mov al, ' ' call WriteChar movzx eax, dh call WriteDec call Crlf lea dx, tGetMseconds call WriteString call GetMseconds mov DWORD PTR [buf], eax call WriteDec call Crlf call WhereXY push dx mov dh, 6 mov dl, 40 call Gotoxy lea dx, tGotoxy call WriteString mov bl, lightBlue lea si, hello2 call WriteColoredString pop dx call Gotoxy lea dx, tIsDigit call WriteString lea si, tDecStr J1: lodsb test al, al jz J2 call IsDigit setz al or al, 30h call WriteChar jmp J1 J2: call Crlf lea dx, tRandom32 call WriteString call Random32 call WriteDec call Crlf lea dx, tRandomRange call WriteString mov ecx, 10 J3: mov eax, 1000 call RandomRange call WriteDec mov al, ' ' call WriteChar loop J3 call Crlf lea dx, tRandomize call WriteString call Randomize mov ecx, 10 J4: mov eax, 1000 call RandomRange call WriteDec mov al, ' ' call WriteChar loop J4 call Crlf lea dx, tStr_compare call WriteString push OFFSET tString2 push OFFSET tString1 call Str_compare setz al movzx eax, al call WriteDec mov al, ' ' call WriteChar push OFFSET tString1 push OFFSET tString1 call Str_compare setz al movzx eax, al call WriteDec call Crlf lea dx, tStr_copy call WriteString mov ax, SEG buf mov es, ax push OFFSET buf push OFFSET tString1 call Str_copy xor al, al mov cx, 0FFFFh lea di, buf repne scasb mov BYTE PTR [di-1], ' ' push di push OFFSET tString2 call Str_copy lea dx, buf call WriteString call Crlf lea dx, tStr_length call WriteString push OFFSET tString1 call Str_length call WriteDec call Crlf lea dx, tStr_trim call WriteString mov eax, 'sss' mov DWORD PTR [tString2 + 3], 'sss' mov ax, ds mov es, ax push 's' push OFFSET tString2 call Str_trim cld ; Restore the default direction (up) lea dx, tString2 call WriteString call Crlf lea dx, tStr_ucase call WriteString push OFFSET tString1 call Str_ucase lea dx, tString1 call WriteString call Crlf lea dx, tWaitMsg call WriteString call WaitMsg lea dx, tDumpRegs call WriteString call DumpRegs lea dx, tShowFPUStack call WriteString finit fld1 fldPI fldz fld fDouble fld fSingle call ShowFPUStack call Crlf lea dx, tWriteFloat call WriteString call WriteFloat call Crlf lea dx, tReadFloat call WriteString finit call ReadFloat call GotoPrevChar mov al, ' ' call WriteChar call WriteFloat call Crlf lea dx, tReadString call WriteString lea dx, buf call ReadString push ax call GotoPrevChar mov al, ' ' call WriteChar pop ax movzx eax, ax call WriteDec mov al, ' ' call WriteChar lea dx, buf call WriteString call Crlf lea dx, tReadInt call WriteString call ReadInt push eax call GotoPrevChar mov al, ' ' call WriteChar pop eax call WriteDec call Crlf lea dx, tReadChar call WriteString call ReadChar push ax mov al, ' ' call WriteChar pop ax call WriteChar call Crlf lea dx, tReadHex call WriteString call ReadHex mov esi, eax call GotoPrevChar mov al, ' ' call WriteChar mov eax, esi call WriteBin mov al, ' ' call WriteChar mov eax, esi call WriteInt mov al, ' ' call WriteChar mov eax, esi call WriteDec call Crlf call Crlf call WaitMsg mov ax, red call SetTextColor lea dx, tSetTextColor call WriteString lea dx, hello1 call WriteString call Crlf lea dx, tDelay call WriteString call GetMseconds mov ebx, eax mov eax, 100000 call Delay call GetMseconds sub eax, ebx call WriteDec call Crlf mov ax, 4c00h int 21h main ENDP ;--------------------------------------------------- WhereXY PROC ; ; Gets the cursor position on video page 0. ; Receives: nothing ; Returns: DH, DL = row, column ;--------------------------------------------------- push ax push bx push cx mov ah, 3 xor bh, bh int 10h pop cx pop bx pop ax ret WhereXY ENDP ;--------------------------------------------------- WhatXY PROC ; ; Gets the character at cursor position on video page 0. ; Receives: nothing ; Returns: AH, AL = attribute, character ;--------------------------------------------------- push bx mov ah, 8 xor bh, bh int 10h pop bx ret WhatXY ENDP ;--------------------------------------------------- GotoPrevChar PROC ; ; Gets the previous character from cursor position on video page 0. ; Receives: nothing ; Returns: Cursor on the video position right after the previous character ;--------------------------------------------------- pusha call WhereXY xor dl, dl @@J1: call Gotoxy dec dh call WhatXY cmp al, 20h je @@J1 inc dh mov bx, dx call GetMaxXY mov dh, bh @@J2: dec dl call Gotoxy call WhatXY cmp al, 20h je @@J2 inc dl call Gotoxy popa ret GotoPrevChar ENDP ;------------------------------------------------------ WriteColoredString PROC ; ; Writes a colored string ; Receives: DS:SI = Pointer to a zero terminated string ; BL = Color attribute (see Irvine16.inc) ; Returns: nothing ;------------------------------------------------------- pusha xor bh, bh @@nextchar: lodsb test al, al jz @@return mov cx, 1 mov ah, 09h int 10h push bx mov ah, 3 int 10h inc dl mov ah, 2 int 10h pop bx jmp @@nextchar @@return: popa ret WriteColoredString ENDP .DATA hello db "Hello world",0 hello1 db "Hello red world",0 hello2 db "Hello blue planet",0 tDecStr db " 2790000000 owns Donald Trump at least, but I only want a thank you",0 fDouble dq 1.23456789 fSingle dd 9.87654321 tString1 db 'first',0 tString2 db 'second',0 buf db 80h DUP(0) tClrscr db "Clrscr: ",0 tDelay db "Delay (10 seconds): ",0 tDumpMem db "DumpMem: ",0 tDumpRegs db "DumpRegs: ",0 tGetCommandtail db "GetCommandtail: ",0 tGetMaxXY db "GetMaxXY: ",0 tGetMseconds db "GetMseconds: ",0 tGotoxy db "Gotoxy: ",0 tIsDigit db "IsDigit: ",0 tRandom32 db "Random32: ",0 tRandomize db "Randomize: ",0 tRandomRange db "RandomRange: ",0 tReadChar db "ReadChar: ",0 tReadFloat db "ReadFloat: ",0 tReadHex db "ReadHex: ",0 tReadInt db "ReadInt: ",0 tReadString db "ReadString: ",0 tSetTextColor db "SetTextColor: ",0 tShowFPUStack db "ShowFPUStack: ",0 tStr_compare db "Str_compare: ",0 tStr_copy db "Str_copy: ",0 tStr_length db "Str_length: ",0 tStr_trim db "Str_trim: ",0 tStr_ucase db "Str_ucase: ",0 tWaitMsg db "WaitMsg: ",0 tWriteBin db "WriteBin: ",0 tWriteBinB db "WriteBinB: ",0 tWriteChar db "WriteChar: ",0 tWriteDec db "WriteDec: ",0 tWriteFloat db "WriteFloat: ",0 tWriteHex db "WriteHex: ",0 tWriteHexB db "WriteHexB: ",0 tWriteInt db "WriteInt: ",0 tWriteString db "WriteString: ",0 END main 中的currentUser: LiveData<User>属性(由UserRepository公开)以更新ProfileViewModel属性。此属性通过数据绑定绑定到UI,并且在发生更改时应更新UI。这是我的假设。

我尝试通过“转换”设置user: User属性,但是没有用。

一些代码? user

DatabaseService.kt

fun getById(documentId: String): MutableLiveData<T> { val resultObj = MutableLiveData<T>() db.collection(className) .document(documentId) .get() .addOnCompleteListener { task -> if (task.isSuccessful) { val obj = task.result?.toObject(modelClass) resultObj.value = obj } else { Log.d(TAG, task.exception?.localizedMessage) } } return resultObj; }

UserRepository.kt

fun getCurrentUser(): LiveData<User> { return this.getById(FirebaseAuthService.userUid) }

ProfileViewModel.kt

// This doesn't work. val userRep = UserRepository(); var user: User = User() init { Transformations.map(userRep.getCurrentUser()) { firebaseUser -> user = firebaseUser; } } 属性绑定到UI。如何使用livedata对象的值更新此属性。我不想观察user: User的片段,并保留对UI组件的引用,并在发生更改的情况下进行更新。

1 个答案:

答案 0 :(得分:0)

当属性user更改时,视图将不会更新,因为它不知道它已更改。尝试使用MutableLiveData<User>

ProfileViewModel.kt

val userRep = UserRepository();
val user = MutableLiveData<User>(User())

init {
    Transformations.map(userRep.getCurrentUser()) { firebaseUser ->
        user.postValue(firebaseUser);
    }
}

这样做,视图将更新,因为LiveData属性中使用的user将通知其更改。