List not behaving

Jamie

Member
Licensed User
Longtime User
Hi. I have this code that is driving me crazy.
B4X:
Dim temp As String
      temp = cells(4) 'hold value
      cells(4)="" 'remove this value ... shouldn't go in newlist
      newlist.add(cells) 
      Log(cells(0)&cells(1)&cells(2)&cells(3)&cells(4)) ' "" is in cell(4)
      cells(4) = temp 'replace original value ' if this line isn't commented out the value will end up in newlist
      Log(cells(0)&cells(1)&cells(2)&cells(3)&cells(4))

As the comments show if I cell(4) = temp "after" newlist.add(cells) the value I was holding in temp ends up in newlist.
If I comment out cell(4) = temp then it doesn't go into newlist.add(cells)
My guess is that the code completes in the sub and newlist.add(cells) happens after the sub completes, so the cells(4) has been refilled with the value before newlist is written.
Have I got this right because it's driving me crazy............. :BangHead:
 

Jamie

Member
Licensed User
Longtime User
The code is part of a bigger loop that runs through the list matching items with a listview and adding 1 line, that's a list, at a time if it finds a match. It works fine, except that if I make a change after it should write to the newlist it doesn't work.
I'll try your idea and see if it works
Thanks
 
Upvote 0

Jamie

Member
Licensed User
Longtime User
Tried your idea but got this
java.lang.ClassCastException: java.lang.String[] cannot be cast to java.util.List
 
Upvote 0

Jamie

Member
Licensed User
Longtime User
I even tried this with the same results
B4X:
Dim temp As String
      temp = cells(4) 'hold value
      cells(4)="" 'remove value so saved newlist won't have it...
      writelist(cells)
      'newlist.add(cells)
      Log(cells(0)&cells(1)&cells(2)&cells(3)&cells(4))
      cells(4) = temp 'replace value
      Log(cells(0)&cells(1)&cells(2)&cells(3)&cells(4))
and added
B4X:
Sub writelist(value As Object)
newlist.add(value)
End Sub

I can see by the log that the values are changing correctly but when looking at newlist the cells(4) is the value that was replaced with the original, which is wrong.
 
Upvote 0

Jamie

Member
Licensed User
Longtime User
Can you upload your code as a small project (File - Export as zip)?

There are a number of extra files needed to make the program work. I can't share them as they are internal company files. I'll write a small program and try to recreate the problem. If that doesn't work, I'll make some dummy files and upload the whole mess.
Thanks
Jamie...
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Hi. I have this code that is driving me crazy.
B4X:
Dim temp As String
      temp = cells(4) 'hold value
      cells(4)="" 'remove this value ... shouldn't go in newlist
      newlist.add(cells) 
      Log(cells(0)&cells(1)&cells(2)&cells(3)&cells(4)) ' "" is in cell(4)
      cells(4) = temp 'replace original value ' if this line isn't commented out the value will end up in newlist
      Log(cells(0)&cells(1)&cells(2)&cells(3)&cells(4))

What I was saying in the other post. When you log your files you select an element of the array to log. When you are adding to the list in your above code you make no reference to the element. You have:

B4X:
newlist.add(cells)

you should have:

B4X:
newlist.add(cells(element_selection_here))
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
There is no problem in reality with this sub, I hope I can explain and if I err, surely other guys here may as well correct me.
You populate your list with objects. The array cells() is just an object here. So, when you want to read its contents, by getting it from the list, your output will be the most recent values set in your array.
Now, I am sure there are elegant ways of storing these variables in the list and getting them back, even though I would choose to use a 2d array for this purpose.

Margret suggested another thing, if you really want to populate the list with separated items. The code below works ok.
B4X:
Dim lst As List 
lst.Initialize
Dim cells(5) As String 
For i=0 To 4:cells(i)=i:Next
Dim temp As String
temp=cells(4)
cells(4)=""
lst.AddAll (cells)
cells(4)=temp
Dim ret As String
ret=""
For i=0 To lst.Size -1:ret=ret & lst.Get (i) & CRLF:Next
Msgbox(ret,"ok")
 
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
He said in post 3 that this is part of a larger loop. In your code you have .addall which works if you dont pick an element. His fist post has .add without selecting an element. It would really help if he posted the code of the complete loop. If he uses .add, should he not select the element and if .addall the element is not needed.
 
