2

我无法弄清楚这里有什么问题。我试图将 Idea 类型中的对象“idea”添加到列表ideaList 中。然后它抛出这个异常。有谁知道?

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) 在 smoca.ch.kreagen.FragmentStateHandler.chooseAndShowForVoting(FragmentStateHandler.java:69) 在 smoca.ch.kreagen.Adapters.ArchiveAdapter$1.onClick(ArchiveAdapter.java:50) 在 android.view.View.performClick(View .java:5155)

我想在列表创建并由领域数据填充后添加它。就在第一个以 Idea 为参数的构造函数中。

班级代码:

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));
    }
}

}

4

1 回答 1

3

您不能修改作为 RealmResult 返回的列表

相反,您可能希望复制查询结果以获取可变列表。

就像是

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();}

如您所见,您不能再使用 Collections.emptyList 作为默认值,因为它是不可变的,我们不直接使用 ideaQueryResult,而是将列表中的所有元素复制到我们自己的可变 ArrayList 中。

于 2015-10-06T09:00:57.937 回答