Recent

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

Bandy

  • Jr. Member
  • **
  • Posts: 84
How to add items to ListView1:jListView?
« on: August 08, 2025, 10:13:19 am »
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

  • Jr. Member
  • **
  • Posts: 84
Re: How to add items to ListView1:jListView?
« Reply #1 on: August 10, 2025, 11:14:43 am »
Please guys... anyone can help?

cdbc

  • Hero Member
  • *****
  • Posts: 2777
    • http://www.cdbc.dk
Re: How to add items to ListView1:jListView?
« Reply #2 on: August 10, 2025, 12:06:05 pm »
Hi
Look at the examples, demoes & the source-code provided and think a little bit... Don't be so helpless.
Regards Benny
If it ain't broke, don't fix it ;)
PCLinuxOS(rolling release) 64bit -> KDE6/QT6 -> FPC Release -> Lazarus Release &  FPC Main -> Lazarus Main

Bandy

  • Jr. Member
  • **
  • Posts: 84
Re: How to add items to ListView1:jListView?
« Reply #3 on: August 10, 2025, 01:40:46 pm »
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

  • Hero Member
  • *****
  • Posts: 799
Re: How to add items to ListView1:jListView?
« Reply #4 on: August 10, 2025, 02:00:31 pm »
I'm trying to add items to ListView1 from a text file when the app starts (OnCreate event) with the following code:
<snip>
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][+][-]
  1. procedure TAndroidModule1.AndroidModule1Create(Sender: TObject);
  2. var
  3.   f: TextFile;
  4.   a: string;
  5.   Item: TListItem;
  6. begin
  7.   AssignFile(f, '/Spațiu de stocare intern/Fisiere/Raspunsuri.txt');
  8.   Reset(f);
  9.   while not EOF(f) do
  10.   begin
  11.     // First line
  12.     ReadLn(f, a);
  13.     Item := ListView1.Items.Insert(0);
  14.     Item.Caption := a;
  15.  
  16.     // Second line
  17.     ReadLn(f, a);
  18.     Item := ListView1.Items.Insert(0);
  19.     Item.Caption := a;
  20.  
  21.     // Third line
  22.     ReadLn(f, a);
  23.     Item := ListView1.Items.Insert(0);
  24.     Item.Caption := a;
  25.  
  26.     // Skip lines until a single space ' ' is found
  27.     repeat
  28.       ReadLn(f, a);
  29.     until a = ' ';
  30.   end;
  31.   CloseFile(f);
  32. end;
  33.  

EDIT: I have attached a minimal working sample in the zip.
« Last Edit: August 10, 2025, 02:04:08 pm by Aruna »

Bandy

  • Jr. Member
  • **
  • Posts: 84
Re: How to add items to ListView1:jListView?
« Reply #5 on: August 10, 2025, 04:22:59 pm »
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\lazarus_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

  • Hero Member
  • *****
  • Posts: 1683
Re: How to add items to ListView1:jListView?
« Reply #6 on: August 11, 2025, 12:28:02 am »
AssignFile(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

  • Hero Member
  • *****
  • Posts: 799
Re: How to add items to ListView1:jListView?
« Reply #7 on: August 11, 2025, 04:21:39 am »
Is 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

  • Jr. Member
  • **
  • Posts: 84
Re: How to add items to ListView1:jListView?
« Reply #8 on: August 11, 2025, 09:32:20 am »
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

  • Hero Member
  • *****
  • Posts: 1683
Re: How to add items to ListView1:jListView?
« Reply #9 on: August 11, 2025, 11:04:30 am »
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.

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

  • Jr. Member
  • **
  • Posts: 84
Re: How to add items to ListView1:jListView?
« Reply #10 on: August 11, 2025, 01:59:27 pm »
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.

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.
« Last Edit: August 11, 2025, 02:22:04 pm by Bandy »

dseligo

  • Hero Member
  • *****
  • Posts: 1683
Re: How to add items to ListView1:jListView?
« Reply #11 on: August 11, 2025, 10:35:47 pm »
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.

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

  • New Member
  • *
  • Posts: 26
Re: How to add items to ListView1:jListView?
« Reply #12 on: August 12, 2025, 12:42:14 am »
@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.
Lazarus v3.8, LAMW v0.8.6.4-blue, NDK v22b, Android 15, Windows 11 mainly but also use LAMW on Linux.

jmpessoa

  • Hero Member
  • *****
  • Posts: 2330
Re: How to add items to ListView1:jListView?
« Reply #13 on: August 12, 2025, 12:44:25 am »
Hi, All!

For LAMW,  don't use the event "OnCreate". Ex.:
Code: Pascal  [Select][+][-]
  1. procedure TAndroidModule1.AndroidModule1Create(Sender: TObject);
  2.  
to call Android stuff.... (only to call pure pascal stuff...)

For LAMW you need handle "OnJNIPrompt" event to call android API stuff....!
Lamw: Lazarus Android Module Wizard
https://github.com/jmpessoa/lazandroidmodulewizard

Bandy

  • Jr. Member
  • **
  • Posts: 84
Re: How to add items to ListView1:jListView?
« Reply #14 on: August 13, 2025, 09:37:57 am »
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.

 

TinyPortal © 2005-2018