Saving contents of 2 edit text boxes to one file name.

bodycode

Member
Licensed User
Longtime User
Hi. I'm trying to develop an app that needs to load and save the contents of two edit text boxes to one file name. Is that possible?
 

bodycode

Member
Licensed User
Longtime User
Loading and saving to and from file names.

Thanks Margret. Well, not only do I need to save data under unique file names that the user needs to specify but needs to load that file name back into the edit boxes as well, of course, populating the edit text boxes with the data previously saved.

So there'd be a load button, and a save button, which gives the user the opportunity to both load and save data, with full file naming/saving/loading capability, under one button, that would save the contents of the two edt boxes (set to numeric input). That's basically the whole shebang.
 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Best thing to do is save the edittext 'text' to a map using the edittext name as the key then save the map to file. On load it is a simple matter to loop thru the map and load the edittext.
 
Upvote 0

bodycode

Member
Licensed User
Longtime User
I'm basically a very new newbie and don't know what you're really talking about here at all in any way.. Don't know what a "map" is besides it's general meaning in everyday talk, and, don't know how on earth I'd implement loading back anything with a loop. I'd need to see some examples using buttons, loading and saving, etc, sorry about my ignorance but that's where I'm at, if you can point me to some resources or show me an example of load/save two editbox contents to one file with one button, that'd be greatly appreciated.



Best thing to do is save the edittext 'text' to a map using the edittext name as the key then save the map to file. On load it is a simple matter to loop thru the map and load the edittext.
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Something like this should work for you. Be sure to change all the EditText names to whatever you like.

B4X:
Sub Globals
      Dim sets As Map
      sets.Initialize
End Sub

Sub savebtns
      sets.Clear
      sets.Put("EditText1", EditText1.Text)
      sets.Put("EditText2", EditText2.Text)
      File.WriteMap(File.DirInternal, "btnset.set", sets)
End Sub

Sub loadbtns
      sets = File.ReadMap(File.DirInternal, "btnset.set")
      EditText1.Text = sets.Get("EditText1")
      EditText2.Text = sets.Get("EditText2")
End Sub
 
Upvote 0

bodycode

Member
Licensed User
Longtime User
I'm going to give this a try.

Thank you very much.

By the way, does the "load& save code" offer to load & save actual file names? When loading, does the user see a list of files to load? Thanks in advance, as always.

Marsh

 
Upvote 0

bodycode

Member
Licensed User
Longtime User
File name loading and saving

Without a file dialog box offering to save the edit box's values to a user entered file name when hitting a "save" button, this really can't be used. What I was hoping to do, was to have a way for a user to type in values into two text boxes, and then save both textbox contents to one file name, with one save button, which is offered to the user in a file system dialog box. I have a feeling that with some tweaking the code you showed me can be used in some way.

Similarly, when they want to load previously saved edt box values, they simply hit the load button, giving the user the opportunity to pick and choose a previously saved file with a file system dialog box which contains the contents of previously saved edt text boxes.

Also very important, would be to actually display the currently loaded file, as the file name (just one of course), I'd do that in a simple label control.

If that can be accomplished, then this app would go out on the Google market immediately with your name in the credits, of course And anyone else who'd contribute too All the rest of the code's very simple and already written.

I really had to chunk together code from the tutorial. Took me a long time, even trying to figure out the layout code, etc.

By the way, where did you learn your mastery of B4A? I don't see how full mastery could be achieved using just the user manual and tutorial, unless I'm missing something? I'd really love to take a quantum leap in learning this at a higher level.

By the way, does the "load& save code" offer to load & save actual file names? When loading, does the user see a list of files to load? Thanks in advance, as always.

Marsh
 
Last edited:
Upvote 0

lagore

Active Member
Licensed User
Longtime User
What you need is the dialogs library http://www.b4x.com/forum/additional-libraries-official-updates/6776-dialogs-library.html one of the dialogs in the example has a very good example of a file dialog which should be exactly what you need. Not sure about the others but I taught myself VB.net and this is very similar, I am a big fan of trying out all sorts of different options and downloading example programs and working thru them, nothing like getting your hands dirty.
 
Upvote 0

bodycode

Member
Licensed User
Longtime User
I have it.

Thanks. I have the dialogs library. But It seems to be far and away removed from what I'm looking to do. However, I will try to stare at the code and give me an epiphany I might be wrong about that, maybe it's exactly what I need, I'll see. Ok then.

Using the dialog library, I did in fact, already use a numeric input dialog box to populate a variable with a value, but for some reason it ends up crashing my emulator (and my android device) as well once in a while.
I truly do need more docs and more tutorials with careful step-wise examples to step me through the more subtle stuff. so would you say this is an exact replica of the Basic language?




 
Upvote 0

lagore

Active Member
Licensed User
Longtime User
Just a short example of a save and load sub
B4X:
Sub btnSave_Click
   Dim fd As FileDialog
   Dim ret As Int
   fd.FastScroll = True
   fd.FilePath = File.DirRootExternal ' also sets ChosenName to an emtpy string
   fd.ChosenName = "btnset"
   ret = fd.Show("Please Enter File Name" & CRLF & "And Select Save Location", "Save", "Cancel", "",Null)   
   If ret = -1 Then 
      sets.Clear
       sets.Put("EditText1", EditText1.Text)
       sets.Put("EditText2", EditText2.Text)
       File.WriteMap(fd.FilePath, fd.ChosenName & ".set", sets)
   End If
   
End Sub
 
