1. *** New version of B4J is available ***
    B4J v7.8
    Dismiss Notice

Android Question Set Data into Object

Discussion in 'Android Questions' started by fishwolf, Jun 9, 2019.

  1. fishwolf

    fishwolf Active Member Licensed User

    How to can set single data into a Object?

    Example set all data into object
    Code:
    Dim TableList As List

      TableList.Initialize
      TableList.Add(
    Array(Ind+1, ResultMap.Get("name") )
    Flow for set single data into object
    Code:
    Dim TableList As List
    Dim TableRow as ????

      TableList.Initialize

      TableRow???? = Ind+
    1
      TableRow???   = ResultMap.Get(
    "name")

      TableList.Add(TableRow)
     
  2. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    Dim row() As Object = TableRow.Get(5'update the sixth item
    row(0) = 111
    row(
    1) = "asdasd"
     
  3. fishwolf

    fishwolf Active Member Licensed User

    Set the all list with the last value

    i'm tring to set the b4xTable from Map derived from a json

    Code:
    Sub ShowResults (DataMap As Map)
    Dim ResultMap As Map
    Dim ResultsList As List
    Dim TableRow(2As Object
    Dim Ind As Int
    Dim TableList As List

      
        ResultsList = DataMap.Get(
    "results")
        
    Log ("ResultsList=" & ResultsList)
     
        TableList.Initialize
              
        
    For Ind = 0 To ResultsList.Size - 1
            ResultMap = ResultsList.Get(Ind)
            
            TableRow(
    0) = Ind + 1
            TableRow(
    1) = ResultMap.Get("name")
          
            TableList.Add(TableRow)
      
        
    Next

        TableResults.SetData(TableList)
      
    End Sub
    TableList=(ArrayList) [[Ljava.lang.Object;@1634124, [Ljava.lang.Object;@1634124, [Ljava.lang.Object;@1634124, [Ljava.lang.Object;@1634124, [Ljava.lang.Object;@1634124, [Ljava.lang.Object;@1634124, [Ljava.lang.Object;@1634124, [Ljava.lang.Object;@1634124, [Ljava.lang.Object;@1634124, [Ljava.lang.Object;@1634124, [Ljava.lang.Object;@1634124, [Ljava.lang.Object;@1634124]
     
    Last edited: Jun 10, 2019
  4. Erel

    Erel Administrator Staff Member Licensed User

    What happens after you call SetData?
     
  5. fishwolf

    fishwolf Active Member Licensed User

    The table show all rows with the last value of ResultsList

    ResultsList contain 12 object, the first column for all rows is set to "12"
     
  6. rosippc64a

    rosippc64a Active Member Licensed User

    Code:
    For Ind = 0 To ResultsList.Size - 1
            
    dim ResultMap as Map = ResultsList.Get(Ind)
            
    Dim TableRow(2As Object
            TableRow(
    0) = Ind + 1
            TableRow(
    1) = ResultMap.Get("name")
         
            TableList.Add(TableRow)
     
        
    Next
     
    Erel likes this.
  7. Erel

    Erel Administrator Staff Member Licensed User

  8. fishwolf

    fishwolf Active Member Licensed User

    Thank you.

    i understand that the problem is that TableRow is add to list for reference, if you define olny once the variable, the reference is always the same.
    If define the variable into the loop, the reference of variale is always different.

    Code:
    ' Wrong
    Dim TableRow(2As Object 
    For Ind = 0 To ResultsList.Size - 1
            
    dim ResultMap as Map = ResultsList.Get(Ind)
            
    ' Correct
            Dim TableRow(2As Object
            TableRow(
    0) = Ind + 1
            TableRow(
    1) = ResultMap.Get("name")
         
            TableList.Add(TableRow)
     
        
    Next
     
  9. Erel

    Erel Administrator Staff Member Licensed User

    Code:
    'even better:
    For Ind = 0 To ResultsList.Size - 1
            
    dim ResultMap as Map = ResultsList.Get(Ind)
            TableList.Add(
    Array(Ind + 1, ResultMap.Get("name"))
        
    Next
     
Loading...
  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice