0

I am starting to learn coding for android now and have a Problem in a ListFragment. I found threads with the same problem but none of them helped be finding / understanding the problem in my code.

I created an Android bottom navigation activity and modified the Dashboard Fragment. The Goal is to have a list with a button and when it is clicked a keyboard should open, a new string be entered and added to the list. In the following code I tried to implement a button that adds always the same string when it is clicked since I am still learning, I will work on the keyboard part later.

The code always compiles without problem but crashes when the button is clicked and the adapter.add command is executed. It also crashes when I add the adapter.add directly after the setListAdapter in onCreateView.

These are the files I use:

DashboardFragment.java

package com.example.aktiehq.workout.ui.dashboard;

import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.TextView;

import androidx.fragment.app.ListFragment;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.Observer;
import androidx.lifecycle.ViewModelProviders;

import com.example.aktiehq.workout.R;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class DashboardFragment extends ListFragment {

    //private DashboardViewModel dashboardViewModel;
    String[] values = new String[] { "Message1", "Message2", "Message3" };
    public ArrayAdapter<String> adapter;

    public View onCreateView(@NonNull LayoutInflater inflater,
                             ViewGroup container, Bundle savedInstanceState) {

            View rootView = inflater.inflate(R.layout.fragment_dashboard, container,
                    false);


            adapter = new ArrayAdapter<String>(getActivity(),
                    android.R.layout.simple_list_item_1, values);
            setListAdapter(adapter);

            FloatingActionButton button = (FloatingActionButton) rootView.findViewById(R.id.addListElement);
            //b.setOnClickListener((View.OnClickListener) this);
            button.setOnClickListener(new View.OnClickListener()
            {
                @Override
                public void onClick(View v)
                {
                    adapter.add("test");
                    adapter.notifyDataSetChanged();
                }
            });
        return rootView;

    }

}

fragment_dashboard.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".ui.dashboard.DashboardFragment">

    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/addListElement"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="64dp"
        android:clickable="true"
        android:src="@android:drawable/ic_input_add"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <ListView
        android:id="@android:id/list"
        android:layout_width="409dp"
        android:layout_height="609dp"
        tools:layout_editor_absoluteX="1dp"
        tools:layout_editor_absoluteY="1dp" />

</androidx.constraintlayout.widget.ConstraintLayout>

Error when I click the button:

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.example.aktiehq.workout, PID: 18355
    java.lang.UnsupportedOperationException
        at java.util.AbstractList.add(AbstractList.java:404)
        at java.util.AbstractList.add(AbstractList.java:425)
        at android.widget.ArrayAdapter.add(ArrayAdapter.java:194)
        at com.example.aktiehq.workout.ui.dashboard.DashboardFragment$1.onClick(DashboardFragment.java:45)
        at android.view.View.performClick(View.java:5198)
        at android.view.View$PerformClick.run(View.java:21147)
        at android.os.Handler.handleCallback(Handler.java:739)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:148)
        at android.app.ActivityThread.main(ActivityThread.java:5417)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

Can anyone please help me explaining and fixing the problem?

Thank you very much in advance,

Frank

  • You can't add/remove items from an `ArrayAdapter` backed by an array. Use an `ArrayList` instead, then your `add()` will work. – Mike M. May 02 '20 at 06:46
  • Do you have a custom add() method in your adapter that adds to your ArrayList? – tendai May 02 '20 at 07:48
  • Can you please post the code of your adapter here – tendai May 02 '20 at 07:48
  • 1. First change String[] values to ArrayList values = new ArrayList() 2. Override onCreate() and init value for values by values.add("Message1"), values.add("Message2") ... 3. in method onClick call values.add("test"); adapter.notifyDatasetChange() – Công Hải May 02 '20 at 10:27

0 Answers0