Commit a360c8c9 authored by fpeterfalvi's avatar fpeterfalvi
Browse files

MultipleChoiceFragment added

parent 132d0179
......@@ -23,6 +23,7 @@ dependencies {
implementation 'com.android.support:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
implementation 'com.android.support:design:26.1.0'
implementation 'com.android.support:support-v4:26.1.0'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
......
package onlab.kvizclient;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
......@@ -19,9 +21,9 @@ import java.io.OutputStreamWriter;
import java.io.PrintWriter;
import java.net.UnknownHostException;
public class GameActivity extends AppCompatActivity {
public class GameActivity extends AppCompatActivity implements MultipleChoiceFragment.OnFragmentInteractionListener {
private TextView questionText;
private TextView displayTextView;
private BufferedReader input;
......@@ -29,14 +31,15 @@ public class GameActivity extends AppCompatActivity {
Handler updateConversationHandler;
Thread commThread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
questionText = (TextView) findViewById(R.id.QuestionTextView);
final EditText answerEditText = (EditText) findViewById(R.id.AnswerEditText);
Button sendAnswerButton = (Button) findViewById(R.id.SendAnswerButton);
displayTextView = (TextView) findViewById(R.id.DisplayTextView);
updateConversationHandler = new Handler();
try {
......@@ -48,16 +51,15 @@ public class GameActivity extends AppCompatActivity {
e.printStackTrace();
}
sendAnswerButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
output.println(answerEditText.getText());
}
});
CommunicationThread ct = new CommunicationThread();
commThread = new Thread(ct);
commThread.start();
}
CommunicationThread commThread = new CommunicationThread();
Thread newthread = new Thread(commThread);
newthread.start();
@Override
protected void onDestroy() {
commThread.interrupt();
super.onDestroy();
}
class CommunicationThread implements Runnable {
......@@ -66,10 +68,9 @@ public class GameActivity extends AppCompatActivity {
while (!Thread.currentThread().isInterrupted()) {
try {
String read = input.readLine();
Log.d("GameActivity", "Szervertől kapott üzenet: " + read);
if (read != null && !read.equals("$$$$")) {
Log.d("GameActivity", "Szervertől kapott üzenet: " + read);
updateConversationHandler.post(new GameActivity.updateUIThread(read));
//String params[] = read.split("##");
}
} catch (IOException e) {
Thread.currentThread().interrupt();
......@@ -87,8 +88,27 @@ public class GameActivity extends AppCompatActivity {
@Override
public void run() {
questionText.setText(msg);
String[] params = msg.split("##");
if (params.length == 5) {
replaceFragment(params);
}
}
}
private void replaceFragment(String[] strings) {
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
MultipleChoiceFragment frag = MultipleChoiceFragment.newInstance(strings[0], strings[1], strings[2], strings[3], strings[4]);
fragmentTransaction.replace(R.id.FragmentContainer, frag);
fragmentTransaction.commit();
}
@Override
public void onAnswered(String answer) {
Log.d("A válasz: ", answer);
displayTextView.setText(answer);
output.println(answer);
}
}
......@@ -98,6 +98,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
@Override
protected void onDestroy() {
Log.d("LobbyActivity", "Destroyed");
if (mNsdManager != null && discovering) {
mNsdManager.stopServiceDiscovery(mDiscoveryListener);
discovering = false;
......@@ -294,7 +295,7 @@ public class LobbyActivity extends AppCompatActivity implements ServerConnection
} catch (Exception e) {
e.printStackTrace();
}
while (!Thread.currentThread().isInterrupted()) {
while (!Thread.currentThread().isInterrupted() && exit) {
try {
if (serverModel.getSocket().isConnected()) {
Log.d("LobbyActivity", "Connected");
......
package onlab.kvizclient;
import android.app.Activity;
import android.app.Fragment;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MultipleChoiceFragment extends Fragment {
private static final String QUESTION = "QUESTION";
private static final String ANSWER1 = "ANSWER1";
private static final String ANSWER2 = "ANSWER2";
private static final String ANSWER3 = "ANSWER3";
private static final String ANSWER4 = "ANSWER4";
private String question;
private String[] answers = new String[4];
private TextView questionTextTextView;
private Button[] answerButtons = new Button[4];
private OnFragmentInteractionListener mListener;
public MultipleChoiceFragment() {
// Required empty public constructor
}
public static MultipleChoiceFragment newInstance(String question, String answer1, String answer2, String answer3, String answer4) {
MultipleChoiceFragment fragment = new MultipleChoiceFragment();
Bundle args = new Bundle();
args.putString(QUESTION, question);
args.putString(ANSWER1, answer1);
args.putString(ANSWER2, answer2);
args.putString(ANSWER3, answer3);
args.putString(ANSWER4, answer4);
fragment.setArguments(args);
return fragment;
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
question = getArguments().getString(QUESTION);
answers[0] = getArguments().getString(ANSWER1);
answers[1] = getArguments().getString(ANSWER2);
answers[2] = getArguments().getString(ANSWER3);
answers[3] = getArguments().getString(ANSWER4);
}
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_multiple_choice, container, false);
}
@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
questionTextTextView = (TextView) getView().findViewById(R.id.QuestionTextTextView);
questionTextTextView.setText(question);
answerButtons[0] = (Button) getView().findViewById(R.id.Answer1Button);
answerButtons[1] = (Button) getView().findViewById(R.id.Answer2Button);
answerButtons[2] = (Button) getView().findViewById(R.id.Answer3Button);
answerButtons[3] = (Button) getView().findViewById(R.id.Answer4Button);
for (int i=0;i<4;i++) {
answerButtons[i].setOnClickListener(new MyOnClickListener(i));
answerButtons[i].setText(answers[i]);
}
}
@Override
public void onAttach(Activity context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
@Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public interface OnFragmentInteractionListener {
void onAnswered(String answer);
}
class MyOnClickListener implements View.OnClickListener {
int index;
public MyOnClickListener(int index) {
this.index = index;
}
@Override
public void onClick(View v) {
if (mListener != null) {
mListener.onAnswered(answerButtons[index].getText().toString());
Log.d("FRAGMENT", "Válasz történt");
}
}
};
}
......@@ -14,20 +14,16 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/QuestionTextView"
android:text="Ide jön a kérdés"/>
android:id="@+id/DisplayTextView"
android:text="Ez a TextView nem tartozik a fragmenthez"/>
<EditText
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/AnswerEditText"
android:text="Ide írd a választ"/>
android:orientation="vertical"
android:id="@+id/FragmentContainer">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/SendAnswerButton"
android:text="Send"/>
</LinearLayout>
</LinearLayout>
......
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="onlab.kvizclient.MultipleChoiceFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/QuestionTextTextView"
android:text="Kérdés"
android:textSize="20sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Answer1Button"
android:text="Válasz1"
android:textSize="20sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Answer2Button"
android:text="Válasz2"
android:textSize="20sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Answer3Button"
android:text="Válasz3"
android:textSize="20sp"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/Answer4Button"
android:text="Válasz4"
android:textSize="20sp"/>
</LinearLayout>
</FrameLayout>
Melyik hónap 31 napos? július február június november
Mi Olaszország fővárosa? Róma London Berlin Moszkva
Melyik nem emlős? szula oroszlán farkas rozmár
Melyik kontinensen található Peru? Dél-Amerika Észak-Amerika Ázsia Afrika
Melyik város van legtávolabb Budapesttől? Sydney Makó Jeruzsálem London
Melyik nem trapéz? deltoid paralelogramma négyzet téglalap
Melyik NEM gázbolygó? Föld Jupiter Uránusz Neptunusz
Melyik folyó partján fekszik Budapest? Duna Jangce Nílus Szajna
......@@ -26,10 +26,11 @@ import java.util.List;
import onlab.kvizserver.model.ClientModel;
import onlab.kvizserver.model.GuessQuestion;
import onlab.kvizserver.model.MultipleChoiceQuestion;
public class GameActivity extends AppCompatActivity {
private List<GuessQuestion> questions = new ArrayList<>();
private List<MultipleChoiceQuestion> questions = new ArrayList<>();
private int questionIndex = 0;
......@@ -41,6 +42,8 @@ public class GameActivity extends AppCompatActivity {
final List<TextView> playerAnswerTextViews = new ArrayList<>();
final List<String> answers = new ArrayList<>();
Handler updateConversationHandler;
@Override
......@@ -55,15 +58,16 @@ public class GameActivity extends AppCompatActivity {
BufferedReader buffreader;
try {
if (questionFileName == null) {
buffreader = new BufferedReader(new InputStreamReader(getAssets().open("defaultQuestions.txt")));
buffreader = new BufferedReader(new InputStreamReader(getAssets().open("multiple.txt")));
} else {
buffreader = new BufferedReader(new InputStreamReader(new FileInputStream(new File(questionFileName))));
}
String line;
while ((line = buffreader.readLine()) != null) {
String[] parts = line.split("\t");
GuessQuestion guessQuestion = new GuessQuestion(parts[0], Integer.parseInt(parts[1]));
questions.add(guessQuestion);
MultipleChoiceQuestion multipleChoiceQuestion = new MultipleChoiceQuestion(
parts[0], parts[1], parts[2], parts[3], parts[4]);
questions.add(multipleChoiceQuestion);
}
} catch (IOException e) {
e.printStackTrace();
......@@ -71,7 +75,11 @@ public class GameActivity extends AppCompatActivity {
Button nextQuestionBtn = (Button) findViewById(R.id.NextQuestion);
final TextView questionText = (TextView) findViewById(R.id.QuestionText);
final TextView correctAnswerText = (TextView) findViewById(R.id.CorrectAnswerText);
final List<TextView> answerTextViews = new ArrayList<>();
answerTextViews.add((TextView) findViewById(R.id.Answer1TextView));
answerTextViews.add((TextView) findViewById(R.id.Answer2TextView));
answerTextViews.add((TextView) findViewById(R.id.Answer3TextView));
answerTextViews.add((TextView) findViewById(R.id.Answer4TextView));
final LinearLayout playerAnswersLinearLayout = (LinearLayout) findViewById(R.id.PlayerAnswers);
numberOfPlayers = ClientHolder.size();
......@@ -101,18 +109,25 @@ public class GameActivity extends AppCompatActivity {
@Override
public void onClick(View view) {
if (questionIndex < questions.size()) {
GuessQuestion question = questions.get(questionIndex);
MultipleChoiceQuestion question = questions.get(questionIndex);
String questionString = question.getQuestionText();
questionText.setText(questionString);
correctAnswerText.setText("Correct answer: " + question.getCorrectAnswer());
answers.clear();
answers.addAll(question.getAnswers());
for (int i=0;i<4;i++) {
answerTextViews.get(i).setText(answers.get(i));
}
for (int i=0;i<outputs.size();i++) {
playerAnswerTextViews.get(i).setText("The answer of the " + Integer.toString(i + 1) + ". player: ");
outputs.get(i).println(questionString);
outputs.get(i).println(questionString + "##" + answers.get(0) + "##" + answers.get(1)
+ "##" + answers.get(2) + "##" + answers.get(3));
}
questionIndex++;
} else {
questionText.setText("No more questions.");
correctAnswerText.setText("");
for (int i=0;i<4;i++) {
answerTextViews.get(i).setText("");
}
}
}
});
......
package onlab.kvizserver.model;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class MultipleChoiceQuestion {
private String questionText;
private String correctAnswer;
private List<String> otherAnswers = new ArrayList<>();
public MultipleChoiceQuestion(String questionText, String correctAnswer, String ans2, String ans3, String ans4) {
this.questionText = questionText;
this.correctAnswer = correctAnswer;
otherAnswers.add(ans2);
otherAnswers.add(ans3);
otherAnswers.add(ans4);
}
public String getQuestionText() {
return questionText;
}
public String getCorrectAnswer() {
return correctAnswer;
}
public List<String> getAnswers() {
List<String> list = new ArrayList<>();
list.add(correctAnswer);
list.addAll(otherAnswers);
Collections.shuffle(list);
return list;
}
}
......@@ -21,8 +21,29 @@
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Correct answer:"
android:id="@+id/CorrectAnswerText"
android:text="Answer1"
android:id="@+id/Answer1TextView"
android:textSize="30sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Answer2"
android:id="@+id/Answer2TextView"
android:textSize="30sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Answer3"
android:id="@+id/Answer3TextView"
android:textSize="30sp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Answer4"
android:id="@+id/Answer4TextView"
android:textSize="30sp"/>
<LinearLayout
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment