Итак, у меня есть представление коллекции с выбором шаблона элемента. Оба этих шаблона также имеют собственное представление коллекции с выбором шаблона элемента. Когда я добавляю первый элемент во внутреннее представление коллекции, все в порядке. Но когда я попытался добавить второй элемент, я получил исключение. Проблема, как я понимаю, не в этой вложенности, потому что я переработал свой пример, чтобы была только одна коллекция с селектором шаблона элемента, но у меня все равно возникла та же ошибка.
Ошибка трассировка стека:
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
at android.view.ViewGroup.addViewInner(ViewGroup.java:5248)
at android.view.ViewGroup.addView(ViewGroup.java:5077)
at android.view.ViewGroup.addView(ViewGroup.java:5017)
at android.view.ViewGroup.addView(ViewGroup.java:4989)
at crc645d80431ce5f73f11.GroupableItemsViewAdapter_2.n_onBindViewHolder(Native Method)
at crc645d80431ce5f73f11.GroupableItemsViewAdapter_2.onBindViewHolder(GroupableItemsViewAdapter_2.java:48)
at androidx.recyclerview.widget.RecyclerView$Adapter.onBindViewHolder(RecyclerView.java:7254)
at androidx.recyclerview.widget.RecyclerView$Adapter.bindViewHolder(RecyclerView.java:7337)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryBindViewHolderByDeadline(RecyclerView.java:6194)
at androidx.recyclerview.widget.RecyclerView$Recycler.tryGetViewHolderForPositionByDeadline(RecyclerView.java:6460)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6300)
at androidx.recyclerview.widget.RecyclerView$Recycler.getViewForPosition(RecyclerView.java:6296)
at androidx.recyclerview.widget.LinearLayoutManager$LayoutState.next(LinearLayoutManager.java:2330)
at androidx.recyclerview.widget.LinearLayoutManager.layoutChunk(LinearLayoutManager.java:1631)
at androidx.recyclerview.widget.LinearLayoutManager.fill(LinearLayoutManager.java:1591)
at androidx.recyclerview.widget.LinearLayoutManager.onLayoutChildren(LinearLayoutManager.java:668)
at androidx.recyclerview.widget.RecyclerView.dispatchLayoutStep2(RecyclerView.java:4309)
at androidx.recyclerview.widget.RecyclerView.dispatchLayout(RecyclerView.java:4012)
at androidx.recyclerview.widget.RecyclerView.consumePendingUpdateOperations(RecyclerView.java:2028)
at androidx.recyclerview.widget.RecyclerView$ViewFlinger.run(RecyclerView.java:5434)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1231)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:1239)
at android.view.Choreographer.doCallbacks(Choreographer.java:899)
at android.view.Choreographer.doFrame(Choreographer.java:827)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:1214)
at android.os.Handler.handleCallback(Handler.java:942)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loopOnce(Looper.java:201)
at android.os.Looper.loop(Looper.java:288)
at android.app.ActivityThread.main(ActivityThread.java:7872)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:548)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:936)
Мое мнение:
//Other data templates
//Exception when i press this button second time
Также просмотрите код модели:
using Bibliomatic_MAUI_App.Models;
using Bibliomatic_MAUI_App.Services;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Syncfusion.Maui.DataSource.Extensions;
using System.Collections.ObjectModel;
using System.Text.RegularExpressions;
namespace Bibliomatic_MAUI_App.ViewModels
{
[QueryProperty(nameof(Test), "Test")]
public partial class TestEditorViewModel : ObservableObject
{
public List TestQuestionTypes { get; set; } = new List();
public ObservableCollection TestQuestionTypesValues { get; set; } = new ObservableCollection();
[ObservableProperty]
public TestResponse test;
[ObservableProperty]
public TestQuestionTypeValues selectedQuestionTypeValue;
private readonly LocalTestKeeper localTestKeeper;
private readonly LocalTestQuestionImagesKeeper localTestQuestionImagesKeeper;
private readonly LocalTestAnswerImagesKeeper localTestAnswerImagesKeeper;
public TestEditorViewModel(LocalTestKeeper localTestKeeper,
LocalTestQuestionImagesKeeper localTestQuestionImagesKeeper,
LocalTestAnswerImagesKeeper localTestAnswerImagesKeeper)
{
this.localTestKeeper = localTestKeeper;
this.localTestQuestionImagesKeeper = localTestQuestionImagesKeeper;
this.localTestAnswerImagesKeeper = localTestAnswerImagesKeeper;
}
[RelayCommand]
public void OpenPicker()
{
TestQuestionTypesValues.Clear();
List testQuestionTypes = Enum.GetValues(typeof(TestQuestionType))
.Cast()
.ToList();
testQuestionTypes.ForEach(qt => TestQuestionTypesValues.Add(new TestQuestionTypeValues(qt)));
}
[RelayCommand]
public void AddTestQuestion()
{
var questionToBeAdded = new TestQuestion
{
Id = Guid.NewGuid(),
Question = string.Empty,
Test = Test,
TestId = Test.Id,
IsUpdated = false,
QuestionNumber = Test.TestQuestions.Count + 1,
TestQuestionType = SelectedQuestionTypeValue.Type,
QuestionImageSource = string.Empty,
TestAnswers = new ObservableCollection(),
DeletedTestAnswers = new ObservableCollection()
};
Test.TestQuestions.Add(questionToBeAdded);
}
[RelayCommand]
public void DeleteTestQuestion(TestQuestion selectedTestQuestion)
{
Test.TestQuestions.Remove(selectedTestQuestion);
Test.TestQuestions.Cast()
.Where(tq => tq.QuestionNumber > selectedTestQuestion.QuestionNumber)
.ForEach(tq => tq.QuestionNumber--);
}
[RelayCommand]
public void AddTestAnswer(TestQuestion selectedTestQuestion)
{
var answerToBeAdded = new TestAnswer
{
Id = Guid.NewGuid(),
Answer = string.Empty,
Variant = string.Empty,
Message = string.Empty,
IsSelected = false,
AnswerNumber = selectedTestQuestion.TestAnswers.Count + 1,
SelectedTestAnswer = null,
TestQuestion = selectedTestQuestion,
TestQuestionId = selectedTestQuestion.Id,
IsCorrectAnswer = false
};
selectedTestQuestion.TestAnswers.Add(answerToBeAdded);
}
[RelayCommand]
public void DeleteTestAnswer(TestAnswer selectedTestAnswer)
{
var selectedTestQuestion = selectedTestAnswer.TestQuestion;
selectedTestQuestion.TestAnswers.Remove(selectedTestAnswer);
UpdateAnswerNumberWhenRemove(selectedTestQuestion, selectedTestAnswer);
}
public void UpdateAnswerNumberWhenRemove(TestQuestionResponse testQuestion, TestAnswer testAnswer)
{
testQuestion.TestAnswers.Cast()
.Where(ta => ta.AnswerNumber > testAnswer.AnswerNumber)
.ForEach(ta => ta.AnswerNumber--);
}
public void UpdateAnswerNumberWhenInsert(TestQuestion testQuestion)
{
for (int index = 0; index < testQuestion.TestAnswers.Count; index++)
((TestAnswer)testQuestion.TestAnswers.ElementAt(index)).AnswerNumber = index + 1;
}
[RelayCommand]
public async void PickTestQuestionImage(TestQuestion selectedTestQuestion)
{
var image = await PickImage();
if (image == null)
return;
var imagePath = await localTestQuestionImagesKeeper.SaveFileToLocalStorage(selectedTestQuestion, image);
selectedTestQuestion.QuestionImageSource = imagePath;
}
[RelayCommand]
public async void PickTestAnswerImage(TestAnswer selectedTestAnswer)
{
var image = await PickImage();
if (image == null)
return;
string oldAnswerImagePath = selectedTestAnswer.AnswerImageSource;
string newAnswerImagePath = await localTestAnswerImagesKeeper.SaveFileToLocalStorage(selectedTestAnswer, image, oldAnswerImagePath);
selectedTestAnswer.AnswerImageSource = newAnswerImagePath;
}
[RelayCommand]
public async void PickTestVariantImage(TestAnswer selectedTestAnswer)
{
var image = await PickImage();
if(image == null)
return;
string oldVariantImagePath = selectedTestAnswer.VariantImageSource;
string newVariantImagePath = await localTestAnswerImagesKeeper.SaveFileToLocalStorage(selectedTestAnswer, image, oldVariantImagePath);
selectedTestAnswer.VariantImageSource = newVariantImagePath;
}
private async Task PickImage()
{
var selectedImage = await MediaPicker.PickPhotoAsync(new MediaPickerOptions
{
Title = "Please select image"
});
return selectedImage;
}
[RelayCommand]
public async void Unfocused()
{
await localTestKeeper.SaveFileToLocalStorage(Test);
}
}
}
Подробнее здесь: https://stackoverflow.com/questions/766 ... s-a-parent
MAUI Java.Lang.IllegalStateException: 'У указанного дочернего элемента уже есть родительский элемент. Сначала вы должны ⇐ Android
-
- Похожие темы
- Ответы
- Просмотры
- Последнее сообщение