Skip to content Skip to sidebar Skip to footer

Java.lang.runtimeexception: Illegal Position! In Tagview

TagView library is used in the app that I'm developing. I'm adding the tags from an ArrayList which I get from a MultiSelectDialog. The tags I'm adding to the TagView does have an

Solution 1:

This error will be thrown under the following circumstances:

privatevoid onAddTag(String text, int position) {
        if (position < 0 || position > mChildViews.size()) {
            thrownewRuntimeException("Illegal position!");
        }

From your code:

mTagContainerLayout2.addTag(selectedNames.get(i), selectedIds.get(i));

You are trying to add tags to a position which doesn't exists inside the TagView layout.

onAddTag(String text, int position) is useful if you already have a list of TagView and you want to add a new item in between or at the end of the list.

Use

mTagContainerLayout.addTag(String text); for adding single item.

Or

mTagContainerLayout.setTags(List<String> tags); for adding multiple items.

How indexing works?

id for the tags will auto increase based on indexing starting from 0. You might be wondering how to detect the MultiSelectDialogid along with the TagViewid? For this you can use a HashMap with MultiSelectDialogid as key and TagViewid as value.

HashMap<Integer, Integer> hashMap = new HashMap<Integer, Integer>();

.onSubmit(new MultiSelectDialog.SubmitCallbackListener() {
@Override
publicvoid onSelected(ArrayList<Integer> selectedIds, ArrayList<String> selectedNames, String dataString) {
    //Adding tags
    mTagContainerLayout2.addTag(selectedNames());

    for (int i = 0; i < selectedIds.size(); i++) {
        hashMap.add(selectedIds.get(i), i);
    }
}

To get the id of a TagView item by the id of MultiSelectDialog

int selectedTagView = hashMap.get(multiSelectDialogId);

Post a Comment for "Java.lang.runtimeexception: Illegal Position! In Tagview"