package org.example.mbtiapplication;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.Spinner;
import android.widget.TextView;
public class MBTITest extends Activity implements OnItemSelectedListener {
int I, E, N, S, T, F, J, P;
String[] type;
ArrayList<String> Evaluator = new ArrayList<String>(4);
Spinner firstSpinner ;
Spinner secondSpinner ;
Spinner thirdSpinner;
Spinner fourthSpinner ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
I = 0;
E = 0;
N = 0;
S = 0;
T = 0;
F = 0;
J = 0;
P = 0;
setContentView(R.layout.activity_mbtitest);
firstSpinner = (Spinner) findViewById(R.id.spinner1);
secondSpinner = (Spinner) findViewById(R.id.spinner2);
thirdSpinner = (Spinner) findViewById(R.id.spinner3);
fourthSpinner = (Spinner) findViewById(R.id.spinner4);
type = new String[4];
addItemsOnSpinner1();
addItemsOnSpinner2();
addItemsOnSpinner3();
addItemsOnSpinner4();
Evaluator.add("");
Evaluator.add("");
Evaluator.add("");
Evaluator.add("");
final Button calculate = (Button) findViewById(R.id.calculate);
calculate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
getPersonality();
evaluateType();
StringBuilder typeBuilder = new StringBuilder();
for (String letters : type) {
typeBuilder.append(letters);
}
TextView calculatedPersonality = (TextView) findViewById(R.id.personalitytype);
calculatedPersonality.setText(typeBuilder.toString());
}
});
}
public void addItemsOnSpinner1(){
Spinner firstSpinner = (Spinner) findViewById(R.id.spinner1);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinnerarraybool, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
firstSpinner.setAdapter(adapter);
}
public void addItemsOnSpinner2(){
Spinner firstSpinner = (Spinner) findViewById(R.id.spinner2);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinnerarraybool, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
firstSpinner.setAdapter(adapter);
}
public void addItemsOnSpinner3(){
Spinner firstSpinner = (Spinner) findViewById(R.id.spinner3);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinnerarraybool, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
firstSpinner.setAdapter(adapter);
}
public void addItemsOnSpinner4(){
Spinner firstSpinner = (Spinner) findViewById(R.id.spinner4);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinnerarraybool, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
firstSpinner.setAdapter(adapter);
}
@Override
public void onItemSelected(AdapterView<?> parent, View arg1, int arg2,
long arg3) {
switch (arg1.getId()){
case R.id.spinner1:
String firstAnswer = firstSpinner.getSelectedItem().toString();
Evaluator.remove(0);
Evaluator.add(0 ,firstAnswer.toString());
break;
case R.id.spinner2:
String secondAnswer = firstSpinner.getSelectedItem().toString();
Evaluator.remove(1);
Evaluator.add(1, secondAnswer.toString());
break;
case R.id.spinner3:
String thirdAnswer = firstSpinner.getSelectedItem().toString();
Evaluator.remove(2);
Evaluator.add(2, thirdAnswer.toString());
break;
case R.id.spinner4:
String fourthAnswer = firstSpinner.getSelectedItem().toString();
Evaluator.remove(3);
Evaluator.add(3, fourthAnswer.toString());
break;
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
//Evaluates answers
public void getPersonality(){
if (Evaluator.get(0).equals(R.string.True)){
E = E + 1;
}
if (Evaluator.get(0).equals(R.string.IntheMiddle)){
E = E + 1;
I = I + 1;
}
if (Evaluator.get(0).equals(R.string.False)){
I = I + 1;
}
if (Evaluator.get(1) == "True"){
N += 1;
}
if (Evaluator.get(1) == "In the Middle"){
N += 1;
S += 1;
}
if (Evaluator.get(1) == "False"){
S += 1;
}
if (Evaluator.get(2) == "True"){
F += 1;
}
if (Evaluator.get(2) == "In the Middle"){
F += 1;
T += 1;
}
if (Evaluator.get(2) == "False"){
T += 1;
}
if (Evaluator.get(3) == "True"){
P += 1;
}
if (Evaluator.get(3) == "In the Middle"){
P += 1;
J += 1;
}
if (Evaluator.get(3) == "False"){
J += 1;
}
}
public void evaluateType(){
//INTRO VS EXTRO
if (E > I){
type[0] = "E";
}
if (I > E){
type[0] = "I";
}
if (I == E){
type[0] = "X";
}
//INTUITION VS SENSING
if (N > S){
type[1] = "N";
}
if (S > N){
type[1] = "S";
}
if (S == N){
type[1] = "x";
}
//THINKING VS FEELING
if (T > F){
type[2] = "T";
}
if (F > T){
type[2] = "F";
}
if (T == F){
type[2] = "x";
}
//JUDGING VS PERCIEVING
if (J > P){
type[3] = "J";
}
if (P > J){
type[3] = "P";
}
if (P == J){
type[3] = "x";
}
}
}
正如您在此處看到的,我的文本視圖正在返回Xxxx。我嘗試過不同的方式來回應,但我確定我的問題在於我的getPersonality()方法。我能做些什麼來允許我的evaluateType()方法顯示E,或者我而不是x?我是一個noobie,對不起。 編輯:澄清。我想要我的類型變量來顯示E X或I,具體取決於選擇哪個Spinner項目。我能做些什麼來修復我的變量不會在Android中被更改?
'R.string.True'是一個整數,表示一個字符串的id。 'firstSpinner.getSelectedItem()。toString()'是一個字符串。對這些進行比較沒有任何意義。你會知道在問這個問題之前你是否花時間一步一步地運行你的代碼。 – njzk2