Android Tutorial ListView tutorial

U

unba1300

Guest
Listview with edittexts instead of labels?

If you want to alter the text of the views you could try to replace the Labels by EditText views. I haven't tried it yet but it should work.

Best regards.

Is that possible? I'd like to present the user with a list of items from a simple array, but enable the user to change those items; then I'll update the array with the user's input, and write to a file. So is there such a thing here as a 'ListEditView'? Thanks.
 

Abubaker

New Member
the second line list view didn work with me beside the image
i use this code

HTML:
Sub Globals
    Dim ListView1 As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
    ListView1.Initialize("ListView1")
    For i = 1 To 300
        ListView1.AddSingleLine("Item #" & i)
    Next
    Activity.AddView(ListView1, 0, 0, 100%x, 100%y)
   Dim Bitmap1 As Bitmap
Bitmap1.Initialize(File.DirAssets, "button.gif")
For i = 1 To 300
    ListView1.AddSingleLine("Item #" & i)
    ListView1.AddTwoLines("Item #" & i, "This is the second line.")
    ListView1.AddTwoLinesAndBitmap("Item #" & i, "This is the second line.", Bitmap1)     
Next
End Sub
Sub ListView1_ItemClick (Position As Int, Value As Object)
    Activity.Title = Value
End Sub
 

MoskmaN

New Member
How can i load an animated gif?
I added the animated gif as resource, but the image remain static.
How can i load a image directly from a url link?


thanx :)
 

moster67

Expert
Licensed User
Longtime User
Abraham has written a library for animated gifs. Take a look in the library section.

Sent from my HTC HD2
 

newSteve

Member
Licensed User
Longtime User
Can you change the text color on individual items of a listview?

Is it possbile to change the text color (or font) of an individual item on a listview?

An example would be a check list with completed items in gray or strikethrough, high priority items in red.
 

GHosaPhat

Member
Having Some Issues

Hi there. I'm having some issues with the ListView (and a couple of other things) that I can't seem to figure out. I've been playing with it :BangHead: but I just don't see why it isn't working.

I'm developing a little game for my wife, and I'm currently just trying to populate a high-score list from a text file. However, when I execute the code, my ListView constantly comes up blank.
B4X:
Sub SetHighScores
   Dim PlayerName As String
   Dim PlayerScore As String
   Dim I As Int
   
   lstHighScores.Initialize("lstHighScores")
   'lstHighScores.Color = Colors.Transparent
   'lstHighScores.Clear
   
   If File.Exists(File.DirInternal, "HighScores.txt") = False Then
      Dim TW As TextWriter

      PlayerScore = "75,000"
      PlayerName = "G_HOSA_PHAT"

      For I = 0 To 9
         TW.Initialize(File.OpenOutput(File.DirInternal, "HighScores.txt", False))
         TW.WriteLine(PlayerScore)
         TW.WriteLine(PlayerName)
         
         lstHighScores.AddTwoLines(PlayerScore, PlayerName)
      Next
      
      TW.Close
   Else
      Dim TR As TextReader
      
      TR.Initialize(File.OpenInput(File.DirInternal, "HighScores.txt"))
      
      For I = 0 To 9
         PlayerScore = TR.ReadLine
         PlayerName = TR.ReadLine
         
         lstHighScores.AddTwoLines(PlayerScore, PlayerName)
      Next
      
      TR.Close
   End If
End Sub
Am I just being an idiot (probably), or what?

