diff --git a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java index 32ef244b..45df3c58 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java +++ b/app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java @@ -35,6 +35,8 @@ import android.widget.ImageButton; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.PopupWindow; +import android.widget.RadioButton; +import android.widget.RadioGroup; import android.widget.RelativeLayout; import android.widget.TextView; @@ -158,12 +160,27 @@ class TopicAdapter extends RecyclerView.Adapter { PollViewHolder holder = (PollViewHolder) currentHolder; holder.question.setText(poll.getQuestion()); if (poll.getAvailableVoteCount() > 1) { - for (int i = 0; i < entries.length; i++) { + LinearLayout optionsLayout = new LinearLayout(context); + optionsLayout.setOrientation(LinearLayout.HORIZONTAL); + for (Poll.Entry entry : entries) { CheckBox checkBox = new CheckBox(context); - checkBox.setText(entries[i].getEntryName()); + checkBox.setText(entry.getEntryName()); checkBox.setOnCheckedChangeListener((buttonView, isChecked) -> - viewModel.onVoteCheckboxClicked(holder.optionsLayout.indexOfChild(buttonView), isChecked)); + viewModel.onVoteCheckboxClicked(optionsLayout.indexOfChild(buttonView), isChecked)); + optionsLayout.addView(checkBox); + } + holder.rootLayout.addView(optionsLayout, 1); + } else if (poll.getAvailableVoteCount() == 1) { + RadioGroup radioGroup = new RadioGroup(context); + for (int i = 0; i < entries.length; i++) { + RadioButton radioButton = new RadioButton(context); + radioButton.setText(entries[i].getEntryName()); + radioButton.setOnClickListener(v -> viewModel.onRadioButtonCLicked(radioGroup.indexOfChild(v))); + radioGroup.addView(radioButton); } + holder.rootLayout.addView(radioGroup, 1); + } else { + //Showing results } } else { Post currentPost = (Post) topicItems.get(position); @@ -665,7 +682,7 @@ class TopicAdapter extends RecyclerView.Adapter { static class PollViewHolder extends RecyclerView.ViewHolder { final TextView question, errorTooManySelected; - final LinearLayout optionsLayout; + final LinearLayout rootLayout; final AppCompatButton submitButton; final AppCompatButton removeVotesButton; @@ -673,7 +690,7 @@ class TopicAdapter extends RecyclerView.Adapter { super(itemView); question = itemView.findViewById(R.id.question_textview); - optionsLayout = itemView.findViewById(R.id.options_layout); + rootLayout = (LinearLayout) itemView; submitButton = itemView.findViewById(R.id.submit_button); removeVotesButton = itemView.findViewById(R.id.remove_vote_button); errorTooManySelected = itemView.findViewById(R.id.error_too_many_checked); diff --git a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java index 1a4c5adc..18ae0ab5 100644 --- a/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java +++ b/app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java @@ -5,8 +5,6 @@ import android.content.Context; import android.content.SharedPreferences; import android.os.AsyncTask; import android.preference.PreferenceManager; -import android.view.View; -import android.widget.CheckBox; import java.util.ArrayList; @@ -46,7 +44,7 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa * holds the adapter position of the post being edited */ private int postBeingEditedPosition; - private ArrayList selectedVoteIndices; + private ArrayList selectedVoteIndices = new ArrayList<>(); private TopicTask currentTopicTask; private PrepareForEditTask currentPrepareForEditTask; @@ -249,12 +247,13 @@ public class TopicViewModel extends BaseViewModel implements TopicTask.OnTopicTa } public void onVoteCheckboxClicked(int index, boolean checked) { - if (checked) { - selectedVoteIndices.add(index); - } else { - selectedVoteIndices.remove(index); - } + if (checked) selectedVoteIndices.add(index); + else selectedVoteIndices.remove(index); + } + public void onRadioButtonCLicked(int index) { + selectedVoteIndices.clear(); + selectedVoteIndices.add(index); } // <-------------Just getters, setters and helper methods below here----------------> diff --git a/app/src/main/res/layout/activity_topic_poll.xml b/app/src/main/res/layout/activity_topic_poll.xml index 3c68a7db..96c55c96 100644 --- a/app/src/main/res/layout/activity_topic_poll.xml +++ b/app/src/main/res/layout/activity_topic_poll.xml @@ -1,5 +1,6 @@ - -