Browse Source

cover single choice poll

pull/55/head
Thodoris1999 6 years ago
parent
commit
a381bc8e39
  1. 27
      app/src/main/java/gr/thmmy/mthmmy/activities/topic/TopicAdapter.java
  2. 15
      app/src/main/java/gr/thmmy/mthmmy/viewmodel/TopicViewModel.java
  3. 7
      app/src/main/res/layout/activity_topic_poll.xml

27
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<RecyclerView.ViewHolder> {
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<RecyclerView.ViewHolder> {
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<RecyclerView.ViewHolder> {
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);

15
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<Integer> selectedVoteIndices;
private ArrayList<Integer> 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---------------->

7
app/src/main/res/layout/activity_topic_poll.xml

@ -1,5 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/rootLayout"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
@ -10,12 +11,6 @@
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:id="@+id/options_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"/>
<TextView
android:id="@+id/error_too_many_checked"
android:layout_width="wrap_content"

Loading…
Cancel
Save