EDIT: Yep, I'm an idiot (to some extent). I commented out the Initialize line (because I already have the ListView object defined in my Designer), and tried again. Now I'm showing one line, with only the first entry (maybe because they're all the same?), but at least I've got it populated with something now. I'm sure just a little more work and I can add them all in there.

EDIT 2: Okay, so I deleted the text file and started over. When I ran it this time, it recreated the file and populated the list with 10 copies of the same thing (as it should have). Unfortunately, when I ran it again, I come up with the same results as before. One line in the ListView with text, and 9 blank lines.

So, I added a Select Case on the file creation piece to add an ordinal rank to each entry (1ST PLACE, 2ND PLACE, etc.) and tried again, and it worked this time. So, there we go. I think I've got it worked out myself. :) I still have to work through the actual replacement of entries when a new high score goes in there, but I got the ListView part of my issue worked out. Here's what I ended up with:
B4X:
Sub SetHighScores
   Dim PlayerName As String
   Dim PlayerScore As String
   Dim ScoreTemp As Long
   Dim I As Int
   
   If File.Exists(File.DirInternal, "HighScores.txt") = True Then File.Delete(File.DirInternal, "HighScores.txt")
   
   If File.Exists(File.DirInternal, "HighScores.txt") = False Then
      Dim PlayerRank As String
      Dim TW As TextWriter

      PlayerScore = "75,000"
      PlayerName = "G_HOSA_PHAT"

      TW.Initialize(File.OpenOutput(File.DirInternal, "HighScores.txt", False))
      
      For I = 1 To 10
         Select Case I
            Case 1
               PlayerRank = "1ST PLACE"
            Case 2
               PlayerRank = "2ND PLACE"
            Case 3
               PlayerRank = "3RD PLACE"
            Case 4
               PlayerRank = "4TH PLACE"
            Case 5
               PlayerRank = "5TH PLACE"
            Case 6
               PlayerRank = "6TH PLACE"
            Case 7
               PlayerRank = "7TH PLACE"
            Case 8
               PlayerRank = "8TH PLACE"
            Case 9
               PlayerRank = "9TH PLACE"
            Case 10
               PlayerRank = "10TH PLACE"
         End Select
         
         TW.WriteLine(PlayerRank & " - " & PlayerName)
         TW.WriteLine(PlayerScore)
         
         lstHighScores.AddTwoLines2(PlayerRank & " - " & PlayerName, PlayerScore, I)
      Next
      
      TW.Close
   Else
      Dim TR As TextReader
      
      TR.Initialize(File.OpenInput(File.DirInternal, "HighScores.txt"))
      
      For I = 1 To 10
         PlayerScore = TR.ReadLine
         PlayerName = TR.ReadLine
         
         lstHighScores.AddTwoLines2(PlayerScore, PlayerName, I)
      Next
      
      TR.Close
   End If
End Sub
 
Last edited:

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code is wrong:
B4X:
 For I = 0 To 9
            TW.Initialize(File.OpenOutput(File.DirInternal, "HighScores.txt", False))
            TW.WriteLine(PlayerScore)
            TW.WriteLine(PlayerName)
            
            lstHighScores.AddTwoLines(PlayerScore, PlayerName)
        Next
You are reopening the file and overwriting it every iteration.
A simpler solution:
B4X:
Dim List1 As List
List1.Initialize
 For I = 0 To 9
  List1.Add(PlayerScore)
  List1.Add(PlayerName)
  lstHighScores.AddTwoLines(PlayerScore, PlayerName)
 Next
 File.WriteList(List1, File.DirInternal, "HighScores.txt")
 

Abner

Member
Licensed User
Longtime User
Why won't a ListView list items?

Sub Globals
'These global variables will be redeclared each time the activity is created.
'These variables can only be accessed from this module.

Dim listInnovationOptions As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("actinnovationanddesign")

listInnovationOptions.AddTwoLines("Preliminary Rating Date","00-00-00")
listInnovationOptions.AddTwoLines("Integrated Project Team Meeting Frequency"," ")
listInnovationOptions.AddTwoLines("Integrated Project Team Member"," ")

End Sub

''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''

A bit of my code. The ListView was created in the designer. I gave it a different color as the background, and it shows up as that solid color, but none of these items appear.

Important note: I downloaded an example ListView project posted and opened it in B4A that was said to work by forum post (.../tutorials/listview.zip). It was also blank when I compiled it!

Is this an emulator failure, or B4A itself does not work? Is there an installation update I did not get? Windows 7 64-bit, I installed everything this week.

Oh yeah, I did try this on multiple android emulations. Same issue on them all.


I GOT THE SOLUTION:
The tutorial itself did not bother to set the initialization of the ListView. It does not have a height, label height, or anything else set to a functional value. It even set the 'top' variable to a negative value on the label's initial location, so even if you did succeed in setting your item's overall height, your label hides off the screen (yes, someone bothered to make the default a nonzero value that was disfunctional). Someone at B4A really doesn't want anyone to use ListViews.
 
Last edited:

Abner

Member
Licensed User
Longtime User
well, um, oops!

Could you post your project as a zip file.
It would be much easier to help you because we could test it in the same conditions as you to see what happens.

Best regards.

I'm pretty sure my android emulator isn't up to date completely...and it took me a while to find out how to update it. I will delete my posts if I discover that's been the issue!
:sign0104:

See my above post. Not the issue.
 
Last edited:

Abner

Member
Licensed User
Longtime User
I can't work!

Failed to rename directory C:\Android\android-sdk-windows\tools to C:\Android\android-sdk-windows\temp\ToolPackage.old01.

.....there is nothing using this folder. It won't let me move it. I can't even move it manually. Disabling anti-virus changed nothing. Closing the B4A software did nothing. So I can't update to android SDK tools revision 16. I've killed the explorer process thinking it was holding the folder hostage. Nope.

Also, updating everything else (which let me update) did not solve the issue. The tutorial ListView program still comes up one solid color for me in the emulator.

Any ideas?

P.S. I don't think this matters, but there is other development software on this machine. I made a fresh installation of everything (Android Emu, Java, B4A) that the installation asked for, though. None of the installations for B4A are shared with other software.
P.P.S. The android emu folder is not being indexed. I disabled indexing long ago.

Final edit: See above post. Not the issue.
 
Last edited:

yonson

Active Member
Licensed User
Longtime User
Action when ListView Text is too long

Hopefully someone has a simple solution to this but I've not been able to find anything on the forums so far, basically what can I do when the title of an entry in a list view is too long?

At the moment it just rolls off the end of the screen. I know I can change fonts etc to squeeze more characters on but my issue is that different android phones will have different limits on characters I suppose due to screen size / resolution etc


I assume someone has come across this issue before - any ideas?

Many thanks in advance!
 

Abner

Member
Licensed User
Longtime User
yes sir! Now about my error...

Please don't delete your posts. This makes the whole thread broken.

Okay, I will edit my posts and clarify. Good point. Once again the :sign0104: sign.

A little googling came up with this great link for the android SDK updating it's own folder (sadly, it errors that it can't update because the folder is in use, and itself is using the folder. Devs are so blind!)
http://code.google.com/p/android/issues/detail?id=4410

Now to see if my error is still there, now that everything is 'up to date':

...it is :BangHead: . See my above post about how I finally fixed this.
 
Last edited:
Top