Recent

Author Topic: How to add items to ListView1:jListView?  (Read 7938 times)

cdbc

  • Hero Member
  • *****
  • Posts: 2807
    • http://www.cdbc.dk
Re: How to add items to ListView1:jListView?
« Reply #15 on: August 13, 2025, 12:11:15 pm »
Hi
Quote
For LAMW you need handle "OnJNIPrompt" event to call android API stuff....!
What, in the above is NOT clear to you?!?
/bc
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

nicelybrewed

  • New Member
  • *
  • Posts: 26
Re: How to add items to ListView1:jListView?
« Reply #16 on: August 16, 2025, 11:56:03 am »
@Bandy
Check my code in Reply #12, as mentioned by others, use OnJNIPrompt for startup code.
Lazarus v3.8, LAMW v0.8.6.4-blue, NDK v22b, Android 15, Windows 11 mainly but also use LAMW on Linux.

Bandy

  • Jr. Member
  • **
  • Posts: 84
Re: How to add items to ListView1:jListView?
« Reply #17 on: August 18, 2025, 03:09:49 pm »
@nicelybrewed: Please read my post from Reply #14 and translate the code for me.

@everyone: Where do I find documentation about OnJNIPrompt event?
« Last Edit: August 21, 2025, 09:07:47 am by Bandy »

Bandy

  • Jr. Member
  • **
  • Posts: 84
Re: How to add items to ListView1:jListView?
« Reply #18 on: August 21, 2025, 09:09:37 am »
Anyone... please help me.

mirce.vladimirov

  • Sr. Member
  • ****
  • Posts: 269
Re: How to add items to ListView1:jListView?
« Reply #19 on: August 21, 2025, 01:38:12 pm »
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?

This is my code, i used Zeos Query which you can order as you like :
Code: Pascal  [Select][+][-]
  1.      jListView1.Clear;
  2.      ZeosQuery1.first;
  3.  
  4.      while not ZeosQuery1.EOF do begin
  5.              jListView1.Add(
  6.              ZeosQuery1.FieldByName('datafield1').AsString + '|' + // <<- '|' is used as a column separator
  7.              'Second field:' + FormatFloat('#,##0.00', ZeosQuery1.FieldByName('datafield2').asfloat) + '|' +
  8.              'Third field:' +  ZeosQuery1.FieldByName('datafield3').AsString)
  9.            );
  10.              ZeosQuery1.Next;
  11.      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][+][-]
  1. var
  2.   MyFile: TStringList;
  3. begin
  4.   MyFile:=TStringList.Create;
  5.   MyFile.LoadFromFile('TheRealFile.txt');  
  6.   MyFile.Sort;
  7.  
  8. //do everything you need to
  9.  
  10.   MyFile.free;  // <<-- do this after you do all the processing the data contained in it
  11. end;
  12.  

and then iterate thru the items of the TSTringlist and put each line in the jListView as described above.

I hope that helped.
« Last Edit: August 21, 2025, 01:56:15 pm by mirce.vladimirov »

dseligo

  • Hero Member
  • *****
  • Posts: 1686
Re: How to add items to ListView1:jListView?
« Reply #20 on: August 25, 2025, 02:25:29 pm »
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][+][-]
  1. procedure TAndroidModule1.Button1Click(Sender: TObject);
  2. var f: Textfile;
  3.     i: Integer;
  4. begin
  5.   AssignFile(f, GetFilePath(fpathData) + '\test.dat');
  6.   Rewrite(f);
  7.   WriteLn(f, 'Created: ' + FormatDateTime('yyyy-mm-dd hh:nn:ss ', Now));
  8.   For i := 1 to 10 do
  9.     WriteLn(f, 'Line ' + IntToStr(i) + '.');
  10.   CloseFile(f);
  11. end;
- in OnJNIPrompt method of the form put following code:
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  2. var f: Textfile;
  3.     s: String;
  4. begin
  5.   ListView1.Clear;
  6.   If FileExists(GetFilePath(fpathData) + '\test.dat') then
  7.   begin
  8.     AssignFile(f, GetFilePath(fpathData) + '\test.dat');
  9.     Reset(f);
  10.     While not Eof(f) do
  11.     begin
  12.       ReadLn(f, s);
  13.       ListView1.Add(s);
  14.     end;
  15.     CloseFile(f);
  16.   end
  17.   else
  18.     ListView1.Add('File doesn''t exists.');
  19. 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

  • Jr. Member
  • **
  • Posts: 84
