ここで何が問題なのかわかりません。タイプ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)
リストが作成され、レルムデータが入力された後に追加したいと思います。最初のコンストラクターで、アイデアをパラメーターとして使用します。
クラスコード:
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));
}
}
}