Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

MAUI Grouped ListView Remove Item Causes Error when more than one Group item exists #26015

Open
PBlaa opened this issue Nov 21, 2024 · 1 comment · May be fixed by #26082
Open

MAUI Grouped ListView Remove Item Causes Error when more than one Group item exists #26015

PBlaa opened this issue Nov 21, 2024 · 1 comment · May be fixed by #26082
Labels
area-controls-listview ListView and TableView p/3 Work that is nice to have platform/android 🤖 s/needs-repro Attach a solution or code which reproduces the issue t/bug Something isn't working

Comments

@PBlaa
Copy link

PBlaa commented Nov 21, 2024

Description

I have a grouped list in C# MAUI MVVC, destination is Andoid

public class GroupData : ObservableCollection<DetailData>
{
    public string Name { get; private set; }
    public GroupData(string name, ObservableCollection<DetailData> d) : base(d) 
              { Name = name; }
}

public class DetailData
{
    public int Nr { get; set; }
    public string Detail { get; set; }
}

Test data are added like this

public ObservableCollection<GroupData> GetData()
{
    ObservableCollection<GroupData> data = new ObservableCollection<GroupData>();

    data.Add(new GroupData("Group1", new ObservableCollection<DetailData>
    {
        new DetailData { Nr = 1, Detail = "Test 1" },
        new DetailData { Nr = 2, Detail = "Test 2" }
    }));

    data.Add(new GroupData("Group2", new ObservableCollection<DetailData>
    {
        new DetailData { Nr = 3, Detail = "Test 3" },
        new DetailData { Nr = 4, Detail = "Test 4" }
    }));

    return data;
}

Main Page View Model has the bindable collection

public ObservableCollection<GroupData> GroupData { get; } = new ObservableCollection<GroupData>();

and an event on a button when showing the list.
Clicking on it should remove the selected item.

[RelayCommand]
private async Task ButtonOk(DetailData data)
{
    foreach (var item in GroupData)
    {
        foreach (var child in item)
        {
            if (child.Nr == data.Nr)
            {
                item.Remove(child);
                break;
            }
        }
    }
}

Data are shown with this

<RefreshView Command="{Binding RefreshCommand}" IsRefreshing="{Binding IsBusy, Mode=TwoWay}">
    <ScrollView>
        <Grid>
            <ListView ItemsSource="{Binding GroupData}" 
                      IsGroupingEnabled="True"
                      HasUnevenRows="True">
                <ListView.GroupHeaderTemplate>
                    <DataTemplate x:DataType="daten:GroupData">
                        <ViewCell>
                            <Label Text="{Binding Name}"/>
                        </ViewCell>
                    </DataTemplate>
                </ListView.GroupHeaderTemplate>
                <ListView.ItemTemplate>
                    <DataTemplate x:DataType="daten:DetailData">
                        <ViewCell>
                            <Grid >
                                <Label Text="{Binding Detail}"/>
                                <ImageButton Source="ok.jfif"
                                    Command="{Binding Source={RelativeSource AncestorType={x:Type local:MainPageViewModel}}, 
                                    Path=ButtonOkCommand}" 
                                    CommandParameter="{Binding .}"/>
                            </Grid>
                        </ViewCell>
                    </DataTemplate>
                </ListView.ItemTemplate>
            </ListView>
        </Grid>
    </ScrollView>
</RefreshView>

Now, when adding ONLY ONE grouped item to the observable collection "Group 1" with the two childs the removing works perfect.

But when adding a SECOND "Group 2" it unfortunately fails with error

Java.Lang.IllegalStateException: 'The specified child already has a parent. You must call removeView() on the child's parent first.'

Steps to Reproduce

see code and description

Link to public reproduction project repository

No response

Version with bug

8.0.93 SR9.3

Is this a regression from previous behavior?

No, this is something new

Last version that worked well

No response

Affected platforms

Android

Affected platform versions

No response

Did you find any workaround?

remove

<RefreshView >
    <ScrollView>

Relevant log output

@PBlaa PBlaa added the t/bug Something isn't working label Nov 21, 2024
@PureWeen PureWeen added area-controls-listview ListView and TableView s/needs-repro Attach a solution or code which reproduces the issue platform/android 🤖 labels Nov 21, 2024
Copy link
Contributor

Hi @PBlaa. We have added the "s/needs-repro" label to this issue, which indicates that we require steps and sample code to reproduce the issue before we can take further action. Please try to create a minimal sample project/solution or code samples which reproduce the issue, ideally as a GitHub repo that we can clone. See more details about creating repros here: https://github.com/dotnet/maui/blob/main/.github/repro.md

This issue will be closed automatically in 7 days if we do not hear back from you by then - please feel free to re-open it if you come back to this issue after that time.

@PureWeen PureWeen added the p/3 Work that is nice to have label Nov 21, 2024
@kubaflo kubaflo linked a pull request Nov 24, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-controls-listview ListView and TableView p/3 Work that is nice to have platform/android 🤖 s/needs-repro Attach a solution or code which reproduces the issue t/bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants