List view improper sorting behaviour

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi,
I created simple file browser for windows mobile listing all files and folders. My List view has two column, 1 displaying File/Folder Name and second displaying file size. I made use of FindFirstFile() and FindNextFile() to get File and folder details. Then
I add it to List view.
Following Code adds file/folder name and file size in list view.

<div style="color:black; background-color:white
<pre><span style="color:green //Function add items and subitems to list.
<span style="color:green //ToAddItem: File/Folder Name sent immediately after getting from FindNextFile().
<span style="color:green //ToAddSubItem: Receives low order Filesize.<br/>//iImage gives icon for received folder/file.<br/>//bDiretory: TRUE->cFileName of WIN32_FILE_DATA used in FindNextFile() is directory.


<span style="color:blue void AddItemToList(HWND hList,LPWSTR ToAddItem, DWORD ToAddSubItem, INT iImage, BOOL bDirectory)
{
LV_ITEM LvItem;
INT iItemCount=0;

memset(&LvItem,0,<span style="color:blue sizeof(LvItem));

LvItem.mask = LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM;
LvItem.cchTextMax = MAX_PATH;
LvItem.pszText = ToAddItem;
LvItem.iImage = iImage;
<span style="color:green //Since lParam parameter of LVITEM need to be unique for <br/> //each items im giving value on own using global static DWORD i.
<span style="color:green //For every items i will have unique lParam.
LvItem.lParam = (LPARAM)i;
i++;

iItemCount=ListView_GetItemCount(hList);
LvItem.iItem=iItemCount;

LvItem.iSubItem = 0;
SendMessage(hList,LVM_INSERTITEM,0,(LPARAM)&LvItem);

<span style="color:blue if(!bDirectory)
{
<span style="color:green //FormatWithCommas funtion return DWORD filesize in comma formatted TCHAR()
LvItem.pszText = FormatWithCommas(ToAddSubItem);
LvItem.iSubItem = 1;
SendMessage(hList,LVM_SETITEM,0,(LPARAM)&LvItem);
}
}
[/code]


<br/>
This function adds items successfully in list view. Next when I click on "Name" column, my file names should get sorted. But not happening so:-(
Following is how I handled Column click.

<div style="color:black; background-color:white
<pre><span style="color:blue case LVN_COLUMNCLICK:
pNm = (NMLISTVIEW*)lParam;

<span style="color:green // The user clicked a column header - sort by this criterion.<br/> //ListView_SortItemEx not supported in windows mobile.<br/> <br/> ListView_SortItems(pNm->hdr.hwndFrom,Compare,lParam);
<span style="color:blue break;
[/code]



This part also works well. But I dont no why following Compare function not sorting PROPERLY.
<br/>
<div style="color:black; background-color:white
<pre><span style="color:green //Used to compare and sort list view items.
<span style="color:blue int CALLBACK Compare(LPARAM lParam1, LPARAM lParam2,LPARAM lParamSort)
{
NMLISTVIEW *pnmlv = (NMLISTVIEW*)lParamSort;
DWORD dw1;
DWORD dw2;
TCHAR TempPath[MAX_PATH]={0};
TCHAR str[MAX_PATH];
TCHAR str2[MAX_PATH];

ListView_GetItemText(pnmlv->hdr.hwndFrom, lParam1, pnmlv->iSubItem, str, MAX_PATH);
ListView_GetItemText(pnmlv->hdr.hwndFrom, lParam2, pnmlv->iSubItem, str2, MAX_PATH);
<br/> <span style="color:green //pathTrack keeps hold on current folder/file path. Need path to pass to GetFileAttributes().
wsprintf(TempPath,L<span style="color:#a31515 "%s\%s",pathTrack,str);
dw1=GetFileAttributes(TempPath);


memset(TempPath,0,MAX_PATH);
wsprintf(TempPath,L<span style="color:#a31515 "%s\%s",pathTrack,str2);
dw2=GetFileAttributes(TempPath);<br/> <br/> <span style="color:green //Checks for folder or files. If both comparing string are same attribute, them compare, else leave it.
<span style="color:blue if(!(dw1 & FILE_ATTRIBUTE_DIRECTORY) && !(dw2 & FILE_ATTRIBUTE_DIRECTORY) && (dw1 & FILE_ATTRIBUTE_ARCHIVE) && (dw2 & FILE_ATTRIBUTE_ARCHIVE))
<span style="color:blue return (lstrcmp(str2, str));
<span style="color:blue else <span style="color:blue if((dw1 & FILE_ATTRIBUTE_DIRECTORY) && (dw2 & FILE_ATTRIBUTE_DIRECTORY))
<span style="color:blue return (lstrcmp(str2, str));
}
[/code]


After pressing column button, listview items are rearranged but not sorted.:-(
Plz help me out to sort out problem and list view items..
Thankyou.
-Spark
<br/>
<br/>

View the full article
 
Back
Top