Re: How to add items to ListView1:jListView?
« Reply #21 on: August 26, 2025, 10:29:04 am »
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][+][-]
  1. procedure TAndroidModule1.Button1Click(Sender: TObject);
  2. var f: Textfile;
  3.     i: Integer;
  4. begin
  5.   AssignFile(f, GetFilePath(fpathData) + '\test.dat');
  6.   Rewrite(f);
  7.   WriteLn(f, 'Created: ' + FormatDateTime('yyyy-mm-dd hh:nn:ss ', Now));
  8.   For i := 1 to 10 do
  9.     WriteLn(f, 'Line ' + IntToStr(i) + '.');
  10.   CloseFile(f);
  11. end;
- in OnJNIPrompt method of the form put following code:
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.AndroidModule1JNIPrompt(Sender: TObject);
  2. var f: Textfile;
  3.     s: String;
  4. begin
  5.   ListView1.Clear;
  6.   If FileExists(GetFilePath(fpathData) + '\test.dat') then
  7.   begin
  8.     AssignFile(f, GetFilePath(fpathData) + '\test.dat');
  9.     Reset(f);
  10.     While not Eof(f) do
  11.     begin
  12.       ReadLn(f, s);
  13.       ListView1.Add(s);
  14.     end;
  15.     CloseFile(f);
  16.   end
  17.   else
  18.     ListView1.Add('File doesn''t exists.');
  19. 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).
« Last Edit: August 26, 2025, 11:34:58 am by Bandy »

dseligo

  • Hero Member
  • *****
  • Posts: 1686
Re: How to add items to ListView1:jListView?
« Reply #22 on: August 26, 2025, 01:43:45 pm »
1) 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][+][-]
  1. ListView1.Insert(0, s);

Quote
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).

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][+][-]
  1. const cRCOpenFile = 1001;
  2.       cFileName = '\test.dat';

Change Button1 click method to:
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.Button1Click(Sender: TObject);
  2. begin
  3.   RequestOpenFile(GetEnvironmentDirectoryPath(dirDownloads), 'text/plain', cRCOpenFile); // or: dirDocuments
  4. end;

Put following code in OnActivityResult method of the form:
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.AndroidModule1ActivityResult(Sender: TObject; requestCode: integer;
  2.   resultCode: TAndroidResult; intentData: jObject);
  3. var Uri: jObject;
  4.     BytesFromUri: TDynArrayOfJByte;
  5.     fs: TFileStream;
  6. begin
  7.   If (resultCode = RESULT_OK) and (requestCode = cRCOpenFile) then
  8.   begin
  9.     Uri := IntentManager1.GetDataUri(intentData);
  10.  
  11.     // write file from Uri to app data directory (where you can read/write as usual)
  12.     BytesFromUri := LoadBytesFromUri(Uri);
  13.     fs := TFileStream.Create(GetFilePath(fpathData) + cFileName, fmCreate);
  14.     try
  15.       fs.Seek(0, soFromBeginning);
  16.       fs.Write(BytesFromUri[0], Length(BytesFromUri));
  17.     finally
  18.       fs.Free;
  19.       SetLength(BytesFromUri, 0);
  20.     end;
  21.   end;
  22. 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.
« Last Edit: August 26, 2025, 01:48:13 pm by dseligo »

Bandy

  • Jr. Member
  • **
  • Posts: 84
Re: How to add items to ListView1:jListView?
« Reply #23 on: August 27, 2025, 10:05:19 am »
@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.?
« Last Edit: August 28, 2025, 09:27:12 am by Bandy »

dseligo

  • Hero Member
  • *****
  • Posts: 1686
Re: How to add items to ListView1:jListView?
« Reply #24 on: August 28, 2025, 12:16:43 pm »
1) 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#onActivityResult(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][+][-]
  1. If (resultCode = RESULT_OK) and (requestCode = cRCOpenFile) then

Get URI reference to data in file (https://developer.android.com/reference/android/content/Intent#getData()):
Code: Pascal  [Select][+][-]
  1. Uri := IntentManager1.GetDataUri(intentData);

Read bytes from file to an array:
Code: Pascal  [Select][+][-]
  1. 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.

Quote
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.?

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.

 

TinyPortal © 2005-2018