How to add items to ListView1:jListView? lazarus / 43-Android ---------------------------------------------------------------------- Bandy · 2025-08-08 08:13 Hello. I want code to add items to ListView1:jListView and show them in reversed order, meaning the last added item to be displayed on top of (or first on) the list? How can I do it? ---------------------------------------------------------------------- Bandy · 2025-08-10 09:14 Please guys... anyone can help? ---------------------------------------------------------------------- cdbc · 2025-08-10 10:06 Hi Look at the examples, demoes & the source-code provided and think a little bit... Don't be so helpless. Regards Benny ---------------------------------------------------------------------- Bandy · 2025-08-10 11:40 I'm trying to add items to ListView1 from a text file when the app starts (OnCreate event) with the following code: procedure TAndroidModule1.AndroidModule1Create(Sender: TObject); var f:TextFile;     a:string; begin AssignFile(f, '/Spațiu de stocare intern/Fisiere/Raspunsuri.txt'); Reset(f); while not(eof(f)) do   begin         with ListView1.Items do           begin             readln(f,a);             Add(a);             readln(f,a);             Add(a);             readln(f,a);             Add(a);           end;         repeat          readln(f,a);         until a=' ';   end; CloseFile(f); end; /Spațiu de stocare intern/Fisiere/Raspunsuri.txt translates from romanian language to english language to /Internal Storage/Files/Answers.txt. This is where my text file is located. My smartphone has Android 13 in romanian language. The app starts, shows the Android icon and the exits, doesn't do anything. What's wrong, please? ---------------------------------------------------------------------- Aruna · 2025-08-10 12:00 Quote from: Bandy on August 10, 2025, 01:40:46 pmI'm trying to add items to ListView1 from a text file when the app starts (OnCreate event) with the following code: The app starts, shows the Android icon and the exits, doesn't do anything. What's wrong, please? Right now your code uses Add(a), which always appends to the bottom of the ListView.To make new entries appear at the top, you need to use Insert(0) instead. Also, TListView.Items.Insert(0) doesn’t take the text directly, you must assign it to .Caption after creating the item. Here’s your modified code so that every read line is inserted at the top: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TAndroidModule1.AndroidModule1Create(Sender: TObject);var  f: TextFile;  a: string;  Item: TListItem;begin  AssignFile(f, '/Spațiu de stocare intern/Fisiere/Raspunsuri.txt');  Reset(f);  while not EOF(f) do  begin    // First line    ReadLn(f, a);    Item := ListView1.Items.Insert(0);    Item.Caption := a;     // Second line  ReadLn(f, a);    Item := ListView1.Items.Insert(0);    Item.Caption := a;     // Third line    ReadLn(f, a);    Item := ListView1.Items.Insert(0);    Item.Caption := a;     // Skip lines until a single space ' ' is found    repeat      ReadLn(f, a);    until a = ' ';  end;  CloseFile(f);end;  EDIT: I have attached a minimal working sample in the zip. ---------------------------------------------------------------------- Bandy · 2025-08-10 14:22 I get errors when running the above code in LAMW4Windows 3.8. These are the messages: Messages, Hints: 2 Verbose: Selected chip architecture: arm64-v8a Verbose: Taking libraries from folder: C:\Users\Andy\Desktop\Android\Prognoza\libs\arm64-v8a Verbose: Found library: libcontrols.so Note: Duplicate unit "controls" in "controls", orphaned ppu "C:\Users\Andy\Desktop\Android\Prognoza\obj\controls\controls.o" Note: Duplicate unit "controls" in "LCLBase 3.8", ppu="C:\lamw_manager \LAMW\LAMW4Windows\lazarus_trunk\lcl\units\aarch64- android\controls.ppu", source="C:\lamw_manager\LAMW\LAMW4Windows\lazar us_trunk\lcl\controls.pp" Project: Executing command before: Success before build... Compile Project, OS: android, CPU: aarch64, Target: C:\Users\Andy\Desktop\Android\Prognoza\libs\arm64-v8a\libcontrols.so: Exit code 1, Errors: 11, Hints: 1 unit1.pas(21,61) Hint: Parameter "intentData" not used unit1.pas(63,9) Error: Identifier not found "TListItem" unit1.pas(63,18) Error: Error in type definition unit1.pas(71,38) Error: Wrong number of parameters specified for call to "Insert" classesh.inc(739,15) Error: Found declaration: Insert(LongInt;const AnsiString); unit1.pas(72,10) Error: Illegal qualifier unit1.pas(76,38) Error: Wrong number of parameters specified for call to "Insert" classesh.inc(739,15) Error: Found declaration: Insert(LongInt;const AnsiString); unit1.pas(77,10) Error: Illegal qualifier unit1.pas(81,38) Error: Wrong number of parameters specified for call to "Insert" classesh.inc(739,15) Error: Found declaration: Insert(LongInt;const AnsiString); unit1.pas(82,10) Error: Illegal qualifier Exception, Errors: 1 Fatal: [Exception] Failed: Cannot build project Is the above code for Android app development? I need code for Android app developement. ---------------------------------------------------------------------- dseligo · 2025-08-10 22:28 Quote from: Bandy on August 10, 2025, 01:40:46 pmAssignFile(f, '/Spațiu de stocare intern/Fisiere/Raspunsuri.txt'); Is this path inside your app? If not, you could have issues with permission to access file. I would try two things: 1. Instead of reading a file, try to add just couple of test strings. 2. Try to first create test file in app's storage, and write couple of test lines to that file. After that read that file and see if your code works. ---------------------------------------------------------------------- Aruna · 2025-08-11 02:21 Quote from: Bandy on August 10, 2025, 04:22:59 pmIs the above code for Android app development? I need code for Android app developement.   My apologies, what I sent is Lazarus (desktop) not android. ---------------------------------------------------------------------- Bandy · 2025-08-11 07:32 For dseligo: AssignFile(f, '/Spațiu de stocare intern/Fisiere/Raspunsuri.txt'); is a line inside my app code. The file '/Spațiu de stocare intern/Fisiere/Raspunsuri.txt' is already created and already contains text/strings, I'm just trying to read it. I don't have a working code for this. ---------------------------------------------------------------------- dseligo · 2025-08-11 09:04 Quote from: Bandy on August 11, 2025, 09:32:20 amFor dseligo: AssignFile(f, '/Spațiu de stocare intern/Fisiere/Raspunsuri.txt'); is a line inside my app code. The file '/Spațiu de stocare intern/Fisiere/Raspunsuri.txt' is already created and already contains text/strings, I'm just trying to read it. I don't have a working code for this. Where is this file created? Did you create it in your app and is it located in internal storage of your app? If it is in external storage you'll need permissions to access file. That's why I suggested to steps to test if that is a case. ---------------------------------------------------------------------- Bandy · 2025-08-11 11:59 Quote from: dseligo on August 11, 2025, 11:04:30 amQuote from: Bandy on August 11, 2025, 09:32:20 amFor dseligo: AssignFile(f, '/Spațiu de stocare intern/Fisiere/Raspunsuri.txt'); is a line inside my app code. The file '/Spațiu de stocare intern/Fisiere/Raspunsuri.txt' is already created and already contains text/strings, I'm just trying to read it. I don't have a working code for this. Where is this file created? Did you create it in your app and is it located in internal storage of your app? If it is in external storage you'll need permissions to access file. That's why I suggested to steps to test if that is a case. The file is already created by me, I mean already written by me using the keyboard. /Spațiu de stocare intern/Fisiere/Raspunsuri.txt translates from romanian language to english language to /Internal Storage/Files/Answers.txt. Here is located (stored) the file, in /Internal Storage/Files/Answers.txt. My smartphone has Android 13 in romanian language. Please, I need working code in LAMW4Windows 3.8 to read the file and to view it in ListView1. I hope now it's clear. ---------------------------------------------------------------------- dseligo · 2025-08-11 20:35 Quote from: Bandy on August 11, 2025, 01:59:27 pmQuote from: dseligo on August 11, 2025, 11:04:30 amQuote from: Bandy on August 11, 2025, 09:32:20 amFor dseligo: AssignFile(f, '/Spațiu de stocare intern/Fisiere/Raspunsuri.txt'); is a line inside my app code. The file '/Spațiu de stocare intern/Fisiere/Raspunsuri.txt' is already created and already contains text/strings, I'm just trying to read it. I don't have a working code for this. Where is this file created? Did you create it in your app and is it located in internal storage of your app? If it is in external storage you'll need permissions to access file. That's why I suggested to steps to test if that is a case. The file is already created by me, I mean already written by me using the keyboard. /Spațiu de stocare intern/Fisiere/Raspunsuri.txt translates from romanian language to english language to /Internal Storage/Files/Answers.txt. Here is located (stored) the file, in /Internal Storage/Files/Answers.txt. My smartphone has Android 13 in romanian language. Please, I need working code in LAMW4Windows 3.8 to read the file and to view it in ListView1. I hope now it's clear. It's not clear. Did you create file within your app? Or is it somewhere else in your phone (external storage) and not within your application? If it's in external storage it won't work like that. Look here: https://developer.android.com/training/data-storage Test two things from reply #6 and post results here. ---------------------------------------------------------------------- nicelybrewed · 2025-08-11 22:42 @Bandy Not sure if this is what you're after, but this is a simple add string to a jlistview, loading in a reversed stringlist. Have a look at main.pas attached. You can tweak it how you want it, I'm saving a file to the apps own internal storage using dirDownloads (/Internal storage/Android/data/org.lamw.bandy/files/Download). I'm on Android 14, but expect it should be OK back to Android 10 at least, possibly further, it uses a jListView, jEditText and jButton. ---------------------------------------------------------------------- jmpessoa · 2025-08-11 22:44 Hi, All! For LAMW,  don't use the event "OnCreate". Ex.: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TAndroidModule1.AndroidModule1Create(Sender: TObject); to call Android stuff.... (only to call pure pascal stuff...) For LAMW you need handle "OnJNIPrompt" event to call android API stuff....! ---------------------------------------------------------------------- Bandy · 2025-08-13 07:37 Guys, I want to make a confession: I have little or no Android development knowledge and I have some Windows development knowledge; I have an Windows app made in Lazarus 4.0 for Windows that I love very much; I want to translate this Windows app code to build and run (Ctrl+F1) it in LAMW4Windows 3.8 and on my Android smartphone. So I'm giving you parts of the Lazarus 4.0 for Windows code and please, please, please give me the translation to LAMW4Windows 3.8 code. This is part of the Lazarus 4.0 for Windows code: procedure TForm1.FormCreate(Sender: TObject); var f:TextFile;      a:string; begin AssignFile(f, 'Raspunsuri.txt'); Reset(f);                      while not(eof(f)) do   begin         with ListView1.Items.Insert(0) do           begin             readln(f,a);             Caption:=a;             readln(f,a);             SubItems.Add(a);             readln(f,a);             SubItems.Add(a);           end;         repeat          readln(f,a);         until a=' ';   end; CloseFile(f); end; This code runs when the OnCreate event is triggered in Windows, meaning that this code execute automatically when the Windows app is started, without pressing any buttons or do any action. Also in Android, the code should execute automatically when the Android app is started, without pressing any buttons or do any action. ---------------------------------------------------------------------- cdbc · 2025-08-13 10:11 Hi QuoteFor LAMW you need handle "OnJNIPrompt" event to call android API stuff....!What, in the above is NOT clear to you?!? /bc ---------------------------------------------------------------------- nicelybrewed · 2025-08-16 09:56 @Bandy Check my code in Reply #12, as mentioned by others, use OnJNIPrompt for startup code. ---------------------------------------------------------------------- Bandy · 2025-08-18 13:09 @nicelybrewed: Please read my post from Reply #14 and translate the code for me. @everyone: Where do I find documentation about OnJNIPrompt event? ---------------------------------------------------------------------- Bandy · 2025-08-21 07:09 Anyone... please help me. ---------------------------------------------------------------------- mirce.vladimirov · 2025-08-21 11:38 Quote from: Bandy on August 08, 2025, 10:13:19 amHello. I want code to add items to ListView1:jListView and show them in reversed order, meaning the last added item to be displayed on top of (or first on) the list? How can I do it? This is my code, i used Zeos Query which you can order as you like : Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i      jListView1.Clear;     ZeosQuery1.first;      while not ZeosQuery1.EOF do begin             jListView1.Add(             ZeosQuery1.FieldByName('datafield1').AsString + '|' + // <<- '|' is used as a column separator              'Second field:' + FormatFloat('#,##0.00', ZeosQuery1.FieldByName('datafield2').asfloat) + '|' +             'Third field:' +  ZeosQuery1.FieldByName('datafield3').AsString)            );           ZeosQuery1.Next;     end; EDIT: this is used in my code on show , and i am using that on jbuttonclick too more EDIT: of course , if you dont use Zeos Query but a text file istead, you first need to load that file in a TString List and order it, and then write to jListView Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i var   MyFile: TStringList;begin  MyFile:=TStringList.Create;  MyFile.LoadFromFile('TheRealFile.txt');     MyFile.Sort; //do everything you need to   MyFile.free;  // <<-- do this after you do all the processing the data contained in itend;  and then iterate thru the items of the TSTringlist and put each line in the jListView as described above. I hope that helped. ---------------------------------------------------------------------- dseligo · 2025-08-25 12:25 I created test project which reads file and show content in ListView. These are steps to recreate it: - create new project 'LAMW [GUI] Android Module' and save it - put jButton and jListView on a form and set their layout and positions - in Button1 click method put following code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TAndroidModule1.Button1Click(Sender: TObject);var f: Textfile;    i: Integer;begin  AssignFile(f, GetFilePath(fpathData) + '\test.dat');  Rewrite(f);  WriteLn(f, 'Created: ' + FormatDateTime('yyyy-mm-dd hh:nn:ss ', Now));  For i := 1 to 10 do    WriteLn(f, 'Line ' + IntToStr(i) + '.');  CloseFile(f);end;- in OnJNIPrompt method of the form put following code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);var f: Textfile;    s: String;begin  ListView1.Clear;  If FileExists(GetFilePath(fpathData) + '\test.dat') then  begin    AssignFile(f, GetFilePath(fpathData) + '\test.dat');    Reset(f);    While not Eof(f) do    begin      ReadLn(f, s);      ListView1.Add(s);    end;    CloseFile(f);  end  else    ListView1.Add('File doesn''t exists.');end; After first run there is no file and 'File doesn't exists' message is displayed in ListView. Press Button1 and restart app. Now you should see rows from file in ListView. I attached screenshots from my phone (first run and 2nd run after pressing Button1). I also attached content of JNI directory. ---------------------------------------------------------------------- Bandy · 2025-08-26 08:29 Quote from: dseligo on August 25, 2025, 02:25:29 pmI created test project which reads file and show content in ListView. These are steps to recreate it: - create new project 'LAMW [GUI] Android Module' and save it - put jButton and jListView on a form and set their layout and positions - in Button1 click method put following code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TAndroidModule1.Button1Click(Sender: TObject);var f: Textfile;    i: Integer;begin  AssignFile(f, GetFilePath(fpathData) + '\test.dat');  Rewrite(f);  WriteLn(f, 'Created: ' + FormatDateTime('yyyy-mm-dd hh:nn:ss ', Now));  For i := 1 to 10 do    WriteLn(f, 'Line ' + IntToStr(i) + '.');  CloseFile(f);end;- in OnJNIPrompt method of the form put following code: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);var f: Textfile;    s: String;begin  ListView1.Clear;  If FileExists(GetFilePath(fpathData) + '\test.dat') then  begin    AssignFile(f, GetFilePath(fpathData) + '\test.dat');    Reset(f);    While not Eof(f) do    begin      ReadLn(f, s);      ListView1.Add(s);    end;    CloseFile(f);  end  else    ListView1.Add('File doesn''t exists.');end; After first run there is no file and 'File doesn't exists' message is displayed in ListView. Press Button1 and restart app. Now you should see rows from file in ListView. I attached screenshots from my phone (first run and 2nd run after pressing Button1). I also attached content of JNI directory. Thank you, dseligo! It works! 1) Now, please modify the code to add the lines in reversed order (last added line to be shown first in ListView). 2) After that, please make me understand how to create the file test.dat in other path (location) different from GetFilePath(fpathData) + '\test.dat', let's say on the root of SdCard (something like this SdCard+'\test.dat', and then read test.dat from this different path (location). ---------------------------------------------------------------------- dseligo · 2025-08-26 11:43 Quote from: Bandy on August 26, 2025, 10:29:04 am1) Now, please modify the code to add the lines in reversed order (last added line to be shown first in ListView). This is easy. Just change line where is ListView1.Add(s); to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i ListView1.Insert(0, s); Quote2) After that, please make me understand how to create the file test.dat in other path (location) different from GetFilePath(fpathData) + '\test.dat', let's say on the root of SdCard (something like this SdCard+'\test.dat', and then read test.dat from this different path (location). This is a bit trickier. Problem is that Android limits access to data outside your app for security. I use RequestOpenFile procedure and then copy bytes from chosen file to local app storage. There you can read/write files without restriction. There are (probably) other ways to do it, but this approach is enough for me. To do it, put component jIntentManager to your form. Add following constants somewhere above methods that use them: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i const cRCOpenFile = 1001;      cFileName = '\test.dat'; Change Button1 click method to: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TAndroidModule1.Button1Click(Sender: TObject);begin  RequestOpenFile(GetEnvironmentDirectoryPath(dirDownloads), 'text/plain', cRCOpenFile); // or: dirDocumentsend; Put following code in OnActivityResult method of the form: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i procedure TAndroidModule1.AndroidModule1ActivityResult(Sender: TObject; requestCode: integer;  resultCode: TAndroidResult; intentData: jObject);var Uri: jObject;    BytesFromUri: TDynArrayOfJByte;    fs: TFileStream;begin  If (resultCode = RESULT_OK) and (requestCode = cRCOpenFile) then  begin    Uri := IntentManager1.GetDataUri(intentData);     // write file from Uri to app data directory (where you can read/write as usual)    BytesFromUri := LoadBytesFromUri(Uri);    fs := TFileStream.Create(GetFilePath(fpathData) + cFileName, fmCreate);    try      fs.Seek(0, soFromBeginning);      fs.Write(BytesFromUri[0], Length(BytesFromUri));    finally      fs.Free;      SetLength(BytesFromUri, 0);    end;  end;end; When you press Button1 you'll get to choose file. After choosing file is copied to your app data directory (so you don't have to choose file each time). I attached files from project. ---------------------------------------------------------------------- Bandy · 2025-08-27 08:05 @dseligo: 1) Please explain every line of the code from OnActivityResult method of the form. 2) Please where do I find information/documentation about RequestOpenFile, GetEnvironmentDirectoryPath, dirDownloads, Uri, IntentManager, OnActivityResult event of the form, OnJNIPrompt event of the form, etc.? ---------------------------------------------------------------------- dseligo · 2025-08-28 10:16 Quote from: Bandy on August 27, 2025, 10:05:19 am1) Please explain every line of the code from OnActivityResult method of the form. Method OnActivityResult is called after some activity ends (activity that you started with RequestOpenFile in this case). More here: https: //developer.android.com/reference/android/app/Activity#onActivityResul t(int,%20int,%20android.content.Intent) Checks if this is result for calling RequestOpenFile (because you can call various activities and get results from them): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i If (resultCode = RESULT_OK) and (requestCode = cRCOpenFile) then Get URI reference to data in file (https://developer.android.com/refer ence/android/content/Intent#getData()): Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i Uri := IntentManager1.GetDataUri(intentData); Read bytes from file to an array: Code: Pascal  [Select][+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i BytesFromUri := LoadBytesFromUri(Uri); The rest is standard Free Pascal, it writes data from array to file stream, to create file in scope of your app so you can read contect directly. Quote2) Please where do I find information/documentation about RequestOpenFile, GetEnvironmentDirectoryPath, dirDownloads, Uri, IntentManager, OnActivityResult event of the form, OnJNIPrompt event of the form, etc.? Try and examine examples in your LAMW Lazarus installation directory (..\fpc-lazarus\ccr\lamw\demos\GUI), such as: AppPublicFoldersAccessDemo1, lots of AppIntentDemo... examples, AppTest1, ... You can (or must?) also read Android documentation (I gave you couple of links above) and search through LAMW source code.