2013-11-25 46 views
-2
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中被更改?

+1

'R.string.True'是一個整數,表示一個字符串的id。 'firstSpinner.getSelectedItem()。toString()'是一個字符串。對這些進行比較沒有任何意義。你會知道在問這個問題之前你是否花時間一步一步地運行你的代碼。 – njzk2

回答

2

這是很多代碼,並嘗試在帖子中進行調試。你需要遵循代碼,看看它似乎弄亂了。但是,我確實注意到一件事情可能會快速導致您的問題。在您的onItemSelected()中,您正在比較錯誤的View與您的Spinnerid s。第二個參數是TextView,它顯示Spinner項目。你想比較你的第一個參數。此外,你可能想要將你的參數改爲有意義的。這裏是我的意思

@Override 
public void onItemSelected(AdapterView<?> parent, View view, int position, 
     long id) { 
    switch (parent.getId()){ // here use parent 
    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; 

由於A - Ç指出,你在這裏比較Strings錯誤

if (Evaluator.get(1) == "In the Middle") 

.equals()是正確的,因爲那裏比較他們==引用的對象比較值。另外,正如在njzk2上面的評論中指出的那樣,R.string.someString返回引用Stringint id。您需要getString(R.id.someString)才能獲得String值。

我並不是說聽起來很粗魯,但是你真的需要仔細閱讀這篇文章,看一看Java文檔,學習Java更好一點,否則你將會有一段時間學習Android 。而且,如果你只發布你認爲最相關的東西,那麼更多的人會看到它。我相信我可以找到更多,但我不想完成每一段代碼。當我們認爲相關的時候,我們總是可以請求更多的代碼。

+0

另外,他的'getPersonality()'方法將字符串與兩個不正確的方法進行比較。 –

+0

我試過.get()和.equals ...不知道該從哪裏做什麼 –

+1

@ A - C謝謝,我編輯過,以包含它。 – codeMagic

相關問題