Checkbox Array??? Noobie question

giga

Well-Known Member
Licensed User
Longtime User
I have two checkboxes that write different text into the same file.
I am having a problem when the user checks the boxes one overwrites the other. Because they are in different modules. I would like to add the text to the second line.

Do I need an array to have the text go below each other in the same file?

Thanks for all the help. :)

Sub CheckBoxShowImage_CheckedChange(Checked As Boolean)

myImageView.Initialize("myImageView")
If Checked = True Then
Try
Dim Writer As TextWriter
Writer.Initialize(File.OpenOutput(DirName, FileName1, False))
Writer.WriteLine("This is the first line" & CRLF)
Catch
End Try
Writer.Close

myImageView.Visible = True
Else
myImageView.Visible = False
End If

End Sub

Sub CheckBoxShowImage2_CheckedChange(Checked2 As Boolean)

myImageView.Initialize("myImageView")
If Checked2 = True Then
Try
Dim Writer As TextWriter
Writer.Initialize(File.OpenOutput(DirName, FileName1, False))
Writer.WriteLine("This is the second line" & CRLF)
Catch
End Try
Writer.Close

myImageView.Visible = True
Else
myImageView.Visible = False

End If

End Sub
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Please use [ code ] [ /code ] tags (without spaces) when posting code.

1. I recommend you to remove the Try Catch blocks. They will only hide real problems.
2. This line is most probably wrong: myImageView.Initialize("myImageView")
You should not initialize views created with the designer. Instead you should right click on the view and choose: Dim myImageView As Image

3. WriteLine adds an end of line character. You do not need to add it yourself.

4. If I understand the problem correctly you should set the EventName of both checkboxes to the same value and then have one sub similar to:
B4X:
Sub CheckBoxShowImage_CheckedChange(Checked As Boolean)
 Dim lines As List
 lines.Initialize
 If CheckBox1.Checked Then lines.Add("First line")
 If CheckBox2.Checked Then lines.Add("Second line")
 File.WriteList(...)
End Sub
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
Please use [ code ] [ /code ] tags (without spaces) when posting code.

1. I recommend you to remove the Try Catch blocks. They will only hide real problems.
2. This line is most probably wrong: myImageView.Initialize("myImageView")
You should not initialize views created with the designer. Instead you should right click on the view and choose: Dim myImageView As Image

3. WriteLine adds an end of line character. You do not need to add it yourself.

4. If I understand the problem correctly you should set the EventName of both checkboxes to the same value and then have one sub similar to:
B4X:
Sub CheckBoxShowImage_CheckedChange(Checked As Boolean)
 Dim lines As List
 lines.Initialize
 If CheckBox1.Checked Then lines.Add("First line")
 If CheckBox2.Checked Then lines.Add("Second line")
 File.WriteList(...)
End Sub

:signOops: Sorry Erel, about the code. Thanks for the response. I put the Try Catch in per the forum because I keep getting a java.null.exception if the file has been created and the app sees it in the directory when I start it. if I manually remove the file the java.null.exception goe away.

I will try your suggestion and let you know.:)
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
Erel,

the code you recommended makes complete sense however, I must be doing something wrong. I am not sure if the the CheckedChange is firing or I wrote the code wrong.:sign0148:

B4X:
Sub CheckBoxShowImage_CheckedChange(Checked As Boolean)

Dim Writelist As TextWriter

If CheckBox1.Checked Then lines.Add("This is line one")
If CheckBox2.Checked Then lines.Add("This is line two")
If CheckBox3.Checked Then lines.Add("This is line three")
If CheckBox4.Checked Then lines.Add("This is line four")
If CheckBox5.Checked Then lines.Add("This is line five")
If CheckBox6.Checked Then lines.Add("This is line six")

File.Writelist(File.DirRootExternal & "/logs", "log.txt", lines)
Writelist.Close

