Help with Adding text into a list

sarim123

Member
Licensed User
Longtime User
(My First Post:sign0060:)I for a long time i have been searching on how to add text from a EditText box to a list. All i am trying to do is put text into a textbox and when Submit is pressed it puts it in a list. I also want to make sure the text saves when the app is closed. I have tried this tutorial but i am getting nowhere:( Thanks for all your help!!!!!!!!!
 

klaus

Expert
Licensed User
Longtime User
Something like this:
B4X:
Sub Process_Globals
    Dim lstText As List
End Sub

Sub Globals
    Dim edtText As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    If FirstTime = True Then
        If File.Exist(File.DirInternal, "Text.txt") = True Then
            lstText = File.ReadList(File.DirInternal, "Text.txt")
        Else
            lstText.Initialize
        End If
    End If
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    File.WriteList(File.DirInternal, "Text.txt", lstText)
End Sub

Sub btnSubmit_Click
    lstText.Add(edtText.Text)
End Sub
You need to add the EditText view to the activity either in a layout file or by code.

Best regards.
 
Upvote 0

sarim123

Member
Licensed User
Longtime User
Thanks sooo much for helping me but im getting this error

Compiling code. Error
Error compiling program.
Error description: Unknown member: exist
Occurred on line: 11
If File.Exist(File.DirInternal, "Text.txt") = True Then
Word: exist
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
B4X:
If File.Exist(File.DirInternal, "Text.txt") = True Then
Should be:
B4X:
If File.Exists(File.DirInternal, "Text.txt") = True Then
The letter s is missing.
 
Upvote 0

sarim123

Member
Licensed User
Longtime User
i get a black screen on the emulator

oops spoke a little too soon. When i compile the apk, i get a black screen on the emulator. Is something not initialized or what, what am i doing wrong???
I put in the edit text through the designer, Can someone please help...sorry im a noobie.
 
Upvote 0

sarim123

Member
Licensed User
Longtime User
got the layout to come up but the code isnt working

The code looks right but what am i doing wrong here? I have added everything you guys told me too but when i click the button to put the the text into a list it doesnt work. It doesnt do anything. I tried to mess with the code but that didnt do anything. Heres the code and an attached file.

B4X:
Sub Process_Globals
    Dim lstText As List
End Sub

Sub Globals
    Dim edtText As EditText
   Dim EditText1 As EditText
End Sub

Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Main.bal")
   
   If FirstTime = True Then
        If File.Exists(File.DirInternal, "Text.txt") = True Then
            lstText = File.ReadList(File.DirInternal, "Text.txt")
        Else
            lstText.Initialize
        End If
    End If
End Sub

Sub Activity_Resume
End Sub

Sub Activity_Pause (UserClosed As Boolean)
    File.WriteList(File.DirInternal, "Text.txt", lstText)
End Sub

Sub btnSubmit_Click
   edtText.Initialize("edtText")
    lstText.Add(edtText.Text)
End Sub


Thanks guys!!!
 

Attachments

  • edittext.zip
    6.4 KB · Views: 429
Upvote 0

klaus

Expert
Licensed User
Longtime User
You must remove edtText.Initialize("edtText") !
Why did you add this line.
We didn't show it.
Views added with a layout file are automaticaly initialized.
You need to initialize views only when you add them in the code.

Best regards.
 
Upvote 0

sarim123

Member
Licensed User
Longtime User
still doesnt

Even if you remove it, it still doesn't work. Maybe the list should be on a different Activity, but im not sure.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
What do you mean it doesn't work. I tested it before posting the test program.
What exactly do you expect ?
The List is updated but not visible.
You need to define what you understand with 'list'
In Basic4Android a List is an object holding other objects but nothing is visible.
If you want to see something on the screen you need to fill either a ListView or s Spinner.
You were asking for a list so you got an answer for a List.
Attached a test program with a ListView showing the content of the list.

Best regards.
 

Attachments

  • edittext1.zip
    6.6 KB · Views: 371
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Actually your code works after you remove the initialization of the edit text. If you want to check it out put a message box where I show it below. Run the app, add something in the edit box, exit and get back and do it a few times. You will see that every time it displays the list, it has an additional item.
B4X:
Sub btnSubmit_Click
    lstText.Add(edtText.Text)
    Msgbox(lstText,"The list")
End Sub

I also checked the existence of text.txt file by storing it in the root of my SD card. I changed all DirInternal to DirRootExternal.
 
Upvote 0

sarim123

Member
Licensed User
Longtime User
Thanks soo much Guys..

Thanks soo much Guys..Your the best. Klaus you were right all i had to do is make the list visible. Im sorry it was a mistake on my part.
Sorry but this is my last question, how would i make the texts start at the top. Right now they are coming from the bottem up.



:sign0098:Thanks i really appreciate it. :sign0098:
 
Upvote 0
Top