UnsupportedOperationException when adding element to List (no Arrays.asList)

时间:2015-10-06 08:50:15

标签: android list exception add unsupportedoperation

I cant figure out whats the problem here. Im trying to add an Object "idea" from type Idea to the list ideaList. And then it throws this exception. Anyone knows?

java.lang.UnsupportedOperationException at java.util.AbstractList.add(AbstractList.java:404) at java.util.AbstractList.add(AbstractList.java:425) at smoca.ch.kreagen.Fragments.VotingFragment.(VotingFragment.java:50) at smoca.ch.kreagen.FragmentStateHandler.chooseAndShowForVoting(FragmentStateHandler.java:69) at smoca.ch.kreagen.Adapters.ArchiveAdapter$1.onClick(ArchiveAdapter.java:50) at android.view.View.performClick(View.java:5155)

I want to add it after the list has been created and filled by realm-data. right in the first constructor with Idea as param.

Class-Code:

public class VotingFragment extends Fragment implements  VotingAdapter.ClickListener{

private RecyclerView recyclerView;
private VotingAdapter votingAdapter;
private Realm realm;
private List<Idea> ideaList;
private List<Vote> voteList;
private List<Owner> ownerList;
private FragmentStateHandler fragmentStateHandler;
private TextView status;
private Button startVoting;
private Button stopVoting;

public VotingFragment(Idea idea) {
    getData();
    ideaList.add(idea);
    setStatus(true);
}

public VotingFragment() {
    getData();
    setStatus(false);
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public void onAttach(Activity activity) {
    super.onAttach(activity);

}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
    super.onCreateView(inflater, container, savedInstanceState);
    View layout = inflater.inflate(R.layout.voting_fragment_layout, container, false);
    recyclerView = (RecyclerView) layout.findViewById(R.id.votingDrawerList);
    realm = Realm.getInstance(getActivity());
    fragmentStateHandler = FragmentStateHandler.getInstance();

    // get buttons and views
    status = (TextView) layout.findViewById(R.id.statusTextView);
    startVoting = (Button) layout.findViewById(R.id.startButton);
    stopVoting = (Button) layout.findViewById(R.id.stopButton);

    // clicklisteners for start/stop
    startVoting.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setStatus(true);
            // put ideas in List
            // put votes in List
            // put Owners in List
            // sort Voting
            // set timestamp (24h)

        }
    });

    stopVoting.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            setStatus(false);
            // clear ideas from List
            // put voting in History


        }
    });

    votingAdapter = new VotingAdapter(getActivity(), ideaList, voteList, ownerList);    // params: context, idealist, votelist, ownerlist
    votingAdapter.setClickListener(this);   // set clicklistener on this
    recyclerView.setAdapter(votingAdapter);
    recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));


    return layout;
}


public void getData() {
    ideaList = Collections.emptyList();
    voteList = Collections.emptyList();
    ownerList = Collections.emptyList();

    try {
        RealmQuery<Idea> ideaQuery = realm.where(Idea.class);
        RealmResults<Idea> ideaQueryResult = ideaQuery.findAll();

        RealmQuery<Vote> voteQuery = realm.where(Vote.class);
        RealmResults<Vote> voteQueryResult = voteQuery.findAll();

        RealmQuery<Owner> ownerQuery = realm.where(Owner.class);
        RealmResults<Owner> ownerQueryResult = ownerQuery.findAll();

        ideaList = ideaQueryResult;
        voteList = voteQueryResult;
        ownerList = ownerQueryResult;

    } catch (Exception e) { e.printStackTrace();}

    Log.d("joris", "" + ideaList);
    Log.d("joris", "" + voteList);
    Log.d("joris", "" + ownerList);

}

// items clicked in recyclerview
@Override
public void itemClicked(View view, int position) {
    Idea current = ideaList.get(position);
    fragmentStateHandler.showIdeaFrag(current);

}

public void setStatus(boolean val) {
    if(val) {
        status.setText("OPEN");
        status.setTextColor(getResources().getColor(R.color.Green));
    } else {
        status.setText("CLOSED");
        status.setTextColor(getResources().getColor(R.color.Red));
    }
}

}

1 个答案:

答案 0 :(得分:3)

You can't modify the Lists you get back as RealmResult

Instead you probably want to copy the results of your queries to get mutable lists.

Something like

ideaList = new ArrayList<>();
voteList = Collections.emptyList();
ownerList = Collections.emptyList();

try {
    RealmQuery<Idea> ideaQuery = realm.where(Idea.class);
    RealmResults<Idea> ideaQueryResult = ideaQuery.findAll();

    RealmQuery<Vote> voteQuery = realm.where(Vote.class);
    RealmResults<Vote> voteQueryResult = voteQuery.findAll();

    RealmQuery<Owner> ownerQuery = realm.where(Owner.class);
    RealmResults<Owner> ownerQueryResult = ownerQuery.findAll();

    ideaList.addAll(ideaQueryResult);
    voteList = voteQueryResult;
    ownerList = ownerQueryResult;

} catch (Exception e) { e.printStackTrace();}

As you see you can no longer use the Collections.emptyList as default value as it is immutable, and instead of using the ideaQueryResult directly we copy all the elements from the list to our own mutable ArrayList.