Android Question Arrays and my Metal block!

aidymp

Well-Known Member
Licensed User
Longtime User
Hi just a little back ground, but as a child I had an illness and I forgot a lot of stuff, it also seems to hinder me today as I cannot grasp some principles!

I can use B4A and J just fine, BUT I do everything the simple way!

Im trying to rewrite my code, but cannot work out certain things due to this block!

Arrays! I understand the basics but cant visualise them making them hard for me to use, so i don't use them! and this is my major problem!

At present I hard code everything, meaning my programs work but are hard to change! a simple change like adding something in the middle of a list requires me to do a huge amount of work!

The basic idea is based on something like lets say playstore

So I have :

a url of my package
a name of my package
a screen shot of my package
a few more bits of info about my package!

currently I have all this hard coded in the app! i understand how I would do ONE! via a settings file or xml variant with tags. but I dont know how I could read a list of more than one and put all the info in the right place! (not that i dont know, more I cannot understand or get it to lock in my head)

So I am asking if someone could show me how to do this!?

file sample i would like to use

<url>http://myapp.com/app1</url>
<name>"app name"</name>
<imageurl>http://myapp.com/app1</imageurl>
<counter>1020</counter>
<someotherinfo>blah blah</someotherinfo>

now i could read this easy!

but if i added another record how would i read that?

<url>http://myapp.com/app2</url>
<name>"app name"</name>
<imageurl>http://myapp.com/app2</imageurl>
<counter>145020</counter>
<someotherinfo>blah blah</someotherinfo>

<url>http://myapp.com/app3</url>
<name>"app name"</name>
<imageurl>http://myapp.com/app3</imageurl>
<counter>1023420</counter>
<someotherinfo>blah blah</someotherinfo>



currently i add each item to a list, all in code, then i have a lot of code to determine the current item in the list. so if item number 10 is selected, I would have something like

B4X:
Case 10
            url="http://myapp.com/app1"

and so on, but as you can imagine (my list only has 40 items) and i like it in alphabetical order, So if i need to add something in the middle I have a lot of code to change.

Im not asking for you to write it, rather explain in some alternative way that my brain will absorb, or point to various examples that i can look at and try to understand.

Currently the code I am using to add to the list is about 10 lines but repeated 40 times! the code i use when the list is selected is also about 8 lines but also repeated about 40 times!

Anyone able to help at all! I have read and read many books on basic but as I say the mental block, will not let me visualise arrays.

If anyone can help i would be very grateful!

Thanks

Aidy
 

LucaMs

Expert
Licensed User
Longtime User
Sorry, I read half post (I have already sufficiently stoned :D).

To me it seems a "problem" of data Records (Url, Name, ImageUrl, etc. can be Fields of a Record in a SQLite Table), then you could use a SQLite DB.

Another way could be:

1) create a custom type:
B4X:
Type tAppData(Url As String, Name As String, ...)

then:
B4X:
Dim AppData As tAppData
AppData.Initialize
AppData.Url = ...
AppData.Name = ...

Add that AppData to a Map (you should study Maps, they are more useful than Arrays), named, for example, mapAppData, and write / read that Map.

...
...
Dim AppData As tAppData = mapAppData.Get(0)


------


You could save that AppData (of type tAppData) also in a KeyValueStore file (search fo it).
In this case, you could cut Name from the Type declaration and use it as Key in the KVS file.
 
Last edited:
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
i do not understand correctly.
Can you describe it more clearly what exactly you want archieve?
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
i do not understand correctly.
Can you describe it more clearly what exactly you want archieve?

Basically as I cant visualise arrays in my head like I can with most of the other programming commands, functions etc. (which i believe is the key to this)

I want to have a textfile, xml or any file that contains a list of 4-5 items that i can edit and add to at will. I can do it all myself but not using arrays as my head card seem to take the logic!! (im not normal ;) )

so currently my code is about 400 lines to add 40 items of 10 to a list and 400 lines of code to do something with the list! where i know I could do both in possibly 20 lines with arrays!

so list

<url>http://myapp.com/app1</url>
<name>"app name"</name>
<imageurl>http://myapp.com/app1</imageurl>
<counter>1020</counter>
<someotherinfo>blah blah</someotherinfo>

and so on....

I define each of those as separate variables, and add to the list 40 times!!! in code.

Thanks

Aidy
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Sorry, I read half post (I have already sufficiently stoned :D).

To me it seems a "problem" of data Records (Url, Name, ImageUrl, etc. can be Fields of a Record in a SQLite Table), then you could use a SQLite DB.

Another way could be:

1) create a custom type:
B4X:
Type tAppData(Url As String, Name As String, ...)

then:
B4X:
Dim AppData As tAppData
AppData.Initialize
AppData.Url = ...
AppData.Name = ...

Add that AppData to a Map (you should study Maps, they are more useful than Arrays), named, for example, mapAppData, and write / read that Map.

...
...
Dim AppData As tAppData = mapAppData.Get(0)


------


You could save that AppData (of type tAppData) also in a KeyValueStore file (search fo it).
In this case, you could cut Name from the Type declaration and use it as Key in the KVS file.


I saw maps but can they only hold 2 pieces of data? i need 5 or 6 for each or do you mean a separate map for each! (That actually makes more sense!)

Thanks
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I saw maps but can they only hold 2 pieces of data? i need 5 or 6 for each or do you mean a separate map for each! (That actually makes more sense!)

Please, read better my previous post.

