-
Notifications
You must be signed in to change notification settings - Fork 127
/
MainActivity.java
92 lines (73 loc) · 3.29 KB
/
MainActivity.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package androidadvance.com.androidsurveyexample;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import com.androidadvance.androidsurvey.SurveyActivity;
import java.io.IOException;
import java.io.InputStream;
public class MainActivity extends AppCompatActivity {
private static final int SURVEY_REQUEST = 1337;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Nothing fancy here. Plain old simple buttons....
Button button_survey_example_1 = (Button) findViewById(R.id.button_survey_example_1);
Button button_survey_example_2 = (Button) findViewById(R.id.button_survey_example_2);
Button button_survey_example_3 = (Button) findViewById(R.id.button_survey_example_3);
button_survey_example_1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i_survey = new Intent(MainActivity.this, SurveyActivity.class);
//you have to pass as an extra the json string.
i_survey.putExtra("json_survey", loadSurveyJson("example_survey_1.json"));
startActivityForResult(i_survey, SURVEY_REQUEST);
}
});
button_survey_example_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i_survey = new Intent(MainActivity.this, SurveyActivity.class);
i_survey.putExtra("json_survey", loadSurveyJson("example_survey_2.json"));
startActivityForResult(i_survey, SURVEY_REQUEST);
}
});
button_survey_example_3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i_survey = new Intent(MainActivity.this, SurveyActivity.class);
i_survey.putExtra("json_survey", loadSurveyJson("example_survey_3.json"));
startActivityForResult(i_survey, SURVEY_REQUEST);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == SURVEY_REQUEST) {
if (resultCode == RESULT_OK) {
String answers_json = data.getExtras().getString("answers");
Log.d("****", "****************** WE HAVE ANSWERS ******************");
Log.v("ANSWERS JSON", answers_json);
Log.d("****", "*****************************************************");
//do whatever you want with them...
}
}
}
//json stored in the assets folder. but you can get it from wherever you like.
private String loadSurveyJson(String filename) {
try {
InputStream is = getAssets().open(filename);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
return new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
}
}