Upvote 0

Jamie

Member
Licensed User
Longtime User
Here it is

Rename the file book.zip to book.csv, place in root of device. The other file is a program that duplicates my problem.

Tap an item in the top listview. Accepting Save at the popup will transfer the data to newlist and the bottom listview will be populated with part of the information.
Now Press SAVE and type a name for the new file.
As you can see by the log both newlist and cells are behaving as expected.
Now look at the file you saved. You will see that the information removed from cells(4) has made it into the file,
cell2 was just added to see what was happening in newlist before and after changing values

Also notice in the log 4-cell2 that the value changed from 2-cell2 even though it wasn't set after cell2 = newlist.Get(newlist.Size-1)

Maybe this is the way things are supposed to work, but they certainly are confusing me.

I did find a work around, I don't put temp back into cell(4) so it saves right then I just reload the book.csv file so I can still find out what cell(4) value is.

Thanks for all the interest
 

Attachments

  • testlist.zip
    7.4 KB · Views: 325
  • Book.zip
    269 bytes · Views: 318
Last edited:
Upvote 0

margret

Well-Known Member
Licensed User
Longtime User
Hello, I have looked at your app. This is a location of error. I need to understand what you are trying to do with this line:

B4X:
cell2 = newlist.Get(newlist.Size-1)

You have Dimmed cell2() as String. You are trying to place: newlist.Get(newlist.Size-1) into this array. This will work if you give the array an element slot to go into or you use Regex.Split(",", newlist.Get(newlist.Size-1)) to split it into the array.

So, again in detail, what do you want to end up in cell2?
 
Upvote 0

Jamie

Member
Licensed User
Longtime User
Nothing, I only put it there to see the contents of newlist in the log. I'm not sure what you mean. It takes the last line of newlist which was added just before, and which was created when you tapped a line in listview1, and then in the log you can see what newlist contains by looking at cell2(0), cell2(1), etc. It seems to work fine, but again it's not even in the actual program, just for info in this small chunk of code.
Hope that explains it.
:)
 
Upvote 0

Jamie

Member
Licensed User
Longtime User
It throws an exception error on mine and it should. What version of b4a are you using?

2.02
That explains why when I put it in there I had a couple of FATAL errors. But it isn't really needed anyway. The file created shows what is going on in newlist.
 
Upvote 0

Jamie

Member
Licensed User
Longtime User
Ok, when you select from the list and select yes to save it, what should happen to the main list at the top of the screen. Anything, or should it just copy the selected entry to your new file?

Nothing happens to the list at the top. It's a master product list containing hundreds of entries, I cut almost all the items out because it contains private info, eg my cost for products.
So you tap an line in the top listview and the popup comes up and you click save. That puts some of the info into the bottom list and adds a line to newlist, but not the cost. The bottom list is a tailored price list for customers.
So after putting an item or many items into the bottom list, you enter a new file name into the edittextbox and click the save button. A new file is created on the root of your device, which can then be emailed to the customer but also will be imported to the companies main program and merged into the access database.
 
Upvote 0

Jamie

Member
Licensed User
Longtime User
Update

Here is the program again with only the essentials. You need the book.zip file from earlier post and rename it book.csv

Am I DIMming temp wrong or filling the newlist list or cells() wrong.
I do get a Fatal Signal Error when using it sometimes so obviously something is wrong.

Tap item in top list
Select OK to add to bottom list
enter new file name in edittext box and save

New File saved on device will have the value of cell(4) at the end of the line, which shouldn't be there because it was replaced by cells(4)=""

I'm guessing now, from the previous comments, that I'm using my variables and objects wrong.
Hope someone can steer me straight.
Thanks
Jamie...
 

Attachments

  • testlist2.zip
    7.3 KB · Views: 264
Upvote 0

Jamie

Member
Licensed User
Longtime User
Thanks for helping out Margret.
Your program was very informative.
The best solution was splitting the list like you suggested,
cells=Regex.Split(",", newlist.Get(i))
It took a bit of work to go through the program and fix all the spots where I had just dumped cells as a whole into lists and listviews, that was what was causing all the grief.
The listview3 entries were also editable so I could change the selling price so I couldn't use your string manipulation as it was.

I also used your method for setting the return value to exactly the way I wanted my layout
Thanks
Jamie
 
Upvote 0
Top