A Map has only one "piece of data", the first one is the Key of the Map. But you can put any type of data in its "piece of data".

mapAppData.Put("NameOfMyApp1", AppData1) ' <---- where AppData1 is of type tAppData (a "record" composed with Url, ImageUrl...)
mapAppData.Put("NameOfMyApp2", AppData2)
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
Maybe a combination of LIST and MAP is what you are searching.

A Map can hold any number of objects.
Talking about results from a database you can put all fields into one map.
If you need more "items" then you can use a list with x maps

Seeing your post i think you want to hold/store/reload a list of maps and each map will contain
a
<url>http://myapp.com/app1</url>
, a app name
<name>"app name"</name>
, a url to an image
<imageurl>http://myapp.com/app1</imageurl>
, a counter (what ever this counter is :))
<counter>1020</counter>
and someotheronfo
<someotherinfo>b

Right?
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Maybe a combination of LIST and MAP is what you are searching.

A Map can hold any number of objects.
Talking about results from a database you can put all fields into one map.
If you need more "items" then you can use a list with x maps

Seeing your post i think you want to hold/store/reload a list of maps and each map will contain
a
<url>http://myapp.com/app1</url>
, a app name
<name>"app name"</name>
, a url to an image
<imageurl>http://myapp.com/app1</imageurl>
, a counter (what ever this counter is :))
<counter>1020</counter>
and someotheronfo
<someotherinfo>b

Right?

yes correct!

a little more background i have the same app on B4J and B4A so if someone says can you add my app and its called "jack-app" i have to edit 2 apps, add the record in what ever place "j" for "jack-app" would fit, and and then renumber all the case 3, case 4, case 5 right upto 40 or something in both apps, so i want either a list, db (i do have all this info in a database) but dont want to add logins to the app.)

so one i was expecting i could just do 1 list, but like i said for me arrays are very hard to visualize.

Thanks
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Please, read better my previous post.

A Map has only one "piece of data", the first one is the Key of the Map. But you can put any type of data in its "piece of data".

mapAppData.Put("NameOfMyApp1", AppData1) ' <---- where AppData1 is of type tAppData (a "record" composed with Url, ImageUrl...)
mapAppData.Put("NameOfMyApp2", AppData2)

I think i get it, AppData1 actually contains url, image and other info (in an Array! argg! lol) i will try hard, but its a limitation on my brain lol!

Thanks

Aidy
 
Upvote 0

DonManfred

Expert
Licensed User
Longtime User
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then
        kvs.Initialize(File.DirDefaultExternal, "datastore")
    End If
   
   
    'put a "simple" value
    'kvs.PutSimple("time", DateTime.Now)
   
    Dim mylist As List
    mylist.Initialize
   
    Dim m As Map
    m.Initialize
    m.Put("url","http://myapp.com/app1")
    m.Put("name","app name")
    m.put("imageurl","http://myapp.com/app1")
    m.Put("counter",1020)
    m.Put("someotherinfo","Bla bla bla")
    ' Add the map (and all data in it) to the list...
    mylist.Add(m)
   
    Dim m As Map
    m.Initialize
    m.Put("url","http://myapp.com/app2")
    m.Put("name","app name 2")
    m.put("imageurl","http://myapp.com/app2")
    m.Put("counter",2040)
    m.Put("someotherinfo","YO!")
    ' Add the map (and all data in it) to the list...
    mylist.Add(m)
   
    ' write the list to the KVS
    kvs.PutObject("mylist",mylist)
   
    ' now we to rea the list (and their maps)
    '
    Dim newlist As List = kvs.GetObject("mylist")
    For i = 0 To newlist.Size-1
        Dim metadata As Map = newlist.Get(i)
        Log("List-Item # "&i)
        Log("URL="&metadata.Get("url"))
        Log("Name="&metadata.Get("name"))
        Log("ImageURL="&metadata.Get("imageurl"))
        Log("Counter="&metadata.Get("counter"))
        Log("Some other info="&metadata.Get("someotherinfo"))
    Next

End Sub
 

Attachments

  • KVS-Ex.zip
    12.8 KB · Views: 175
Upvote 0

klaus

Expert
Licensed User
Longtime User
... but for many people this is still a difficult topic.
That's what I thought before I began to learn SQL.
At the end, I found it much easier than what I supposed it to be.
Of course you need to learn the basics, but for simple databases it is easy.
 
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
Thank you both, I can visualise this! am I right in thinking, that i can have a private app that i create the datastore with. then upload the datastore to a server, and then download and use the data in both B4J and B4A?

If so this is ideal

Thanks

Aidy
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
You don't need a separate application to generate the database you can create it within the B4A / B4J applications.
You may have a look at chapter 5 SQLite Database in the B4A User's Guide and the code examples.
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
Upvote 0

aidymp

Well-Known Member
Licensed User
Longtime User
You don't need a separate application to generate the database you can create it within the B4A / B4J applications.
You may have a look at chapter 5 SQLite Database in the B4A User's Guide and the code examples.

Hi I know I don't need to have 2 applications but the aim of the exercise is to make adding something to my app easy. currently if someone says "can you list my app in your directory please" I have to edit 2 programs, compile them and then get everyone to download the new version! it was OK initially, but now the app is gaining followers, i'm having to do updates to 2 apps daily. and its getting very time consuming! where I just want a list/or something that I can update and the app reads this updated info.

Like I said it isn't hard for normal programmers, but i'm a little disadvantaged in certain mental area's!

Thanks

Aidy
 
Upvote 0
Top