End Sub
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
1) where do you initialize the 'lines' list? Check the first two lines of Erel's code again :)
2) No need for a writeList textWriter object, you don't use it somewhere.
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
1) where do you initialize the 'lines' list? Check the first two lines of Erel's code again :)
2) No need for a writeList textWriter object, you don't use it somewhere.

Thanks MC73, I initialized lines in sub global. I will remove the textwriter object and see what happens.
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
I tried it this way and still does not create a file. I am using the event name CheckBox1,2,3,4,5,6 to fire off each line. :( I must be missing something.

Dim lines As List
and
lines.initialize are placed in Sub Global

Thanks mc73

B4X:
Sub CheckBoxShowImage_CheckedChange(Checked As Boolean)

If CheckBox1.Checked Then lines.Add("This is line one")
If CheckBox2.Checked Then lines.Add("This is line two")
If CheckBox3.Checked Then lines.Add("This is line three")
If CheckBox4.Checked Then lines.Add("This is line four")
If CheckBox5.Checked Then lines.Add("This is line five")
If CheckBox6.Checked Then lines.Add("This is line six")
File.Writelist(File.DirRootExternal & "/logfiles", "log", lines)


End Sub
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
How do you see that the file was not created?

It is better to write:
B4X:
File.Writelist(File.DirRootExternal, "logiles/log", lines)
You should also make sure that the folder already exists (or create it with File.MakeDir).

I tried it with(File.DirRootExternal, "logiles/log", lines) and still no file created. The folder is there because I have other files in it.

I am able to view the sdcard through Dalvik Explorer.

I even tried through the stream
File.WriteList(File.DirRootExternal, FileName1, lines)

No Luck. :sign0013: to be a bother. I can't tell if the checkbox is even firing.
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
I tried it with(File.DirRootExternal, "logiles/log", lines) and still no file created. The folder is there because I have other files in it.

I am able to view the sdcard through Dalvik Explorer.

I even tried through the stream
File.WriteList(File.DirRootExternal, FileName1, lines)

No Luck. :sign0013: to be a bother. I can't tell if the checkbox is even firing.

:sign0163: have any idea why a file won't create off a checkbox1.checked eventname.

Thanks Again
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
Have you tried to read the file after you save it? Do you get any errors?

Some file explorers only show new files after they are scanned by the media scanner service.
Erel,
Thanks for the reply.
I dont get a file at all. When I did it the other way on a single line it created a file and I was able to read it.

When I went to checkbox1.checked for multiples it does nothing.

I will zip the project and upload it

Thanks,:sign0188:
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
Can't you post your project as a zip file (IDE menu Files / Export As Zip) ?
How do you define the CheckBoxes and how ?

Best regards.

Thanks Klaus for you assistance, Here os the zip file for the checkboxes.:sign0188:
 

Attachments

  • checkboxes.zip
    7.1 KB · Views: 298
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
I have to suppose that you used the designer in order to build these checkboxes. If this is the case, go to designer select one-by-one you checkboxes and alter the property 'eventName' to CheckBoxShowImage .
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
I have to suppose that you used the designer in order to build these checkboxes. If this is the case, go to designer select one-by-one you checkboxes and alter the property 'eventName' to CheckBoxShowImage .

Thanks Mc73, :)

I will try it this way and see if that works. Will let you know.

Should they ALL be the same CheckBoxShowImage not CheckBoxShowImage1,2,3,4,5,6 ?
 
Upvote 0

giga

Well-Known Member
Licensed User
Longtime User
I have to suppose that you used the designer in order to build these checkboxes. If this is the case, go to designer select one-by-one you checkboxes and alter the property 'eventName' to CheckBoxShowImage .

:icon_clap: I bow to you MC73 THANK YOU :icon_clap:
Changing the event name to CheckBoxShowImage did the trick.

However If I close the app and re-open it I get a Java.null.exception. This happens when the app see the file in the directory.

**Example**

If I delete the file the checkboxes created and open the app the Java.null.exception goes away.

If I use the checkboxes again and create the file again the Java.null.exception comes back again when loading the app.
 
Upvote 0
Top