Sub btnLoad_Click
   Dim fd As FileDialog
   Dim ret As Int
   fd.FastScroll = True
   fd.FilePath = File.DirRootExternal ' also sets ChosenName to an emtpy string
   fd.ChosenName = "btnset"
   fd.FileFilter = ".set"
   ret = fd.Show("Please Select File", "Load", "Cancel", "",Null)   
   If ret = -1 Then 
      sets = File.ReadMap(fd.FilePath, fd.ChosenName)
       EditText1.Text = sets.Get("EditText1")
       EditText2.Text = sets.Get("EditText2")
   End If
End Sub
in my activity I have 2 edittexts and a save and load button, when you click save it brings up the first dialog select where you want to save, enter a name then click save. Then if you either clear the edittext, rerun the app or rotate the screen then click load and find the saved file. There is no error trapping in the code it is just an idea of what can be done.
 
Upvote 0

bodycode

Member
Licensed User
Longtime User
sets in sets.clear is in red, undeclared?

You and Margaret really and truly kick ass Thanks so much for your input. This looks to be hittin' it right on the head

I have so much more studying and practicing to do. Argh

Oh and one more thing (I know I know it's always one more thing. heh heh..), how can I actually keep the loaded file name displayed in a label?
 
Last edited:
Upvote 0

lagore

Active Member
Licensed User
Longtime User
In the load_sub above 'fd.ChosenName' is the name of the selected file.
Therefore
B4X:
label1.text = fd.ChosenName
this will put the file name into the the label. If you want to keep this for the next time the app launches then (more Reading) have a look at State Manager for saving settings or if it is just 1 item then you could just save it yourself to another file and check for its existence on activity-create.
 
Upvote 0

bodycode

Member
Licensed User
Longtime User
File Dialog box allows line breaks in file names! Need a solution to that.

So everything works great the way you've suggested things, using the dialogs library. And I'd like to continue using this example, but disallowing a line break in filenames when saving. Is their some kind of setting I can use in the dialog library coding examples to accomplish this?

The file dialog box/button example the dialogs library author shows, allows for a line break! In other words, you type some characters to save something under a file name, and if you hit the return key on the virtual keyboard, it breaks the line, allowing for another line of characters! That is absolutely terrible. Same is true on a real device, which of course, makes the Android OS file system choke rather badly (of course).

I'd need to use a file dialog box to save and load the contents of multiple labels with a single click under one filename without a line return/line break being possible Anything I can do here?

Any help help would be appreciated.

Marsh

 
Upvote 0

klaus

Expert
Licensed User
Longtime User
As a workaround to avoid the line feeds you can add following lines:
B4X:
Dim NewFile As String
NewFile = fd.ChosenName
NewFile = NewFile.Replace(Chr(10), "")
I'd need to use a file dialog box to save and load the contents of multiple labels with a single click under one filename without a line return/line break
I don't understand exactly what you mean with this.

Best regards.
 
Last edited:
Upvote 0

bodycode

Member
Licensed User
Longtime User
Screenshot of two lines in file dialog box!

Here's a screen shot of an illegal 2 line file name in a save file dialog box. I feel that allowing two lines in a file dialog box might be a serious bug in the actual dialog library. None of the number dialog box settings allows this, the same should be true for a file dialog box, right? I'm sending you a screen shot, as well as the code I need to use, it's the code sample the dialog box author used as a tutorial but just slightly modified with other variables. I Don't think it's the code allowing this, I think it's the dialog box itself. Please tell me what you think and what code I can use.

I'm very surprised that this hasn't been mentioned before? Where's my mistake here?

I'd also need to make sure that even if a user loads a file, they don't accidently forget that it's a load file and not save file, and that they might create a 2-line file name as well.

Sub btnSave_Click

Dim fd As FileDialog
Dim ret As Int
fd.FastScroll = True
fd.FilePath = File.DirRootExternal
fd.ChosenName = "btnset"
ret = fd.Show("Enter File name" & CRLF & "AND Select save location", "Save", "Cancel", "", Null)

If ret = -1 Then
sets.Clear
sets.Put("lblNutritLabelWeightOutput", lblNutritLabelWeightOutput.Text)
sets.Put("lblNutritLabelCaloriesOutput", lblNutritLabelCaloriesOutput.Text)
File.WriteMap(fd.FilePath, fd.ChosenName & ".set", sets)
End If

End Sub
 

Attachments

  • dialogbox2lines.JPG
    58 KB · Views: 339
Upvote 0

klaus

Expert
Licensed User
Longtime User
I agree with you that the FileDialog shouldn't allow a line feed.
But as a workaround you can use the code in my previous post that removes any line feed character.

I'm very surprised that this hasn't been mentioned before?
Probably nobody had the idea to put a line feed in a file name.

Best regards.
 
Last edited:
Upvote 0

bodycode

Member
Licensed User
Longtime User
It was an accidental discovery! I was thinking that the return key is almost like the "done" or "enter" key which I thought would finish the input and return me to the program, with the variable values updated by the dialog box. But instead of closing the dialog box, it split the string! Strange.. Anyway, I'll see how I can change the code using your example. How exactly does this work?


 
Upvote 0

bodycode

Member
Licensed User
Longtime User
I realize that But how?

I know that. I wanted to know the technical details as to exactly how the code works.

Thanks for such a quick reply, Klaus.

Is <variable>.Replace and the (Chr(10),"") in documentation? Thanks.


You define a string variable holding the file name, and this line
B4X:
NewFile = NewFile.Replace(Chr(10), "")
removes the line feeds.

Best regards.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…