ListView

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

ListView

Postby Lena » Wed Oct 04, 2017 8:54 am

Hi
How add header and footer programmatically? For header I found pascal code:
Adding headers to TListView programmatically
How translate this code for header and footer?
Thanks.
Lena
BCBJ Master
BCBJ Master
 
Posts: 525
Joined: Sun Feb 06, 2011 1:28 pm

Re: ListView

Postby rlebeau » Wed Oct 04, 2017 11:20 am

That example would translate to this:

Code: Select all
// NOTE: you should use the Form's constructor instead of the OnCreate event...
void __fastcall TListViewHeaders::FormCreate(TObject *Sender)
{
    for (int Group = 1; Group <= 4; ++i)
    {
        TListViewItem *pItem = ListView1->Items->Add();
        pItem->Text = Format(_D("Header %d"), ARRAYOFCONST(( Group )) );
        pItem->Purpose = TListItemPurpose::Header;

        for (int Item = 1; Item <= 10; ++Item)
            ListView1->Items->Add()->Text = Format(_D("Regular item %d.%d"), ARRAYOFCONST(( Group, Item )));
    }
}


To create a footer, simply set the TListViewItem::Purpose property to TListItemPurpose::Footer instead.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1457
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: ListView

Postby Lena » Wed Oct 04, 2017 12:27 pm

Thank You very much!
I had another question. I'm using the standard example embarcadero:
C:\Users\Public\Documents\Embarcadero\Studio\18.0\Samples\CPP\Multi-Device Samples\User Interface\ListView\ListViewCheckList
After I added this code and set PrototypeBindSource1->AutoActivate=false:
Code: Select all
__fastcall TForm1::TForm1(TComponent* Owner)
   : TForm(Owner)
{
  for (int Group = 1; Group <= 10; ++Group)
   {
      TListViewItem *pItem = ListView1->Items->Add();
      pItem->Text = Format(_D("Header %d"), ARRAYOFCONST(( Group )) );
      pItem->Purpose = TListItemPurpose::Header;

      ListView1->Items->Add()->Text = Format(_D("Regular item %d.%d"), ARRAYOFCONST(( Group, Group )));

      TListViewItem *pItem2 = ListView1->Items->Add();
      pItem2->Text = Format(_D("Footer %d"), ARRAYOFCONST(( Group )) );
      pItem2->Purpose = TListItemPurpose::Footer;

   }
}


I get AV in event ListView1UpdateObjects. I'm testing in Windows.
If I comment event ListView1UpdateObjects AV disappears.
How fix it?
All code:
Code: Select all
// This software is Copyright (c) 2015 Embarcadero Technologies, Inc.
// You may only use this software if you are an authorized licensee
// of an Embarcadero developer tools product.
// This software is considered a Redistributable as defined under
// the software license agreement that comes with the Embarcadero Products
// and is subject to that software license agreement.

//---------------------------------------------------------------------------

#include <fmx.h>
#include <algorithm>
#include <list>
#pragma hdrstop

#include "ListViewCheckListForm.h"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.fmx"
#pragma resource ("*.LgXhdpiPh.fmx", _PLAT_ANDROID)

TForm2 *Form2;

std::list<int> FChecked;

//---------------------------------------------------------------------------
__fastcall TForm2::TForm2(TComponent* Owner)
   : TForm(Owner)
{
   for (int Group = 1; Group <= 10; ++Group)
   {
      TListViewItem *pItem = ListView1->Items->Add();
      pItem->Text = Format(_D("Header %d"), ARRAYOFCONST(( Group )) );
      pItem->Purpose = TListItemPurpose::Header;

      ListView1->Items->Add()->Text = Format(_D("Regular item %d.%d"), ARRAYOFCONST(( Group, Group )));

      TListViewItem *pItem2 = ListView1->Items->Add();
      pItem2->Text = Format(_D("Footer %d"), ARRAYOFCONST(( Group )) );
      pItem2->Purpose = TListItemPurpose::Footer;

   }
}
//---------------------------------------------------------------------------
void __fastcall TForm2::ListView1ItemClick(const TObject *Sender, const TListViewItem *AItem)

{
   // Toggle visibility of accessory when item is clicked
   // Save checked state of item
   int _index = (const_cast<TListViewItem*>(AItem))->Index;
   if ((const_cast<TListViewItem*>(AItem))->Objects->AccessoryObject->Visible)
   {
      (const_cast<TListViewItem*>(AItem))->Objects->AccessoryObject->Visible = false;
      FChecked.remove(_index);
   }
   else
   {
      (const_cast<TListViewItem*>(AItem))->Objects->AccessoryObject->Visible = true;
      FChecked.push_back(_index);
   }
}
//---------------------------------------------------------------------------

void __fastcall TForm2::ListView1UpdateObjects(const TObject *Sender, const TListViewItem *AItem)
{

   // In order for text to be truncated properly, shorten text object
   TListViewItem::TListViewItemObjects * _objects = (const_cast<TListViewItem*>(AItem))->Objects;
   _objects->TextObject->Width = _objects->TextObject->Width - (5 + _objects->AccessoryObject->Width);
   // Restore checked state when device is rotated.
   // When listview is resized because of rotation, accessory properties will be reset to default values
   if(!FChecked.empty()){
      std::list<int>::iterator it = std::find(FChecked.begin(), FChecked.end(), (const_cast<TListViewItem*>(AItem))->Index);
      if(it != FChecked.end()) {
         _objects->AccessoryObject->Visible = true;
      } else {
            _objects->AccessoryObject->Visible = false;
        }
   }

}
//---------------------------------------------------------------------------
Lena
BCBJ Master
BCBJ Master
 
Posts: 525
Joined: Sun Feb 06, 2011 1:28 pm

Re: ListView

Postby rlebeau » Wed Oct 04, 2017 12:59 pm

Lena wrote:Thank You very much!
I get AV in event ListView1UpdateObjects. I'm testing in Windows.
If I comment event ListView1UpdateObjects AV disappears.
How fix it?


I can't answer that, especially since you didn't indicate which line of code is crashing. You will just have to debug the code for yourself.

But why are you using a std::list to keep track of checked item indexes? Why not a std::vector or std::map? Or even, why use a separate container at all? You could just derive a new class from TAppearanceListViewItem to hold your state data directly in the ListViewItem itself.
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1457
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: ListView

Postby Lena » Thu Oct 05, 2017 12:52 am

I can't answer that, especially since you didn't indicate which line of code is crashing. You will just have to debug the code for yourself.


I put a breakpoint on the line:
_objects->TextObject->Width = _objects->TextObject->Width - (5 + _objects->AccessoryObject->Width);
After clicking the button F8:
First chance exception at $004052F6. Exception class $C0000005 with message 'access violation at 0x004052f6: read of address 0x00000014'. Process ListViewCheckListProject.exe (4376)

But why are you using a std::list to keep track of checked item indexes?


I just started to study ListView. Thanks for your advice. I just opened the standard example embarcadero:
ListView/ListViewCheckList
Then added your code to the constructor:
__fastcall TForm2::TForm2(TComponent* Owner): TForm(Owner)
When I try to compile, I get an AV higher.
Lena
BCBJ Master
BCBJ Master
 
Posts: 525
Joined: Sun Feb 06, 2011 1:28 pm

Re: ListView

Postby rlebeau » Thu Oct 05, 2017 12:50 pm

Lena wrote:I put a breakpoint on the line:
_objects->TextObject->Width = _objects->TextObject->Width - (5 + _objects->AccessoryObject->Width);
After clicking the button F8:
First chance exception at $004052F6. Exception class $C0000005 with message 'access violation at 0x004052f6: read of address 0x00000014'. Process ListViewCheckListProject.exe (4376)


You are clearly accessing a NULL pointer, but did you debug further to figure out where it is coming from exactly? Is _objects set to NULL? TextObject? AccessoryObject? Did you try telling the debugger to go to memory address 0x004052f6 to see what code is running at that address?
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1457
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: ListView

Postby Lena » Thu Oct 05, 2017 2:14 pm

Is _objects set to NULL?


Not NULL. I added if(_objects)

After line:
_objects->TextObject->Width = _objects->TextObject->Width - (5 + _objects->AccessoryObject->Width);
First chance exception at $00404AE4. Exception class $C0000005 with message 'access violation at 0x00404ae4: read of address 0x00000014'. Process Project1.exe (3980)
Attachments
av.jpg
av.jpg (73.25 KiB) Viewed 1541 times
Lena
BCBJ Master
BCBJ Master
 
Posts: 525
Joined: Sun Feb 06, 2011 1:28 pm

Re: ListView

Postby rlebeau » Thu Oct 05, 2017 2:25 pm

Lena wrote:
Is _objects set to NULL?


Not NULL.


What about the OTHER 2 POINTERS I asked about?

If _objects is not NULL, then:

- Is _objects->TextObject set to NULL?
- Is _objects->AccessoryObject set to NULL?
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1457
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA

Re: ListView

Postby Lena » Thu Oct 05, 2017 2:51 pm

What about the OTHER 2 POINTERS


- Is _objects->TextObject set to NULL? Yes NULL.
- Is _objects->AccessoryObject set to NULL? Yes NULL.
Lena
BCBJ Master
BCBJ Master
 
Posts: 525
Joined: Sun Feb 06, 2011 1:28 pm

Re: ListView

Postby rlebeau » Thu Oct 05, 2017 3:28 pm

Lena wrote:- Is _objects->TextObject set to NULL? Yes NULL.
- Is _objects->AccessoryObject set to NULL? Yes NULL.


Well, there you go then. That is why your code is crashing, since you are not checking for those conditions. The fact that they are NULL means you likely have not setup your ListView's Appearance correctly:

Customizing FireMonkey ListView Appearance
Remy Lebeau (TeamB)
Lebeau Software
User avatar
rlebeau
BCBJ Author
BCBJ Author
 
Posts: 1457
Joined: Wed Jun 01, 2005 3:21 am
Location: California, USA


Return to Technical

Who is online

Users browsing this forum: Bing [Bot] and 17 guests

cron