我无法弄清楚这里有什么问题。我试图将 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));
}
}
}